vtypes-requiredwith

1.0.0 • Public • Published

vtypes-requiredwith

"Required With" validator for validate.js

npm package vtypes

About

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

Installation

Using npm:

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

Usage

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

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 present Error message
truthy boolean false Checks for truthy values instead of checking only for null and undefined values

License

vtypes-requiredwith is MIT licensed

Package Sidebar

Install

npm i vtypes-requiredwith

Weekly Downloads

2

Version

1.0.0

License

MIT

Last publish

Collaborators

  • geraldyeo