# validate-data-tree

> Validate your data trees using a dict schema

Latest version **1.0.5** (published 2018-12-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install validate-data-tree
pnpm add validate-data-tree
yarn add validate-data-tree
bun add validate-data-tree
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2018-12-11 |
| First published | 2018-12-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 45.9 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | cyppan |
| Maintainers | cyppan |
| Keywords | validation |

## Links

- npm: https://www.npmjs.com/package/validate-data-tree
- Repository: https://github.com/cyppan/validate-data-tree
- Homepage: https://github.com/cyppan/validate-data-tree#readme
- Issues: https://github.com/cyppan/validate-data-tree/issues
- npm.io page: https://npm.io/package/validate-data-tree

## Dependencies (4)

- [lodash](https://npm.io/package/lodash.md) ^4.17.11
- [moment](https://npm.io/package/moment.md) ^2.22.2
- [immutable](https://npm.io/package/immutable.md) ^4.0.0-rc.12
- [validator](https://npm.io/package/validator.md) ^10.9.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.5 (latest) — 2018-12-11
- 1.0.4 — 2018-12-08
- 1.0.3 — 2018-12-08
- 1.0.2 — 2018-12-08
- 1.0.1 — 2018-12-08
- 1.0.0 — 2018-12-06

## 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.js

Added validators:

| validator key | arguments |
| --- | --- |
| allowedKeys | array of keys (ex: `['k1', 'k2']`) |
| size | `[min, optional max]` (ex: `[1, 5]` or `[1]` for unbound maximum size) |

---
_Source: https://npm.io/package/validate-data-tree · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
