validators-constructor
Constructor for library of validators
Note: This module works in browsers and Node.js >= 4.0
Installation
npm install validators-constructor
Usage
const validators = ; validators; validators;/* returns:{ message: 'is too long (maximum is 2)', error: 'lengthMax', arg: 2}*/ validators;/* returns (options which end in `Message` are not in the result):{ description: 'Make your number less', message: 'is too many', error: 'range.many', from: 1, to: 5}*/ validators;/* returns:{ message: 'can\'t be blank', error: 'required'}*/
Validator have to return error message if value is invalid and nothing in opposite case.
You can handle validation result in resultHandler
. It is useful for third party validators
const validatorJS = ; //https://github.com/chriso/validator.js//These validators return true if valid, false in opposite case and can throw exception validators; validators;/* returns:{ message: 'abc is not isEmail', error: 'isEmail'}*/ validators; //catch exception by default/* returns:{ message: 'This library (validator.js) validates strings only', error: 'isEmail'}*/
API
Validators([params])
- params (
Object
)- arg (
String
) - name of argument for compared values. By default:arg
- simpleArgsFormat (
Boolean
) - any non object argument will be transformed to the{arg: <argument>}
- oneOptionsArg (
Boolean
) - ignore second options argumentvalidator(value, argOrOptions, ignoredOptions)
- resultHandler (
Function
) - handler of validation result. By defaultfunction(result) { return result }
- exceptionHandler (
Function
) - handler of JS exceptions. By default:null
, E.g.function(err) { return err }
will return error message in standard format - formatStr (
Function
) - Custom template parser. params:templateStr
,variablesObj
. returns:str
- errorFormat (
Object
) - Output format of error. By default:
- arg (
error: '%{validator}' message: '%{message}' $options: true $origin: true
Default formatStr function allows to use %{template} syntax. Next variables are enabled:
validator
- validator name (e.g. maxLength);
message
- string that is returned in case of error;
options which you set in validator options if $options: true
;
options which validator returns instead string (except options that end in Message) if $origin: true
- return (
Validators
) new instance of Validators
const validators = errorFormat: '%{message}'
--
validators.add(validatorName, validator, [params]) or (validators, [params])
-
validatorName (
String
) - Name of validator in validators instance -
validators (
Object
) - Object has structure{validatorName: validator, ...}
-
validator (
Function
orString
orArray
) - Validator or alias or validators array (e.g. ['validatorName', ['validatorName', {...options}], validatorFn]) -
params (
Object
) validator params (see Validators params). Also you can set default 'message' in params -
return (
Validators
) instance of Validators
validators validators
--
validator(value, [arg], [options])
-
value (
Any
) - Validated value -
arg (
Any
) - Value for comparison. Have to exist and can not be an object or boolean. User can set it asoptions.arg
. If you use 'arg' in your validator you must be sure that user will specify this value -
options (
Object
) - Options- arg (
Any
) - Will be set if 'arg' is specified - message (
Any
) - Override error message - parse (
Function
) - Can change input value before validation - (
Any
) - Any custom options
- arg (
-
(
Any
) - Any custom arguments -
return (
Any
) -undefined
if valid or error message. You can use %{template} syntax in message strings (validated value is enabled asvalue
, compared value - ascomparedValue
). Also you can return promise with result of validation
validators; //'Should be less or equal 5'
--
validator.curry([arg], [options])
-
arg, options, etc. - see validator.
-
return (
Function
) - function, which gets value and returns result of validation
lessThen3 = validatorsmaxLength; //Length should be less then 3
Tests
npm installnpm test