vdn

0.9.1 • Public • Published

vdn

Javascript validation library. Data driven schemas.

Written as a module using ES6 (ECMAScript 2015). Also uses the spread operator '...'.

tests

Note

THIS LIBRARY IS NOT ACTIVELY MAINTAINED. IF YOU WISH TO USE IT PLEASE BE PREPARED TO FORK AND ADD YOUR OWN FIXES/ADDITIONS.

Install

$ npm install vdn --save

Usage

const vdn = require('vdn');
 
const schema = {
  type: 'number',
  default: 42,
};
 
vdn.attempt(undefined, schema); // Result == 42 (default used)
vdn.attempt(3, schema);         // Result == 3 (valid)
vdn.attempt('5', schema);       // Result == 5 (string conversion)
vdn.attempt('f', schema);       // Throws ValidationError

Alternatively create schemas using the builder interface.

const schema = vdn.number().default(42);
 
...

There is also an extended schema form that lets you override the default error message for a rule.

const schema = {
  type: {
    value: 'number',
    message: '{{name}} is not a number.',
  },
  default: 42,
};

Because of this you MUST use the extended form when passing an object type as a value, eg:

const schema = {
  type: 'number',
  default: {
    value: {}, // Default is empty object.
  }
};
const schema = {
  type: 'number',
  default: {} // Error, invalid schema.
};

API Documentation

Follow the Link Here.

Readme

Keywords

Package Sidebar

Install

npm i vdn

Weekly Downloads

1

Version

0.9.1

License

MIT

Unpacked Size

46 kB

Total Files

22

Last publish

Collaborators

  • stuartmcmahon