express-json-validator-middleware
Advanced tools
Comparing version 1.2.1 to 1.2.2
{ | ||
"name": "express-json-validator-middleware", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "An Express middleware to validate requests against JSON Schemas", | ||
@@ -8,3 +8,3 @@ "main": "src/index.js", | ||
"test": "mocha", | ||
"lint": "eslint */*.js --fix", | ||
"lint": "eslint **/*.js --fix", | ||
"precommit": "npm run lint && npm run test", | ||
@@ -11,0 +11,0 @@ "prepush": "npm run lint && npm run test", |
@@ -41,3 +41,2 @@ # express-json-validator-middleware | ||
0. Install the module | ||
1. Require the module | ||
@@ -54,3 +53,3 @@ ```js | ||
3. *Optional* - Define a shortcut function. | ||
3. *Optional* - Define a bound shortcut function that can be used instead of Validator.validate | ||
```js | ||
@@ -60,13 +59,13 @@ var validate = validator.validate; | ||
4. Use the function as an Express middleware, passing in an options object of the following format: | ||
4. Use the Validator.validate method as an Express middleware, passing in an options object of the following format: | ||
```js | ||
validate({ | ||
request_property: schema_to_use | ||
Validator.validate({ | ||
requestProperty: schemaToUse | ||
}) | ||
``` | ||
Example: Validate req.body against BodySchema | ||
Example: Validate `req.body` against `bodySchema` | ||
```js | ||
app.post('/street/', validate({body: BodySchema}), function(req, res) { | ||
app.post('/street/', validate({body: bodySchema}), function(req, res) { | ||
// route code | ||
@@ -106,3 +105,3 @@ }); | ||
// Define a shortcut. It is perfectly okay to use validator.validate() as middleware, this just makes it easier | ||
// Define a shortcut function | ||
var validate = validator.validate; | ||
@@ -153,5 +152,6 @@ | ||
app.post('/street/', validate({body: StreetSchema, query: TokenSchema}), function(req, res) { | ||
app.post('/street/', Validator.validate({body: StreetSchema, query: TokenSchema}), function(req, res) { | ||
// application code | ||
}); | ||
``` | ||
@@ -161,2 +161,31 @@ | ||
## Using dynamic schema | ||
Instead of passing in a schema object you can also pass in a function that will return a schema. It is | ||
useful if you need to generate or alter the schema based on the request object. | ||
Example: loading schema from the database | ||
// Middleware executed before validate function | ||
```js | ||
function loadSchema(req, res, next) { | ||
getSchemaFromDB() | ||
.then((schema) => { | ||
req.schema = schema; | ||
next(); | ||
}) | ||
.catch(next); | ||
} | ||
// function that returns a schema object | ||
function getSchema(req) { | ||
// return the schema from the previous middleware or the default schema | ||
return req.schema || DefaultSchema; | ||
} | ||
app.post('/street/', loadSchema, Validator.validate({body: getSchema}), function(req, res) { | ||
// route code | ||
}); | ||
``` | ||
## Custom keywords | ||
@@ -217,5 +246,5 @@ | ||
type: 'string' | ||
}, | ||
required: ['foo'] // <-- correct way | ||
} | ||
} | ||
}, | ||
required: ['foo'] // <-- correct way | ||
} | ||
@@ -239,4 +268,2 @@ | ||
- [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) | ||
- PRs: see Contributors |
@@ -22,13 +22,25 @@ var Ajv = require('ajv'); | ||
var self = this; | ||
// Cache validate functions | ||
const validateFunctions = Object.keys(options).map(function (requestProperty) { | ||
let schema = options[requestProperty]; | ||
let validateFunction = this.ajv.compile(schema); | ||
return [requestProperty, validateFunction]; | ||
}, self); | ||
const schema = options[requestProperty]; | ||
if (typeof schema === 'function') { | ||
return {requestProperty, schemaFunction: schema}; | ||
} | ||
const validateFunction = this.ajv.compile(schema); | ||
return {requestProperty, validateFunction}; | ||
}, self); | ||
// The actual middleware function | ||
return function (req, res, next) { | ||
return (req, res, next) => { | ||
var validationErrors = {}; | ||
for (const [requestProperty, validateFunction] of validateFunctions) { | ||
for (let {requestProperty, validateFunction, schemaFunction} of validateFunctions) { | ||
if (!validateFunction) { | ||
// Get the schema from the dynamic schema function | ||
const schema = schemaFunction(req); | ||
validateFunction = this.ajv.compile(schema); | ||
} | ||
// Test if property is valid | ||
const valid = validateFunction(req[requestProperty]); | ||
@@ -35,0 +47,0 @@ if (!valid) { |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
262
0
12534
4
68