Skip to content

Commit

Permalink
Add documentation for OpenApiValidator.ajv function initialization usage
Browse files Browse the repository at this point in the history
  • Loading branch information
pilerou committed Dec 24, 2021
1 parent 52429c5 commit ecb8424
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,38 @@ function routesV2(app) {

module.exports = app;
```
## Use OpenAPIValidator AJV out of express usage

You can get an AJV module as OpenAPIValidator generates it for express.
Then you can use it for other usages such as websocket request validation...
Instead of initialize OpenApiValidator with middleware, you can get a configured AJV object with all OpenApiValidator mecanisms (serdes...) and loaded schemas.


```javascript
const ajv = await OpenApiValidator.ajv({
apiSpec: './openapi.yaml',
validateRequests: true, // (default)
validateResponses: true, // false by default
});

const req : ReqClass = {
id : '507f191e810c19729de860ea',
}

ajv.validate(
{
type: 'object',
properties: {
id: {
$ref: '#/components/schemas/ObjectId',
},
},
required: ['token'],
additionalProperties: false,
},
req
);
```

## FAQ

Expand Down

0 comments on commit ecb8424

Please sign in to comment.