Request and response validator against OpenAPI Specification
If you are looking for framework specific middleware, you might want to look at following libraries that use oas3-chow-chow under the hood.
$ yarn add oas3-chow-chow
$ # Or
$ npm i oas3-chow-chow
import ChowChow from "oas3-chow-chow";
import * as fs from "fs";
import * as yaml from "js-yaml";
var doc = yaml.safeLoad(fs.readFileSync("./openapi.yml", "utf8"));
const chow = new ChowChow(doc);
chow.validateRequest("./books", {
method: "post",
query: {
expand: ["document", "author"]
},
body: {
name: "a nice book",
author: "me me me"
}
});
chow.validateResponse("./books", {
method: "post",
header: {
"content-type": "application/json"
},
body: {
name: "a nice book",
author: "me me me"
}
});
You could optionally provide configs to the constructor
const chow = new ChowChow(doc, {
headerAjvOptions: {},
cookieAjvOptions: {},
pathAjvOptions: { coerceTypes: true },
queryAjvOptions: { coerceTypes: 'array' },
requestBodyAjvOptions: {},
responseBodyAjvOptions: {},
});
- headerAjvOptions: Ajv options that pass to header ajv instance
- cookieAjvOptions: Ajv options that pass to cookie ajv instance
- pathAjvOptions: Ajv options that pass to path ajv instance, default
{ coerceTypes: true }
- queryAjvOptions: Ajv options that pass to query ajv instance, default
{ coerceTypes: 'array' }
- requestBodyAjvOptions: Ajv options that pass to request body ajv instance
- responseBodyAjvOptions: Ajv options that pass to response body ajv instance