no-input
v1.0.3
Published
A useful tool to validate any input.
Downloads
59
Maintainers
Readme
no-input
A useful tool to validate any input.
Install
Usage
- Options
input(data, nameOrOptions[, pattern, default, error]);
data
{Object} required input data you want to validatenameOrOptions
{String|Object} required key may be exist indata
OR options withname
,pattern
,default
anderror
pattern
{RegExp|Array|Function|Object|String...} optional validatordefault
{Mixed} optional default value when invalid (Function will get the return value)error
{Error} optional special error to throwthrow
{Error} if the value is invalid without default value
- Custom error handler
input.error = function(name) {
return new input.InvalidInputError('Invalid input ' + name);
}
- Example
var data = {type: 'cat'};
var value = input(data, 'type', /^(cat|dog)$/, 'pig', new Error('invalid type'));
//OR
var value = input(data, {name: 'type', pattern: ['cat', 'dog'], default: 'pig', error: new Error('invalid type')]};
// The following code will not be executed, if the value is invalid.
// ...