validate requests against your openapi3 definition
npm install openapi3-middleware
validator checks incoming request as identified by operationId, therefore operationId fields in api definition are required for explicitly matching the right implementation. It's another layer of certainty for your implementation.
const openapi3middleware =const validatorInstance = openapi3middleware// Use directlyvalidatorInstance// throws a validation error if anything goes wrong// Express middleware with default responsesappapp// returns a response with a status code matching what happened and a descriptive message to help fix the error// Express middlewareapp// propagates an error, so custom response can be built in an express error handler// Validating responses BETAconst req =body: <parsed JSON>statusCode: 200method: 'get'headers:'content-type': 'application/json'validatorInstance
Expected format of req object
If you want to use the validator outside express, you can provide a plain object where
req is expected. It needs to provide the fields shown below.
req :=path: 'string'route: path: 'string' // all use of : is removed, the :paramName will be compared with paramName from definitionmethod: 'string' // method name, case insensitive match with the API specbody: Body // Whatever you expect as body - probably an object resulting from parsing JSONheaders: //headers are optional, but if request has a body, the content-type header must exist and match the key in content field exactly.
req =path: '/pet/1'route: path: '/pet/:petId'method: 'post'body: ...headers:'content-type': 'application/json'
Contributing other framework specific middlewares is more than welcome.
When contributing make sure you run
npm run lint and don't get any errors nor warnings.
Feel free to consciously add security warning exceptions, we can discuss in the pull request.