vtypes-invariant

1.0.0 • Public • Published

vtypes-invariant

invariant-style validation for validate.js

npm package vtypes

About

The invariant validator allows you to define a condition/function that has to evaluate to a strict true value for it to be valid.

The concept is similar to the invariant library, but for validate.js.

Note: This validator will always be invoked by default. Thus it will check even when value is undefined or null. You can disable this behaviour by passing presence: false;

Installation

Using npm:

$ npm i --save vtypes-invariant
const validate = require('validate.js');
const invariant = require('vtypes-invariant');

// you can then proceed to register the required validators.
validate.validators.invariant = invariant;

Usage

const constraints = {
  attr: {
    condition: (value) => value === 'bar'
  }
}

validate({}, constraints);
// => {attr: ["Attr invariant violation"]}

validate({attr: 'bar'}, constraints);
// => undefined

validate({attr: 'foo'}, constraints);
// => {attr: ["Attr invariant violation"]}
const constrants = {
  attr: {
    presence: false,
    condition: (value) => value === 'bar'
  }
}

validate({}, constraints);
// => undefined

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

Available Options

name type default description
condition function undefined Returns a truthy value for validation to pass
message string invariant violation Error message
presence boolean true By default, runs even though value is undefined or null
truthy boolean false Allow the conditional function to return truthy values instead of strict boolean

The condition function takes in (value, key, attributes) of the validator.

Create sub-validators

The invariant validator also provides a invariant.create function which lets you quickly create new validators by calling invriant.create(condition, message), providing a condition and message.

License

vtypes-invariant is MIT licensed

Package Sidebar

Install

npm i vtypes-invariant

Weekly Downloads

0

Version

1.0.0

License

MIT

Last publish

Collaborators

  • geraldyeo