Validates express params, query, and body.
npm install validate-express-schema
npm install --save validate-express-schema
`
Uses the is-my-json-valid package. It also uses the additional formats from the is-my-schema-valid package.
Usage
`javascript
const validate = require(validate-express-schema);
const schema = {
params: {
type: 'object',
required: true,
properties: {
userId: {
type: 'integer',
required: true,
format: 'uuid'
}
}
},
body: {
type: 'object',
required: true,
properties: {
username: {
type: 'string',
required: true
}
}
}
};
app.put('/users:userId', validate(schema), (req, res) => {
...
});
`
$3
You can simply specify the properties in the schema object:
`javascript
const equivalentSchema = {
params: {
userId: {
type: 'integer',
required: true,
format: 'uuid'
}
},
body: {
username: {
type: 'string',
required: true
}
}
};
`
If the schema does not have a type property, then the schema is created from the object keys.
API
The validate-express-schema package exports one middleware generator functions and one exception object:
`
validate(schema [, options])
ValidationError
`
Calling the middleware generator functions returns a middleware function that validates the request params, query, and body objects. Validation errors result in next(ValidationError) being called. The ValidationError object has an errors property that lists the validation errors.
The options` are as specified in the is-my-json-valid package.