UNPKG

valida

Version:

Valida - A lightweight sanitizer and validator library for Node.js

46 lines (33 loc) 1.05 kB
var Valida = require('..'); var schema = { age: [ { sanitizer: Valida.Sanitizer.toInt }, { validator: Valida.Validator.required, groups: ['create'] } ], name: [ { validator: Valida.Validator.required, groups: ['update'] } ] }; var person = { age: '10' }; Valida.process(person, schema, function(err, ctx) { if (err) return console.log(err); if (!ctx.isValid()) return console.log(ctx.errors()); console.log('valid', person); }, ['create', 'update']); Valida.process(person, schema, function(err, ctx) { if (err) return console.log(err); if (!ctx.isValid()) return console.log(ctx.errors()); console.log('valid', person); }, 'update'); Valida.process(person, schema, function(err, ctx) { if (err) return console.log(err); if (!ctx.isValid()) return console.log(ctx.errors()); console.log('valid', person); }, 'create'); Valida.process(person, schema, function(err, ctx) { if (err) return console.log(err); if (!ctx.isValid()) return console.log(ctx.errors()); console.log('valid', person); });