Package Exports
- express-openapi-validate
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (express-openapi-validate) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
express-openapi-validate
Express middleware to validate request based on an OpenAPI 3.0 document. OpenAPI used to be called the Swagger specification before version 3.
This package is in early development so most features that you would except are missing.
Usage
Install this package with npm or yarn:
npm install --save express-openapi-validate
# or
yarn add express-openapi-validate
Then use the validator like this:
const fs = require("fs");
const express = require("express");
const OpenApiValidator = require("express-openapi-validate");
const jsYaml = require("js-yaml");
const app = express();
app.use(express.json());
const openApiDocument = jsYaml.safeLoad(
fs.readFileSync("openapi.yaml", "utf-8")
);
const openApiValidator = new OpenApiValidator(openApiDocument);
app.post(
"/echo",
openApiValidator.validate("post", "/echo"),
(req, res, next) => {
res.json({ output: req.body.input });
}
);
app.use((err, req, res, next) => {
const statusCode = err.statusCode || 500;
res.status(statusCode).json({
error: {
name: err.name,
message: err.message,
data: err.data,
},
});
});
# openapi.yaml
openapi: 3.0.1
info:
title: Example API
version: 1.0.0
paths:
/echo:
post:
description: Echo input back
requestBody:
content:
application/json:
schema:
type: object
properties:
input:
type: string
required:
- input
responses:
200:
content:
application/json:
schema:
type: object
properties:
output:
type: string