Skip to content

uphold/uphold-validator.js

Repository files navigation

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

Status

npm version build status

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 process

The release of a version is automated via the release GitHub workflow. Run it by clicking the "Run workflow" button.

License

MIT

About

Extensive validations built on top of validator.js.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 10