no-input

1.0.3 • Public • Published

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

Package Sidebar

Install

npm i no-input

Weekly Downloads

13

Version

1.0.3

License

MIT

Last publish

Collaborators

  • jackong