validation-schema
v2.0.1
Published
A schema-based validation middleware for JSON requests
Downloads
12
Readme
Validation Schema
About
validation-schema
is a Mongoose inspired express middleware with the purpose of validating and sanitizing JSON requests. It supports custom validate/sanitize with asynchronous capabilities.
Note: validation-schema
is still early in development and has many planned features, along with optimization improvements.
Install
npm install validation-schema
Example
A quick example of how validation-schema
could be used. View the documentation.
const validationSchema = require('validation-schema')
app.post('/', validationSchema({
username: {
type: 'string', // Data type check
required: [true, 'A username is required'],
minlength: [5, 'Username must be at least 5 characters'],
maxlength: [25, 'Username must be no more than 25 characters']
},
password: { // can use single values for a default error message
type: 'string',
required: true,
minlength: 5
}
}), (req, res, next) => {
// Detailed report of all errors
// Shows error location, error type e.g 'maxlength' and error messages.
console.log(req.valid.errors)
// Nicely formatted array of error messages
console.log(req.valid.errorMessages)
// Validated and sanitized values
console.log(req.valid.values)
})