vtypes-requiredwithout

1.0.0 • Public • Published

vtypes-requiredwithout

"Required Without" validator for validate.js

npm package vtypes

About

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

The field under validation must be present and not empty only when any of the other specified fields are not present.

Installation

Using npm:

$ npm i --save vtypes-requiredwithout
const validate = require('validate.js');
const requiredWithout = require('vtypes-requiredwithout');
 
// you can then proceed to register the required validators.
validate.validators.requiredWithout = requiredWithout;

Usage

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

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

Available Options

name type default description
attributes array The list of attributes
message string is required when any of these attributes (%{attributes}) are not present Error message
truthy boolean false Checks for truthy values instead of checking only for null and undefined values

License

vtypes-requiredwithout is MIT licensed

Package Sidebar

Install

npm i vtypes-requiredwithout

Weekly Downloads

2

Version

1.0.0

License

MIT

Last publish

Collaborators

  • geraldyeo