@uphold/validator.js

2.1.0 • Public • Published

Uphold Validator.js

Extensive validations built on top of validator.js.

Improvements over validator.js:

  • extended set of asserts
  • logging with debugnyan
  • throwing specific errors on validation failures
  • mask validated data

Install

Add to your package.json dependencies:

npm install @uphold/validator.js

Usage

Setup example:

const { ValidationFailedError } = require('@uphold/http-errors');
const validator = require('@uphold/validator.js');
const asserts = require('path/to/asserts');

module.exports = validator({
  AssertionError: Error,
  ValidationError: ValidationFailedError,
  extraAsserts: asserts,
  mask: true
});

Throw a 400 error (invalid user input):

const { is, validate } = require('path/to/validator');

try {
  validate({
    foo: 'bar'
  }, {
    foo: is.ofLength({ min: 5 })
  });
} catch (e) {
  console.log({
    name: e.name,
    message: e.message,
    errors: e.errors.foo.map(error => error.show())
  });
  // {
  //   name: 'ValidationFailedError',
  //   message: 'Validation Failed',
  //   errors: [{
  //     assert: 'Length',
  //     value: 'bar',
  //     violation: { min: 5 },
  //   }]
  // }
}

Throw a 500 error (invalid code):

const { assert, is } = require('path/to/validator');

try {
  assert({
    foo: 'bar'
  }, {
    foo: is.ofLength({ min: 5 })
  });
} catch (e) {
  console.log({
    name: e.name
  });
  // {
  //   name: 'Error'
  // }
}

Return validated properties (masking):

Create a validator with masking enabled by passing the mask option set to true.

const { assert, is } = require('path/to/validator');

const validatedData = validate({
  foo: 'bar',
  biz: 'baz'
}, {
  foo: is.string()
});

console.log(validatedData);
// { foo: 'bar' }

Release

$ yarn release [<newversion> | major | minor | patch]

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @uphold/validator.js

Weekly Downloads

1,373

Version

2.1.0

License

MIT

Unpacked Size

14.2 kB

Total Files

7

Last publish

Collaborators

  • botatuphold
  • satazor
  • americas
  • kurayama
  • pedrobranco
  • fixe
  • franciscocardoso
  • cristiano.oliveira
  • ruimarinho