1.0.0 • Published 7 years ago

vtypes-objectof v1.0.0

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

vtypes-objectof

"objectOf" validation for validate.js

npm package vtypes

About

The objectOf validator attempts to make sure that the value of all keys in an object conforms to the predefined format.

Installation

Using npm:

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

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

Usage

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

validate({attr: {}}, {attr: {objectof: false}});
// => undefined
const value = {
  attr: {
    a: '',
    b: 't2'
  }
};

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

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

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 object values for %{key} is not validError message
messageKeystring_messagekey in return object for the summary message
notObjectstring%{key} is not of type objectError when value is not an object

License

vtypes-objectof is MIT licensed