vtypes-different

1.0.0 • Public • Published

vtypes-different

"different" validation for validate.js

npm package vtypes

About

The different validator is the opposite of equality validator that is provided by validate.js It can be used to verify that one attribute is always different to another.

By default !== is used to check the quality, if you need to validate more complex objects you can give a function using the comparator option which should be a function that accepts two arguments and returns true if they objects are different and false if they are equal.

Installation

Using npm:

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

Usage

validate({}, {attr: {different: true}});
// => undefined
 
validate({attr: 'foo'}, {attr: {different: true}});
// => {attr: ["Attr must be of type differentean"]}
 
validate({attr: true, {attr: {different: true}});
// => undefined
 
validate({attr: false, {attr: {different: true}});
// => undefined
 
var constraints = {
  password: {
    different: "prevPassword"
  }
};
 
validate({password: "foo", prevPassword: "bar"}, constraints);
// => undefined
 
validate({password: "foo", prevPassword: "foo"}, constraints);
// => {password: ["Password is not different from prev password"]}

For more examples, check out the test files in this package's source folder.

Available Options

name type default description
message string is not different from %{attribute} Error message

License

vtypes-different is MIT licensed

Package Sidebar

Install

npm i vtypes-different

Weekly Downloads

0

Version

1.0.0

License

MIT

Last publish

Collaborators

  • geraldyeo