Skip to content

Commit e5d9a77

Browse files
authored
docs: improve Ajv instance README guidance (#127)
1 parent 5457b75 commit e5d9a77

1 file changed

Lines changed: 59 additions & 12 deletions

File tree

README.md

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ const addressSchema = {
5151
* Initialize a `Validator` instance, optionally passing in
5252
* an Ajv options object.
5353
*
54-
* @see https://github.com/ajv-validator/ajv/tree/v6#options
54+
* @see https://ajv.js.org/options.html
5555
*/
56-
const { validate } = new Validator();
56+
const validator = new Validator();
57+
const { validate } = validator;
5758

5859
/**
5960
* The `validate` method accepts an object which maps request
@@ -72,6 +73,10 @@ app.post("/address", validate({ body: addressSchema }), (request, response) => {
7273
});
7374
```
7475

76+
If you need to add formats, plugins, or custom keywords to Ajv, create a
77+
`Validator` instance first, configure `validator.ajv`, and only then call
78+
`validate()`. See [Ajv instance](#ajv-instance).
79+
7580
Coming from `express-jsonschema`? Read the [migration notes](docs/migrating-from-express-jsonschema.md).
7681

7782
### Schemas in TypeScript
@@ -149,7 +154,8 @@ const addressSchema = {
149154
},
150155
};
151156

152-
const { validate } = new Validator();
157+
const validator = new Validator();
158+
const { validate } = validator;
153159

154160
/**
155161
* Validate `request.body` against `addressSchema`.
@@ -295,19 +301,60 @@ app.post(
295301

296302
## Ajv instance
297303

298-
The Ajv instance can be accessed via `validator.ajv`.
304+
Each `Validator` instance exposes the underlying Ajv instance as
305+
`validator.ajv`.
306+
307+
This is useful if you need to:
308+
309+
- add formats with [`ajv-formats`](https://www.npmjs.com/package/ajv-formats)
310+
- register Ajv plugins
311+
- define [custom keywords](https://ajv.js.org/guide/user-keywords.html)
312+
313+
Important: `validate()` compiles the schemas you pass to it when the middleware
314+
is created. Configure `validator.ajv` before you call `validate()` or add the
315+
middleware to a route.
316+
317+
For example, if your schema uses the `email` format, register
318+
`ajv-formats` first and then create the middleware:
299319

300320
```javascript
301-
import { Validator, ValidationError } from "express-json-validator-middleware";
321+
import { Validator } from "express-json-validator-middleware";
322+
import addFormats from "ajv-formats";
302323

303-
const validator = new Validator();
324+
const validator = new Validator({ allErrors: true });
325+
326+
addFormats(validator.ajv);
327+
328+
const userSchema = {
329+
type: "object",
330+
required: ["email"],
331+
properties: {
332+
email: {
333+
type: "string",
334+
format: "email",
335+
},
336+
},
337+
};
338+
339+
const { validate } = validator;
304340

305-
// Ajv instance
306-
validator.ajv;
341+
app.post("/user", validate({ body: userSchema }), (request, response) => {
342+
response.send({});
343+
});
307344
```
308345

309-
Ajv must be configured *before* you call `Validator.validate()` to add middleware
310-
(e.g. if you need to define [custom keywords](https://ajv.js.org/custom.html).
346+
Plugins and custom keywords follow the same pattern:
347+
348+
```javascript
349+
someAjvPlugin(validator.ajv);
350+
351+
validator.ajv.addKeyword({
352+
keyword: "isEven",
353+
type: "number",
354+
schemaType: "boolean",
355+
validate: (schema, data) => !schema || data % 2 === 0,
356+
});
357+
```
311358

312359
## Upgrading from v2 to v3
313360

@@ -317,8 +364,8 @@ v3.x of this library uses [Ajv v8](https://www.npmjs.com/package/ajv/v/8.11.0).
317364
Notable changes between Ajv v6 and v8:
318365

319366
- All formats have been moved to [ajv-formats](https://www.npmjs.com/package/ajv-formats).
320-
If you're using formats in your schemas, you must install this package to continue
321-
using them.
367+
If you're using formats in your schemas, you must install this package and add it
368+
to `validator.ajv` before calling `validate()`. See [Ajv instance](#ajv-instance).
322369
- The structure of validation errors has changed.
323370
- Support has been dropped for JSON Schema draft-04.
324371

0 commit comments

Comments
 (0)