You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This package is a work in progress - feedback is heavily appreciated
11
-
12
10
Based heavily on https://github.com/trainiac/express-jsonschema. A big thank you to @trainiac for the original package!
13
11
14
-
## Why use this library instead of[express-jsonschema](https://github.com/trainiac/express-jsonschema) ?
12
+
## Why use this library over[express-jsonschema](https://github.com/trainiac/express-jsonschema) ?
15
13
16
-
-**Performance** - We use [ajv](https://github.com/epoberezkin/ajv)instead of [JSONSchema](https://github.com/tdegrunt/jsonschema), offering a significant performance boost.
17
-
-**Newer JSON Schema Standard** - Using [ajv](https://github.com/epoberezkin/ajv)under the hood allows us to support JSON Schema v5 proposal.
14
+
-**Performance** - [ajv](https://github.com/epoberezkin/ajv)offers a significant performance boost over [JSONSchema](https://github.com/tdegrunt/jsonschema),
var { Validator, ValidationError } =require('express-json-validator-middleware');
41
41
```
42
42
43
-
2. Initialize a Validator instance, passing in an optional options object for the Ajv instance. Ajv options can be found here: [ajv#options](https://github.com/epoberezkin/ajv#options)
43
+
2. Initialize a Validator instance, optionally passing in an [ajv#options](https://github.com/epoberezkin/ajv#options) object
5. The validator will either do nothing, if the data is valid, or call next() with a ValidationError as a parameter, if the data is found to be erroneous.
69
-
70
69
## Error handling
71
70
72
-
On encountering erroneous data, the validator will call next with a ValidationError object.
73
-
It is recommended to setup a general error handler for your express app where you will catch errors of type ValidationError
71
+
On encountering erroneous data, the validator will call next() with a ValidationError object.
72
+
It is recommended to setup a general error handler for your app where you will catch ValidationError errors
74
73
75
-
Error example (pseudocode):
74
+
Example - error thrown for the `body` request property
76
75
77
76
```js
78
77
ValidationError {
@@ -83,7 +82,7 @@ ValidationError {
83
82
}
84
83
```
85
84
86
-
Information on Ajv errors can be found here:[ajv#errors](https://github.com/epoberezkin/ajv#validation-errors)
85
+
More information on[ajv#errors](https://github.com/epoberezkin/ajv#validation-errors)
87
86
88
87
## Example Express app
89
88
@@ -95,7 +94,7 @@ var bodyParser = require('body-parser');
95
94
var { Validator, ValidationError } =require('express-json-validator-middleware');
96
95
// Initialize a Validator instance first
97
96
var validator =newValidator({allErrors:true}); // pass in options to the Ajv instance
98
-
// Define a shortcut. It is perfectly okay to use validator.validate() as middleware
97
+
// Define a shortcut. It is perfectly okay to use validator.validate() as middleware, this just makes it easier
99
98
var validate =validator.validate.bind(validator);
100
99
101
100
// Define a JSON Schema
@@ -150,7 +149,9 @@ A valid request must now include a token URL query. Example valid URL: ```/stree
150
149
151
150
## Custom keywords
152
151
153
-
Ajv supports custom keywords out of the box. They must be defined only after you initialize a Validator, but before you any validate() middleware. Example:
152
+
Ajv custom keywords must be defined *before* any validate() middleware
153
+
154
+
Example:
154
155
155
156
```js
156
157
var { Validator, ValidationError } =require('express-json-validator-middleware');
0 commit comments