uphold/validator.js-validate

Name: validator.js-validate

Owner: Uphold

Description: Opinionated object validation function based on validator.js

Created: 2017-03-10 17:14:03.0

Updated: 2017-03-28 14:08:38.0

Pushed: 2017-05-30 14:34:09.0

Homepage: null

Size: 50

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

validator.js-validate

Opinionated object validation function based on validator.js.

Status

npm version node version build status

Installation

Install the package via yarn:

rn add validator.js-validate

or via npm:

m install validator.js-validate --save
Usage
Create validate function

This module exports a function that creates a validate function, for instance:

rt createValidateFunction from 'validator.js-validate';

t validate = createValidateFunction();

The created validate function works just like validating an object, but replaces the last groups argument with an options object:

date(data[Object], constraint[Object|Constraint], options[Object])
Options
mask (Default: true)

Returns given data masked with given constraint keys:

t data = { foo: 'bar', qux: 'qix' };
t constraint = { foo: is.equalTo('bar') };

ole.log(validate(data, constraint));
 foo: 'bar' }

ole.log(validate(data, constraint, { mask: false }));
rue
throws (Default: true)

Throws a new error when validation fails. To enable this option you must pass an error class when creating the validate function as argument.

This error constructor should be prepared to receive violations as argument, for example:

rt StandardError from 'standard-error';
rt createValidateFunction from 'validator.js-validate';

s ValidationFailedError extends StandardError {
nstructor(errors) {
super({ errors });



t validate = createValidateFunction(ValidationFailedError);
t data = { foo: 'bar' };
t constraint = { foo: is.equalTo('biz') };

{
lidate(data, constraint);
tch (e) {
nsole.log(e);
 ValidationFailedError {
   errors: {
     foo: [{
       __class__: 'Violation',
       assert: {
         __class__: 'EqualTo',
         ...
       }
     }]
   }
 }



ole.log(validate(data, constraint, { throws: false }));

 foo: [{
   __class__: 'Violation',
   assert: {
     __class__: 'EqualTo',
     ...
   }
 }]

groups

Use this option to validate with validation groups:

t data = { foo: 'bar' };
t constraint = { foo: [is('bar').EqualTo('bar'), is('biz').equalTo('biz')] };

ole.log(validate(data, constraint, { groups: 'biz' }));

 foo: [{
   __class__: 'Violation',
   assert: {
     __class__: 'EqualTo',
     ...
   }
 }]


ole.log(validate(data, constraint, { groups: 'bar' }));
 foo: 'bar' }
Test suite

Use the test script to run the test suite:

rn test

To test check coverage use the cover script:

rn cover

A full coverage report will be generated on the coverage folder.

Release
rn release [<version> | major | minor | patch]
License

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.