vtypes-arrayof
v1.0.0
Published
Additional constraints for validate.js
Downloads
12
Maintainers
Readme
vtypes-arrayof
"arrayOf" validation for validate.js
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
| name | type | default | description |
| ---------- | -------- | --------------------------------------------------- | --------------------------------------------- |
| formatter | function | (v) => v
| Allows processing of errors before it returns |
| message | string | ^One or more array values for %{key} is not valid
| Error message |
| messageKey | string | _message
| key in return object for the summary message |
| notArray | string | %{key} is not of type array
| Error when value is not an object |
License
vtypes-arrayof
is MIT licensed