1.0.0 • Published 7 years ago
vtypes-requiredwithoutall v1.0.0
vtypes-requiredwithoutall
"Required Without All" validator for validate.js
About
The requiredWithoutAll
validator attempts to ensure that the input
is be present and not empty if ALL 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-requiredwithoutall
const validate = require('validate.js');
const requiredWithoutAll = require('vtypes-requiredwithoutall');
// you can then proceed to register the required validators.
validate.validators.requiredWithoutAll = requiredWithoutAll;
Usage
const constraint = {
attr: {
requiredWithoutAll: {attributes: ['other', 'related']}
}
}
validate({}, constraint);
// => {attr: ['Attr is required when all of these attributes (other, related) are not present']}
validate({other: 'bar', related: 'baz'}, constraint);
// => undefined
validate({other: 'bar'}, constraint);
// => undefined
validate({attr: 'foo'}, 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-requiredwithoutall
is MIT licensed
1.0.0
7 years ago