1.0.0 • Published 7 years ago

vtypes-only v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

vtypes-only

"only" validation for validate.js

npm package vtypes

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

nametypedefaultdescription
truthybooleanfalseChecks for truthy values instead of checking only for null and undefined values
messagestringmust be the only key present. Other attributes (%{attributes}) should not be presentError message

License

vtypes-only is MIT licensed