vtypes-immutable

1.0.0 • Public • Published

vtypes-immutable

"immutable" validator for validate.js

npm package vtypes

About

The immutable validator attempts to ensure that the field is a valid immutable value. The API for checking immutability is referenced off immutable.js.

If no parser is provided, then best effort guess if a value is immutable based on availability of conversion functions like toJS, toJSON, toArray, toObject;

Installation

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

Usage

const Immutable = require('immutable');
 
validate({}, {attr: {immutable: true}});
// => undefined
 
validate({attr: 'foo'}, {attr: {immutable: true}});
// => {attr: ["Attr is not an immutable value"]}
 
// note: both "type" and "library" must be preset.
const constraints = {
  attr: {
    immutable: {
      library: Immutable,
      type: 'List'
    }
  }
}
 
validate({attr: 'foo'}, constraints);
// => {attr: ["Attr is not an immutable List"]}

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

Available Options

name type default description
library func undefined The immutable library
message string is not an immutable %{type} Error message
type string undefined A type that is accepted by immutable library

License

vtypes-immutable is MIT licensed

Package Sidebar

Install

npm i vtypes-immutable

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • geraldyeo