Skip to content

Repository files navigation

OpenAPI Data Validator

Lightweight OpenAPI complete request model validator. Fast, unopinionated, full featured validator for API requests that utilize OpenAPI docs for API documentation.

This is an open source project managed by the Authress Engineering team.

Authress Engineeringnpm version

Usage

It is simple, and that's all there is to it!

npm install openapi-data-validator --save
const{ OpenApiValidator }=require('openapi-data-validator');constspec=require('./openapi.json');constopenApiValidator=newOpenApiValidator({apiSpec: spec});constvalidator=openApiValidator.createValidator();// Configure this to the client's request. It will resolve the expected schema in the spec using the method and route defined, and validate the request parameters.constnewRequest={method: 'GET',// Matched openapi specification generic route, this should be the generic `path` from the spec, such as `/resources/{resourceId}/`, it must match one of them exactly.route: request.routeheaders: {Authorization: 'Bearer Token'},// Query string parameters from the requestquery: {limit: 10},// Body already parsed to JSONbody: {field: true},// Path parameterspath: {user: 'userId'}};awaitvalidator(newRequest);

Compile validator

For improved processing speed the validator can be pre-compiled from the spec

constopenApiValidator=newOpenApiValidator({apiSpec: spec,compiledFilePath: './compiledValidator.json'});awaitopenApiValidator.compileValidator();// Laterconstvalidator=awaitopenApiValidator.loadValidation();// ...awaitvalidator(request);

Checkout the full: Async example

FAQs

Why not just use AJV

AJV is the best, but there are some things that just are very OpenAPI specific that don't make sense to be in the validator. Don't need them? Great, go use AJV.

  • Top level defined Path parameters - AJV doesn't understand
  • Inline request body definitions, AJV doesn't understand schema defined in the method, it has to be in a component
  • Body Content-Type validation - Request bodies with multiple content types allowed

About

OpenAPI Request Validation for API for any framework

Topics

Resources

Stars

16 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages