Specla Validator
Validate an object against a schema.
Install
npm install --save specla-validator
Usage
const Validator = require('specla-validator')
const data = {
name: 'Frederik',
age: 23,
skills: ['js'],
}
const schema = {
name: String,
age: Number,
skills: Array
}
const validator = new Validator(data, schema)
// check for errors
console.log(validator.errors)
Custom rules
Create a custom rule with the Validator.Rule
class.
const rule = new Validator.Rule({
name: 'my-rule',
validate: value => {
// Do something to validate the value
// return true if valid and false if not
}
})
const schema = {
name: rule
}
Rules
The validator exposes some rules for easier schema creation.
const { string, number, array, boolean, object } = Validator
const Schema = {
name: string({ required: true, min: 3, max: 40 }),
age: number({ required: true, min: 0 }),
skills: array({ min: 0, max: 10 })
}