1.0.0 • Published 7 years ago

vtypes-arrayof v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

vtypes-arrayof

"arrayOf" validation for validate.js

npm package vtypes

About

The arrayOf validator attempts to ensure that all values in the array conforms to a predefined rule.

Installation

Using npm:

$ npm i --save vtypes-arrayof
const validate = require('validate.js');
const arrayOf = require('vtypes-arrayOf');

// you can then proceed to register the required validators.
validate.validators.arrayOf = arrayOf;

Usage

validate({}, {attr: {arrayOf: true}});
// => undefined

validate({attr: [], {attr: {arrayOf: false}});
// => undefined

validate({attr: 'foo'}, {attr: {arrayOf: true}});
// => {attr: {_message: 'Attr is not of type array'}}
const value = {
  attr: ['', 't2', 't31']
};

const constraints = {
  attr: {
    arrayOf: {
      contains: { presence: true, length: {is: 3} }
    }
  }
};

validate(value, constraints);
// {
//   attr: {
//     '0': [
//       'Attr[0] can\'t be blank',
//       'Attr[0] is the wrong length (should be 3 characters)'
//     ],
//     '1': [
//       'Attr[1] is the wrong length (should be 3 characters)'
//     ],
//     _message: '^One or more array values for attr is not valid'
//   }
// };

For more examples, check out the test files in this package's source folder.

Available Options

nametypedefaultdescription
formatterfunction(v) => vAllows processing of errors before it returns
messagestring^One or more array values for %{key} is not validError message
messageKeystring_messagekey in return object for the summary message
notArraystring%{key} is not of type arrayError when value is not an object

License

vtypes-arrayof is MIT licensed