npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

validate-data-tree

v1.0.5

Published

Validate your data trees using a dict schema

Downloads

23

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":

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) |