Skip to content

Commit ecb8424

Browse files
committed
Add documentation for OpenApiValidator.ajv function initialization usage
cdimascio#683
1 parent 52429c5 commit ecb8424

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,38 @@ function routesV2(app) {
11481148

11491149
module.exports = app;
11501150
```
1151+
## Use OpenAPIValidator AJV out of express usage
1152+
1153+
You can get an AJV module as OpenAPIValidator generates it for express.
1154+
Then you can use it for other usages such as websocket request validation...
1155+
Instead of initialize OpenApiValidator with middleware, you can get a configured AJV object with all OpenApiValidator mecanisms (serdes...) and loaded schemas.
1156+
1157+
1158+
```javascript
1159+
const ajv = await OpenApiValidator.ajv({
1160+
apiSpec: './openapi.yaml',
1161+
validateRequests: true, // (default)
1162+
validateResponses: true, // false by default
1163+
});
1164+
1165+
const req : ReqClass = {
1166+
id : '507f191e810c19729de860ea',
1167+
}
1168+
1169+
ajv.validate(
1170+
{
1171+
type: 'object',
1172+
properties: {
1173+
id: {
1174+
$ref: '#/components/schemas/ObjectId',
1175+
},
1176+
},
1177+
required: ['token'],
1178+
additionalProperties: false,
1179+
},
1180+
req
1181+
);
1182+
```
11511183

11521184
## FAQ
11531185

0 commit comments

Comments
 (0)