express-json-validator-middleware
Advanced tools
Comparing version 1.1.1 to 1.2.0
{ | ||
"name": "express-json-validator-middleware", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "An Express middleware to validate requests against JSON Schemas", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -10,2 +10,4 @@ # express-json-validator-middleware | ||
<hr> | ||
Coming from `express-jsonschema`? Read our [migration notes](#migrating) | ||
@@ -15,9 +17,7 @@ | ||
Based heavily on https://github.com/trainiac/express-jsonschema. A big thank you to @trainiac for the original package! | ||
## Why use this library over [express-jsonschema](https://github.com/trainiac/express-jsonschema) ? | ||
- **Performance** - [ajv](https://github.com/epoberezkin/ajv) offers a significant performance boost over [JSONSchema](https://github.com/tdegrunt/jsonschema), | ||
- **Performance** - [ajv](https://github.com/epoberezkin/ajv) offers a [significant performance boost over](https://github.com/ebdrup/json-schema-benchmark/blob/master/README.md#performance) JSONSchema. | ||
- **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 | ||
- **Active Maintenance** - `express-json-validator-middleware` is being actively maintained. | ||
@@ -135,3 +135,3 @@ ## Why validate with JSON schemas? | ||
Sometimes your route may depend on the `body` and `query` both having a specific format. In this example I use `body` and `query` but you can choose to validate any `request` properties you'd like. | ||
Sometimes your route may depend on the `body` and `query` both having a specific format. In this example we use `body` and `query` but you can choose to validate any `request` properties you like. | ||
@@ -144,3 +144,3 @@ ```js | ||
token: { // validate token | ||
type: 'string', | ||
type: 'string', | ||
format: 'alphanumeric', | ||
@@ -176,3 +176,3 @@ minLength: 10, | ||
// free to validator in middleware now | ||
// route handlers with validate() | ||
``` | ||
@@ -193,3 +193,3 @@ | ||
## Tests | ||
Tests are written using Mocha | ||
Tests are written using Mocha & Chai | ||
@@ -211,3 +211,2 @@ ``` | ||
```js | ||
// CORRECT | ||
@@ -235,1 +234,9 @@ { | ||
``` | ||
## Credits | ||
- Maintained by [@JouzaLoL](https://github.com/jouzalol) | ||
- [Original Module](https://github.com/trainiac/express-jsonschema) by [@trainiac](https://github.com/trainiac) | ||
- PRs: | ||
- [@GochoMugo](https://github.com/GochoMugo) | ||
- [@teobaranga](https://github.com/teobaranga) |
var Ajv = require('ajv'); | ||
/** | ||
* Express middleware for validating requests | ||
* | ||
* @class Validator | ||
*/ | ||
class Validator { | ||
@@ -9,22 +14,26 @@ constructor(ajvOptions) { | ||
/** | ||
* Express middleware for validating requests | ||
* | ||
* @param {Object} options | ||
* @returns | ||
* Validator method to be used as middleware | ||
* | ||
* @param {Object} options Options in format { request_property: schema } | ||
* @returns | ||
*/ | ||
validate(options) { | ||
// Self is a reference to the current Validator instance | ||
var self = this; | ||
const validateFunctions = Object.keys(options).map(function (requestProperty) { | ||
let schema = options[requestProperty]; | ||
let validateFunction = this.ajv.compile(schema); | ||
return [requestProperty, validateFunction]; | ||
}, self); | ||
// The actual middleware function | ||
return function (req, res, next) { | ||
var validationErrors = {}; | ||
Object.keys(options).forEach(function (requestProperty) { | ||
let schema = options[requestProperty]; | ||
let validateFunction = this.ajv.compile(schema); | ||
let valid = validateFunction(req[requestProperty]); | ||
for (const [requestProperty, validateFunction] of validateFunctions) { | ||
const valid = validateFunction(req[requestProperty]); | ||
if (!valid) { | ||
validationErrors[requestProperty] = validateFunction.errors; | ||
} | ||
}, self); | ||
} | ||
@@ -40,7 +49,5 @@ if (Object.keys(validationErrors).length != 0) { | ||
/** | ||
* Validation Error | ||
* | ||
* | ||
* @class ValidationError | ||
@@ -50,9 +57,2 @@ * @extends {Error} | ||
class ValidationError extends Error { | ||
/** | ||
* Creates an instance of ValidationError. | ||
* @param {any} validationErrors | ||
* | ||
* @memberOf ValidationError | ||
*/ | ||
constructor(validationErrors) { | ||
@@ -63,3 +63,3 @@ super(); | ||
} | ||
}; | ||
} | ||
@@ -66,0 +66,0 @@ module.exports = { |
@@ -5,4 +5,3 @@ const chai = require('chai'); | ||
const { | ||
Validator, | ||
ValidationError | ||
Validator | ||
} = require('../src'); | ||
@@ -48,3 +47,4 @@ | ||
name: 10 | ||
} | ||
}; | ||
chai.request(app) | ||
@@ -51,0 +51,0 @@ .post('/street') |
const expect = require('chai').expect; | ||
const { | ||
Validator, | ||
ValidationError | ||
ValidationError | ||
} = require('../src'); | ||
describe('Simulated Middleware', () => { | ||
describe('Basic Use Case', () => { | ||
const middleware = new Validator().validate({ | ||
body: { | ||
type: 'array' | ||
} | ||
}); | ||
describe('Basic Use Case', () => { | ||
const middleware = new Validator().validate({ | ||
body: { | ||
properties: { | ||
name: { | ||
type: 'string', | ||
} | ||
}, | ||
required: ['name'] | ||
} | ||
}); | ||
it('should throw ValidationError on bad data', () => { | ||
expect(() => middleware({ | ||
body: {} | ||
}, {}, function next(err) { | ||
throw err; | ||
})).to.throw(ValidationError); | ||
}); | ||
it('should throw ValidationError on bad data', () => { | ||
expect(() => middleware({ | ||
body: {} | ||
}, {}, function next(err) { | ||
throw err; | ||
})).to.throw(ValidationError); | ||
}); | ||
it('should call next() on good data', () => { | ||
let nextCalled = false; | ||
expect(() => middleware({ | ||
body: { | ||
name: 'nikolay' | ||
} | ||
}, {}, function next() { | ||
nextCalled = true | ||
})).not.to.throw(); | ||
expect(nextCalled).to.be.true; | ||
}); | ||
}); | ||
it('should call next() on good data', () => { | ||
let nextCalled = false; | ||
expect(() => middleware({ | ||
body: { | ||
name: 'nikolay' | ||
} | ||
}, {}, function next() { | ||
nextCalled = true; | ||
})).not.to.throw(); | ||
expect(nextCalled).to.be.true; | ||
}); | ||
}); | ||
}); |
const expect = require('chai').expect; | ||
const { | ||
Validator, | ||
ValidationError | ||
Validator | ||
} = require('../src'); | ||
describe('Validator', () => { | ||
describe('Basic Validation', () => { | ||
const SCHEMA = { | ||
properties: { | ||
name: { | ||
type: 'string', | ||
maxLength: 8 | ||
}, | ||
info: { | ||
type: 'object', | ||
properties: { | ||
age: { | ||
type: 'integer', | ||
maximum: 100 | ||
}, | ||
name: { | ||
type: 'object', | ||
properties: { | ||
first: { | ||
type: 'string' | ||
}, | ||
last: { | ||
type: 'string' | ||
} | ||
}, | ||
required: ['first', 'last'] | ||
} | ||
}, | ||
required: ['age'] | ||
} | ||
}, | ||
required: ['name'] | ||
}; | ||
describe('Basic Validation', () => { | ||
const SCHEMA = { | ||
properties: { | ||
name: { | ||
type: 'string', | ||
maxLength: 8 | ||
}, | ||
info: { | ||
type: 'object', | ||
properties: { | ||
age: { | ||
type: 'integer', | ||
maximum: 100 | ||
}, | ||
name: { | ||
type: 'object', | ||
properties: { | ||
first: { | ||
type: 'string' | ||
}, | ||
last: { | ||
type: 'string' | ||
} | ||
}, | ||
required: ['first', 'last'] | ||
} | ||
}, | ||
required: ['age'] | ||
} | ||
}, | ||
required: ['name'] | ||
}; | ||
const BAD_DATA = { | ||
name: 'Super Nikolay', | ||
info: { | ||
age: 666, | ||
name: { | ||
last: false | ||
} | ||
const BAD_DATA = { | ||
name: 'Super Nikolay', | ||
info: { | ||
age: 666, | ||
name: { | ||
last: false | ||
} | ||
} | ||
}; | ||
const GOOD_DATA = { | ||
name: 'Nikolay' | ||
}; | ||
} | ||
}; | ||
const GOOD_DATA = { | ||
name: 'Nikolay' | ||
}; | ||
const validate = new Validator().ajv.compile(SCHEMA); | ||
const validate = new Validator().ajv.compile(SCHEMA); | ||
it('Rejects bad data', () => { | ||
let validated = validate(BAD_DATA); | ||
expect(validated).to.be.false; | ||
expect(validate.errors).to.have.length; | ||
}); | ||
it('Rejects bad data', () => { | ||
let validated = validate(BAD_DATA); | ||
expect(validated).to.be.false; | ||
expect(validate.errors).to.have.length; | ||
}); | ||
it('Approves good data', () => { | ||
let validated = validate(GOOD_DATA); | ||
expect(validated).to.be.true; | ||
}); | ||
}); | ||
it('Approves good data', () => { | ||
let validated = validate(GOOD_DATA); | ||
expect(validated).to.be.true; | ||
}); | ||
}); | ||
}); |
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
15950
10
251
234