1.0.0 • Published 7 years ago

vtypes-requiredwithall v1.0.0

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

vtypes-requiredwithall

"Required With All" validator for validate.js

npm package vtypes

About

The requiredWithAll validator attempts to ensure that the input is be present and not empty if ALL of the other specified fields are present.

Installation

Using npm:

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

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

Usage

const constraint = {
  attr: {
    requiredWithAll: {attributes: ['other', 'related']}
  }
}

validate({}, constraint);
// => undefined

validate({attr: 'foo', other: 'bar', related: 'baz'}, constraint);
// => undefined

validate({other: 'bar'}, constraint);
// => undefined

validate({other: 'bar', related: 'baz'}, constraint);
// => {attr: ['Attr is required when any of these attributes (other, related) are present']}

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

Available Options

nametypedefaultdescription
attributesarrayThe list of attributes
messagestringis required when all of these attributes (%{attributes}) are presentError message
truthybooleanfalseChecks for truthy values instead of checking only for null and undefined values

License

vtypes-requiredwithall is MIT licensed