Skip to content

Commit fad0c2f

Browse files
committed
Clean up README
1 parent 7fe9ca7 commit fad0c2f

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

README.md

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
[![npm](https://img.shields.io/npm/v/express-json-validator-middleware.svg)](https://www.npmjs.com/package/express-json-validator-middleware)
88
[![npm](https://img.shields.io/npm/l/express-json-validator-middleware.svg)](https://www.npmjs.com/package/express-json-validator-middleware)
99

10-
This package is a work in progress - feedback is heavily appreciated
11-
1210
Based heavily on https://github.com/trainiac/express-jsonschema. A big thank you to @trainiac for the original package!
1311

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) ?
1513

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),
15+
- **Latest JSON Schema Standard** - [ajv](https://github.com/epoberezkin/ajv) supports JSON Schema v5 proposal.
1816
- **Active Maintenance** - ```express-json-validator-middleware``` is being actively maintained by @JouzaLoL
1917

2018
## Why validate with JSON schemas?
@@ -29,9 +27,11 @@ Based heavily on https://github.com/trainiac/express-jsonschema. A big thank you
2927
## Installation
3028

3129
```sh
32-
$ npm install express-json-validator-middleware --save-dev
30+
$ npm install express-json-validator-middleware
3331
```
3432

33+
```--save``` is no longer necessary as of ```npm@5```
34+
3535
## Getting started
3636

3737
0. Install the module
@@ -40,7 +40,8 @@ $ npm install express-json-validator-middleware --save-dev
4040
var { Validator, ValidationError } = require('express-json-validator-middleware');
4141
```
4242

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
44+
4445
```js
4546
var validator = new Validator({allErrors: true});
4647
```
@@ -65,14 +66,12 @@ app.post('/street/', validate({body: BodySchema}), function(req, res) {
6566
});
6667
```
6768

68-
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-
7069
## Error handling
7170

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
7473

75-
Error example (pseudocode):
74+
Example - error thrown for the `body` request property
7675

7776
```js
7877
ValidationError {
@@ -83,7 +82,7 @@ ValidationError {
8382
}
8483
```
8584

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)
8786

8887
## Example Express app
8988

@@ -95,7 +94,7 @@ var bodyParser = require('body-parser');
9594
var { Validator, ValidationError } = require('express-json-validator-middleware');
9695
// Initialize a Validator instance first
9796
var validator = new Validator({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
9998
var validate = validator.validate.bind(validator);
10099

101100
// Define a JSON Schema
@@ -150,7 +149,9 @@ A valid request must now include a token URL query. Example valid URL: ```/stree
150149

151150
## Custom keywords
152151

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:
154155

155156
```js
156157
var { Validator, ValidationError } = require('express-json-validator-middleware');
@@ -161,6 +162,8 @@ validator.ajv.addKeyword('constant', { validate: function (schema, data) {
161162
? deepEqual(schema, data)
162163
: schema === data;
163164
}, errors: false });
165+
166+
// free to use validate() here
164167
```
165168

166169
More info on custom keywords: [ajv#customs-keywords](https://github.com/epoberezkin/ajv/blob/master/CUSTOM.md#defining-custom-keywords)
@@ -189,29 +192,29 @@ npm test
189192

190193
## Notes
191194

192-
In ```express-jsonschema```, you could define a required property in two ways. Ajv only supports the latter.
195+
In ```express-jsonschema```, you could define a required property in two ways. Ajv only supports one way of doing this.
193196

194197
```js
195-
// WRONG
198+
199+
// CORRECT
196200
{
197201
type: 'object',
198202
properties: {
199203
foo: {
200-
type: 'string',
201-
required: true
202-
}
204+
type: 'string'
205+
},
206+
required: ['foo']
203207
}
204208
}
205209

206-
// CORRECT
207-
210+
// WRONG
208211
{
209212
type: 'object',
210213
properties: {
211214
foo: {
212-
type: 'string'
213-
},
214-
required: ['foo']
215+
type: 'string',
216+
required: true
217+
}
215218
}
216219
}
217-
```
220+
```

0 commit comments

Comments
 (0)