https://github.com/uphold/uphold-validator.js
Extensive validations built on top of validator.js
https://github.com/uphold/uphold-validator.js
Last synced: 2 months ago
JSON representation
Extensive validations built on top of validator.js
- Host: GitHub
- URL: https://github.com/uphold/uphold-validator.js
- Owner: uphold
- License: mit
- Created: 2018-08-06T13:39:54.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-07-11T01:50:59.000Z (almost 2 years ago)
- Last Synced: 2025-01-29T07:30:44.150Z (4 months ago)
- Language: JavaScript
- Size: 528 KB
- Stars: 0
- Watchers: 51
- Forks: 2
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# 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:
```sh
npm install @uphold/validator.js
```## Usage
Setup example:
```js
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):
```js
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):
```js
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`.
```js
const { assert, is } = require('path/to/validator');const validatedData = validate({
foo: 'bar',
biz: 'baz'
}, {
foo: is.string()
});console.log(validatedData);
// { foo: 'bar' }
```## Release
```sh
$ yarn release [ | major | minor | patch]
```## License
MIT