Skip to content

Commit 457917e

Browse files
authored
Merge pull request #12 from JouzaLoL/dev
Dev merge
2 parents 3a6db73 + c9a3dae commit 457917e

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
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

+30-29
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
11
var Ajv = require('ajv');
22

33
class Validator {
4-
constructor(options) {
5-
this.ajv = new Ajv(options);
6-
}
4+
constructor(ajvOptions) {
5+
this.ajv = new Ajv(ajvOptions);
6+
this.validate = this.validate.bind(this);
7+
}
78
/**
89
* Express middleware for validating requests
910
*
1011
* @param {Object} options
1112
* @returns
1213
*/
13-
validate(options) {
14-
var self = this;
15-
return function (req, res, next) {
16-
var validationErrors = {};
14+
validate(options) {
15+
var self = this;
16+
return function (req, res, next) {
17+
var validationErrors = {};
1718

18-
Object.keys(options).forEach(function (requestProperty) {
19-
let schema = options[requestProperty];
20-
let validateFunction = this.ajv.compile(schema);
19+
Object.keys(options).forEach(function (requestProperty) {
20+
let schema = options[requestProperty];
21+
let validateFunction = this.ajv.compile(schema);
2122

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

24-
if (!valid) {
25-
validationErrors[requestProperty] = validateFunction.errors;
26-
}
27-
}, self);
25+
if (!valid) {
26+
validationErrors[requestProperty] = validateFunction.errors;
27+
}
28+
}, self);
2829

29-
if (Object.keys(validationErrors).length != 0) {
30-
next(new ValidationError(validationErrors));
31-
} else {
32-
next();
33-
}
34-
};
35-
}
30+
if (Object.keys(validationErrors).length != 0) {
31+
next(new ValidationError(validationErrors));
32+
} else {
33+
next();
34+
}
35+
};
36+
}
3637
}
3738

3839

@@ -51,14 +52,14 @@ class ValidationError extends Error {
5152
*
5253
* @memberOf ValidationError
5354
*/
54-
constructor(validationErrors) {
55-
super();
56-
this.name = 'JsonSchemaValidationError';
57-
this.validationErrors = validationErrors;
58-
}
55+
constructor(validationErrors) {
56+
super();
57+
this.name = 'JsonSchemaValidationError';
58+
this.validationErrors = validationErrors;
59+
}
5960
};
6061

6162
module.exports = {
62-
Validator,
63-
ValidationError
63+
Validator,
64+
ValidationError
6465
};

test/integration-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ app.use(bodyParser.json());
1717
var validator = new Validator({
1818
allErrors: true
1919
});
20-
var validate = validator.validate.bind(validator);
20+
var validate = validator.validate;
2121

2222
var personSchema = {
2323
properties: {

0 commit comments

Comments
 (0)