Skip to content

Commit c9a3dae

Browse files
committed
Finishing touches
1 parent 3634db5 commit c9a3dae

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

README.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ var { Validator, ValidationError } = require('express-json-validator-middleware'
5050
var validator = new Validator({allErrors: true});
5151
```
5252

53-
3. *Optional* - Define a shortcut function. Bind is necessary here in order to pass `this` correctly
53+
3. *Optional* - Define a shortcut function.
5454
```js
55-
var validate = validator.validate.bind(validator);
55+
var validate = validator.validate;
5656
```
5757

5858
4. Use the function as an Express middleware, passing in an options object of the following format:
@@ -96,10 +96,12 @@ var app = express();
9696
var bodyParser = require('body-parser');
9797

9898
var { Validator, ValidationError } = require('express-json-validator-middleware');
99+
99100
// Initialize a Validator instance first
100101
var validator = new Validator({allErrors: true}); // pass in options to the Ajv instance
102+
101103
// Define a shortcut. It is perfectly okay to use validator.validate() as middleware, this just makes it easier
102-
var validate = validator.validate.bind(validator);
104+
var validate = validator.validate;
103105

104106
// Define a JSON Schema
105107
var StreetSchema = {
@@ -167,7 +169,7 @@ validator.ajv.addKeyword('constant', { validate: function (schema, data) {
167169
: schema === data;
168170
}, errors: false });
169171

170-
// free to use validate() here
172+
// free to validator in middleware now
171173
```
172174

173175
More info on custom keywords: [ajv#customs-keywords](https://github.com/epoberezkin/ajv/blob/master/CUSTOM.md#defining-custom-keywords)
@@ -208,7 +210,7 @@ In `express-jsonschema`, you could define a required property in two ways. Ajv o
208210
foo: {
209211
type: 'string'
210212
},
211-
required: ['foo'] <--
213+
required: ['foo'] // <-- correct way
212214
}
213215
}
214216

@@ -218,7 +220,7 @@ In `express-jsonschema`, you could define a required property in two ways. Ajv o
218220
properties: {
219221
foo: {
220222
type: 'string',
221-
required: true
223+
required: true // nono way
222224
}
223225
}
224226
}

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Validator {
2020
let schema = options[requestProperty];
2121
let validateFunction = this.ajv.compile(schema);
2222

23-
var valid = validateFunction(req[requestProperty]);
23+
let valid = validateFunction(req[requestProperty]);
2424

2525
if (!valid) {
2626
validationErrors[requestProperty] = validateFunction.errors;

0 commit comments

Comments
 (0)