express-json-validator-middleware
Advanced tools
Comparing version 1.1.0 to 1.1.1
{ | ||
"name": "express-json-validator-middleware", | ||
"version": "1.1.0", | ||
"description": "An Express middleware to validate requests against JSON Schemas", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/JouzaLoL/express-json-validator-middleware.git" | ||
}, | ||
"keywords": [ | ||
"express", | ||
"json", | ||
"validate", | ||
"validation", | ||
"validator" | ||
], | ||
"author": "Josef Vacek", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/JouzaLoL/express-json-validator-middleware/issues" | ||
}, | ||
"homepage": "https://github.com/JouzaLoL/express-json-validator-middleware#readme", | ||
"devDependencies": { | ||
"body-parser": "^1.17.1", | ||
"chai": "^3.5.0", | ||
"chai-http": "^3.0.0", | ||
"codecov": "^2.1.0", | ||
"coveralls": "^2.13.0", | ||
"express": "^4.15.2", | ||
"mocha": "^3.2.0", | ||
"nyc": "^10.2.0" | ||
}, | ||
"dependencies": { | ||
"ajv": "^5.2.3" | ||
} | ||
"name": "express-json-validator-middleware", | ||
"version": "1.1.1", | ||
"description": "An Express middleware to validate requests against JSON Schemas", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/JouzaLoL/express-json-validator-middleware.git" | ||
}, | ||
"keywords": [ | ||
"express", | ||
"json", | ||
"validate", | ||
"validation", | ||
"validator" | ||
], | ||
"author": "Josef Vacek", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/JouzaLoL/express-json-validator-middleware/issues" | ||
}, | ||
"homepage": "https://github.com/JouzaLoL/express-json-validator-middleware#readme", | ||
"devDependencies": { | ||
"body-parser": "^1.17.1", | ||
"chai": "^3.5.0", | ||
"chai-http": "^3.0.0", | ||
"express": "^4.16.2", | ||
"mocha": "^3.5.3", | ||
"nyc": "^10.3.2" | ||
}, | ||
"dependencies": { | ||
"ajv": "^5.2.3" | ||
} | ||
} |
@@ -19,4 +19,4 @@ # express-json-validator-middleware | ||
- **Performance** - [ajv](https://github.com/epoberezkin/ajv) offers a significant performance boost over [JSONSchema](https://github.com/tdegrunt/jsonschema), | ||
- **Latest JSON Schema Standard** - [ajv](https://github.com/epoberezkin/ajv) supports JSON Schema v5 proposal. | ||
- **Active Maintenance** - ```express-json-validator-middleware``` is being actively maintained by @JouzaLoL | ||
- **Latest JSON Schema Standard** - [ajv](https://github.com/epoberezkin/ajv) supports JSON Schema v6 proposal. | ||
- **Active Maintenance** - `express-json-validator-middleware` is being actively maintained by @JouzaLoL | ||
@@ -38,3 +38,3 @@ ## Why validate with JSON schemas? | ||
```--save``` is no longer necessary as of ```npm@5``` | ||
`--save` is no longer necessary as of `npm@5` | ||
@@ -55,5 +55,5 @@ ## Getting started | ||
3. *Optional* - Define a shortcut function. Bind is necessary here in order to pass ```this``` correctly | ||
3. *Optional* - Define a shortcut function. | ||
```js | ||
var validate = validator.validate.bind(validator); | ||
var validate = validator.validate; | ||
``` | ||
@@ -102,6 +102,8 @@ | ||
var { Validator, ValidationError } = require('express-json-validator-middleware'); | ||
// Initialize a Validator instance first | ||
var validator = new Validator({allErrors: true}); // pass in options to the Ajv instance | ||
// Define a shortcut. It is perfectly okay to use validator.validate() as middleware, this just makes it easier | ||
var validate = validator.validate.bind(validator); | ||
var validate = validator.validate; | ||
@@ -111,3 +113,3 @@ // Define a JSON Schema | ||
type: 'object', | ||
required: ['number, name, type'], | ||
required: ['number', 'name', 'type'], | ||
properties: { | ||
@@ -157,3 +159,3 @@ number: { | ||
A valid request must now include a token URL query. Example valid URL: ```/street/?token=F42G5N5BGC``` | ||
A valid request must now include a token URL query. Example valid URL: `/street/?token=F42G5N5BGC` | ||
@@ -176,3 +178,3 @@ ## Custom keywords | ||
// free to use validate() here | ||
// free to validator in middleware now | ||
``` | ||
@@ -193,2 +195,3 @@ | ||
## Tests | ||
Tests are written using Mocha | ||
@@ -218,3 +221,3 @@ ``` | ||
}, | ||
required: ['foo'] <-- | ||
required: ['foo'] // <-- correct way | ||
} | ||
@@ -229,6 +232,6 @@ } | ||
type: 'string', | ||
required: true | ||
required: true // nono way | ||
} | ||
} | ||
} | ||
``` | ||
``` |
var Ajv = require('ajv'); | ||
class Validator { | ||
constructor(options) { | ||
this.ajv = new Ajv(options); | ||
} | ||
constructor(ajvOptions) { | ||
this.ajv = new Ajv(ajvOptions); | ||
this.validate = this.validate.bind(this); | ||
} | ||
/** | ||
@@ -13,25 +14,25 @@ * Express middleware for validating requests | ||
*/ | ||
validate(options) { | ||
var self = this; | ||
return function (req, res, next) { | ||
var validationErrors = {}; | ||
validate(options) { | ||
var self = this; | ||
return function (req, res, next) { | ||
var validationErrors = {}; | ||
Object.keys(options).forEach(function (requestProperty) { | ||
let schema = options[requestProperty]; | ||
let validateFunction = this.ajv.compile(schema); | ||
Object.keys(options).forEach(function (requestProperty) { | ||
let schema = options[requestProperty]; | ||
let validateFunction = this.ajv.compile(schema); | ||
var valid = validateFunction(req[requestProperty]); | ||
let valid = validateFunction(req[requestProperty]); | ||
if (!valid) { | ||
validationErrors[requestProperty] = validateFunction.errors; | ||
} | ||
}, self); | ||
if (!valid) { | ||
validationErrors[requestProperty] = validateFunction.errors; | ||
} | ||
}, self); | ||
if (Object.keys(validationErrors).length != 0) { | ||
next(new ValidationError(validationErrors)); | ||
} else { | ||
next(); | ||
} | ||
}; | ||
} | ||
if (Object.keys(validationErrors).length != 0) { | ||
next(new ValidationError(validationErrors)); | ||
} else { | ||
next(); | ||
} | ||
}; | ||
} | ||
} | ||
@@ -55,12 +56,12 @@ | ||
*/ | ||
constructor(validationErrors) { | ||
super(); | ||
this.name = 'JsonSchemaValidationError'; | ||
this.validationErrors = validationErrors; | ||
} | ||
constructor(validationErrors) { | ||
super(); | ||
this.name = 'JsonSchemaValidationError'; | ||
this.validationErrors = validationErrors; | ||
} | ||
}; | ||
module.exports = { | ||
Validator, | ||
ValidationError | ||
Validator, | ||
ValidationError | ||
}; |
@@ -20,3 +20,3 @@ const chai = require('chai'); | ||
}); | ||
var validate = validator.validate.bind(validator); | ||
var validate = validator.validate; | ||
@@ -23,0 +23,0 @@ var personSchema = { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6
213
228
15826
9