@sapphirejs/validator
v0.0.19
Published
Schema and Middleware Validator for Sapphire Framework
Downloads
2
Readme
Validator
A very thin wrapper on top of the fantastic Joi, mostly to provide simpler error handling and validator as middleware. If you know how to use Joi, than you'll feel right at home. Even if you don't, you're minutes away at pumping some awesome validation action.
Installation
$ npm install --save @sapphirejs/validator
Schema
Schemas are plain object literals with the field name as the key and a set of chained functions to declare validation rules. Let's see an example:
const { rule, Validator } = require('@sapphirejs/validator')
let validator = new Validator({
name: 'John',
email: '[email protected]',
age: 27
}, {
name: rule.string().alphanum().required(),
email: rule.string().email().required(),
age: rule.number().min(18).max(200)
})
We've just input some data and a schema declaration for each of those inputs. Just remember that each validation should start with rule
and move on with the chain of functions. Read more at the Joi API Documentation for the available data types and validation functions.
Now that we've run the validator, it exposes some properties to check if it failed or not:
validator.passes // true | false
validator.fails // true | false
Errors are returned in an object literal, so you can further process it or dump it directly as JSON output:
validator.errors
/*
ie: {
name: ['"name" is required'],
email: ['"email" is required', '"email" is not a valid email'],
}
*/
Middleware
Probably more interesting and streamlined than manually validating every request, middlewares can be injected into routes (or globally if that's what you need) and automate validation. They are very simple classes that declare the validation rules, while the rest is handled automatically.
Let's imagine we have a POST /users
route where we send a body of parameters to create a new user.
app.post('/users', (req, res) => { /* handle user creation */ })
Next we build a UserValidator
class that defines the validation rules:
const { ValidatorMiddleware } = require('sapphire-validator')
class UserValidator extends ValidatorMiddleware {
rules() {
return {
name: 'John',
email: '[email protected]',
age: 27
}
}
}
And that's basically it. If we inject that class as a middleware, the request's body will be validated against those rules and a validator
instance passed in the request object.
const UserValidator = require('./validators/uservalidator')
app.post('/users', new UserValidator().middleware, (req, res) => {
if (req.validator.fails)
res.status(422).json(req.validator.errors)
/* handle user creation */
})
By default, the middleware will read req.body
and req.query
as input sources. If you need more control, like including req.params
or cookies, you can always define your own fields()
function inside a middleware class.
const { ValidatorMiddleware } = require('sapphire-validator')
class UserValidator extends ValidatorMiddleware {
fields(req) {
return {...req.body, ...req.params}
}
/* rules omited for brevity */
}
Or even manually define what fields you need:
const { ValidatorMiddleware } = require('sapphire-validator')
class UserValidator extends ValidatorMiddleware {
fields(req) {
return {
name: req.body.name,
age: req.query.age
}
}
/* rules omited for brevity */
}