Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cyppan/validate-data-tree
Validate your data trees using a dict schema
https://github.com/cyppan/validate-data-tree
Last synced: about 1 month ago
JSON representation
Validate your data trees using a dict schema
- Host: GitHub
- URL: https://github.com/cyppan/validate-data-tree
- Owner: cyppan
- License: mit
- Created: 2018-12-04T08:08:27.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-12-11T08:19:44.000Z (about 6 years ago)
- Last Synced: 2024-10-16T08:32:09.505Z (2 months ago)
- Language: JavaScript
- Size: 109 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Validate data tree
This library aims to validate data structures (input object, possibly with nested objects and arrays), by the mean of data structures (the schema).
Usable in the server and the browser seamlessly (as long as you use a compiler like babel)
## Get started
Because "un exemple vaut mille mots":
```js
const { validate } = require('validate-data-tree')const schema = {
email: {
// if allowNull is enabled and the input is null, validators are not called
allowNull: true,
validate: {
isEmail: true,
},
},
phone: {
allowNull: true,
},
settings: {
type: 'object',
schema: {
optinNewsletter: {
validate: {
isBoolean: true,
},
},
},
},
roles: {
type: 'array',
schema: {
$: {
validate: {
allowedKeys: ['name', 'until'],
},
},
name: {
validate: {
len: [3, 50],
matches: '^[A-Z]+$',
},
},
until: {
allowNull: true,
validate: {
isDate: true,
},
},
},
},
$: {
validate: {
// custom predicate, name it the way you want (here "oneOf")
// and it will be called with the value of the input
// at the path corresponding to the key
// (here $ corresponds to the whole object)
oneOf: ({ email, phone }) => email || phone,
// disallow extra keys
allowedKeys: ['email', 'phone', 'settings', 'roles'],
},
},
};const badUserInput = {
// missing an email or a phone
roles: [
// this one is ok
{ name: 'ADMIN', until: '2019-12-05T16:42:40.069Z' },
{ name: 'toolongandlowercase', extraKey: 'aïe' },
],
settings: {
optinNewsletter: 'should be a boolean',
},
};try {
validate(badUserInput, schema);
} catch (e) {
// e is an instance of ValidationErrors
e.errors.forEach((err) => { // instance of ValidationErrorItem
const {
path, value, message, validatorName, validatorArgs,
} = err;
console.log({
path, value, message, validatorName, validatorArgs,
});
});
}=>
{ path: '$',
value:
{ roles:
[ { name: 'ADMIN', until: '2019-12-05T16:42:40.069Z' },
{ name: 'toolongandlowercase', extraKey: 'aïe' } ],
settings: { optinNewsletter: 'should be a boolean' } },
message: 'Validation oneOf on $ failed',
validatorName: 'oneOf',
validatorArgs: [] }
{ path: 'settings.optinNewsletter',
value: 'should be a boolean',
message: 'Validation isBoolean on settings.optinNewsletter failed',
validatorName: 'isBoolean',
validatorArgs: [] }
{ path: 'roles.1',
value: { name: 'toolongandlowercase', extraKey: 'aïe' },
message: 'Validation allowedKeys on roles.1 failed',
validatorName: 'allowedKeys',
validatorArgs: [ 'name', 'until' ] }
{ path: 'roles.1.name',
value: 'toolongandlowercase',
message: 'Validation matches on roles.1.name failed',
validatorName: 'matches',
validatorArgs: [ '^[A-Z]+$' ] }
```## Available validators
This library is inspired from the npm packages `validator.js` and the extensions provided by `sequelize` (the DSL is compliant)
Then you can refer to their docs:
- https://www.npmjs.com/package/validator#validators
- https://github.com/sequelize/sequelize/blob/master/lib/utils/validator-extras.jsAdded validators:
| validator key | arguments |
| --- | --- |
| allowedKeys | array of keys (ex: `['k1', 'k2']`) |
| size | `[min, optional max]` (ex: `[1, 5]` or `[1]` for unbound maximum size) |