vtypes-requiredunless
"Required Unless" validator for validate.js
About
The requiredUnless
validator attempts to ensure that the input is present
and not empty UNLESS another field is present / equal to any predefined value.
Installation
Using npm:
$ npm i --save vtypes-requiredunless
const validate = ;const requiredUnless = ; // you can then proceed to register the required validators.validatevalidatorsrequiredUnless = requiredUnless;
Usage
const constraint1 = attr: requiredUnless: attribute: 'other' ;// => {attr: ['Attr is required when other is present and equal to *']} ;// => {attr: ['Attr is required when other is present and equal to *']} ;// => undefined const constraints2 = attr: requiredUnless: attribute: 'other' attributeValue: 'bar2' ;// => undefined ;// => {attr: ['Attr is required when other is present and equal to *']}
For more examples, check out the test files in this package's source folder.
Available Options
name | type | default | description |
---|---|---|---|
attribute | string | The attribute key that you want to check | |
attributeValue | string | When set, the value of the attribute should equal to this | |
comparator | function | custom comparison method. In the event target value is of a complex type | |
message | string | is required when %{attribute} is present |
Error message |
symbolForAny | string | * | symbol to use when no attributeValue is defined |
truthy | boolean | false | Checks for truthy values instead of checking only for null and undefined values |
License
vtypes-requiredunless
is MIT licensed