Skip to content

Jackong/no-input

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

no-input

A useful tool to validate any input.

NPM version build status David deps node version Gittip

Install

NPM

Usage

  • Options
input(data, nameOrOptions[, pattern, default, error]);
  • data {Object} required input data you want to validate
  • nameOrOptions {String|Object} required key may be exist in data OR options with name, pattern, default and error
  • pattern {RegExp|Array|Function|Object|String...} optional validator
  • default {Mixed} optional default value when invalid (Function will get the return value)
  • error {Error} optional special error to throw
  • throw {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.
// ...

Licences

MIT

About

A useful tool to validate any input.

Resources

License

Stars

Watchers

Forks

Packages

No packages published