vtypes-only
v1.0.0
Published
Additional constraints for validate.js
Downloads
10
Maintainers
Readme
vtypes-only
"only" validation for validate.js
About
The only
validator attempts to make sure that when the field is filled,
it will be the only one that is filled amongst the other fields.
Installation
Using npm:
$ npm i --save vtypes-only
const validate = require('validate.js');
const only = require('vtypes-only');
// you can then proceed to register the required validators.
validate.validators.only = only;
Usage
validate({}, {attr: {only: true}});
// => undefined
validate({attr: {}}, {attr: {only: false}});
// => undefined
const value = {
attr: {
a: '',
b: 't2',
c: false
}
};
const constraints = {
attr: {
only: {
attributes: ['b', 'c']
}
}
};
validate(value, constraints);
// {attr: ['Attr must be the only key with value present. Other attributes (v2, v3) should not be present']}
For more examples, check out the test files in this package's source folder.
Available Options
| name | type | default | description |
| ----------- | ------- | -------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| truthy | boolean | false | Checks for truthy values instead of checking only for null
and undefined
values |
| message | string | must be the only key present. Other attributes (%{attributes}) should not be present
| Error message |
License
vtypes-only
is MIT licensed