arg-validator.js
A simpler way to do argument validations on functions.
Install
npm install arg-validator --save
Example
var argValidator = ; // required: name, age// optional: address{ var arg = ; ; ; arg; arg; // or access the errors by arg.errors}
Compare with the code without arg-validation.js
{ ifsome regexp validation of the url throw "url must be an url"; ifcheck if action is in 'GET' 'POST' 'PUT' 'DELETE' throw "action must be in GET, POST, PUT, DELETE"; ifparams iftypeof params != 'object' throw "params must be an object"; }
And you get all the validation errors
; // ArgValidator: url is not an url. action is not // in [GET, POST, PUT, DELETE]. params is not an object.
Asynchronous Example
var argValidator = ; // required: name, age// optional: address{ var arg = ; ; ; ; ifarg return;} ;
Custom Validation
{ var arg = ; ifworld !== 'world' arg arg;} // Or you can add a custom validation rule byargValidator { var arg = ; ; arg;}
List of Validations
- isExist // arg('a', a) is a shortcut to arg('a', a).isExist()
- isString
- isNumber
- isBoolean
- isFunction
- isObject
- isArray
- isURL
- isStringIn(values...)
- hasProperty(requireProtocol)
License
MIT