vtypes-objectof

1.0.0 • Public • Published

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

name type default description
formatter function (v) => v Allows processing of errors before it returns
message string ^One or more object values for %{key} is not valid Error message
messageKey string _message key in return object for the summary message
notObject string %{key} is not of type object Error when value is not an object

License

vtypes-objectof is MIT licensed

Package Sidebar

Install

npm i vtypes-objectof

Weekly Downloads

2

Version

1.0.0

License

MIT

Last publish

Collaborators

  • geraldyeo