map-validator
An object validator based on node-validator. Heavily inspired by express-validator.
Installation
npm install map-validator --save
Usage
var MapValidator = ; // our key-value data objectvar postBody = name: 'John Doe' email: 'John.Doe@Example.com' // more data here...; // create validator for our objectvar validator = postBody; // validate object keysvalidator;validator;validator; validator;validator; var errors = validator;iferrors throw errors; console;
Which will result:
{ name: 'John Doe', email: 'john.doe@example.com' }
API
instance.verify
function(mappedErrors)
Return all the errors or null
.
The mappedErrors
option can be used to return the errors as key-value map or
as an array (default). On mappedErrors
mode, if two or more errors occur on the
same key, the first error will be chosen.
errors:
key: "email" error: "required" value: "<received input>" key: "email" error: "valid email required" value: "<received input>" key: "password" error: "6 to 20 characters required" value: "<received input>"
mappedErrors:
email: key: "email" error: "required" value: "<received input>" password: key: "password" error: "6 to 20 characters required" value: "<received input>"
MapValidator.extend
function(name, fn)
Used to add your own validators:
MapValidator;
MapValidator.sanitizerExtend
function(name, fn)
Used to add your own sanitizers:
MapValidator;
License
Copyright © 2015 Moshe Simantov ms@development.co.il, MIT License