npm.io
0.3.1 • Published 11 years ago

revalidator

Licence
Apache 2.0
Version
0.3.1
Deps
0
Vulns
0
Weekly
0
Stars
591

revalidator Build Status

A cross-browser / node.js validator with JSONSchema compatibility as the primary goal.

Example

The core of revalidator is simple and succinct: revalidator.validate(obj, schema):

  var revalidator = require('revalidator');

  console.dir(revalidator.validate(someObject, {
    properties: {
      url: {
        description: 'the url the object should be stored at',
        type: 'string',
        pattern: '^/[^#%&*{}\\:<>?\/+]+

This will return with a value indicating if the obj conforms to the schema. If it does not, a descriptive object will be returned containing the errors encountered with validation.

  {
    valid: true // or false
    errors: [/* Array of errors if valid is false */]
  }

In the browser, the validation function is exposed on window.validate by simply including revalidator.js.

Installation

Installing npm (node package manager)
  $ curl http://npmjs.org/install.sh | sh
Installing revalidator
  $ [sudo] npm install revalidator

Usage

revalidator takes json-schema as input to validate objects.

revalidator.validate (obj, schema, options)

This will return with a value indicating if the obj conforms to the schema. If it does not, a descriptive object will be returned containing the errors encountered with validation.

{
  valid: true // or false
  errors: [/* Array of errors if valid is false */]
}
Available Options
  • validateFormats: Enforce format constraints (default true)
  • validateFormatsStrict: When validateFormats is true treat unrecognized formats as validation errors (default false)
  • validateFormatExtensions: When validateFormats is true also validate formats defined in validate.formatExtensions (default true)
  • additionalProperties: When additionalProperties is true allow additional unvisited properties on the object. (default true)
  • cast: Enforce casting of some types (for integers/numbers are only supported) when it's possible, e.g. "42" => 42, but "forty2" => "forty2" for the integer type.
Schema

For a property an value is that which is given as input for validation where as an expected value is the value of the below fields

required

If true, the value should not be undefined

{ required: true }
allowEmpty

If false, the value must not be an empty string

{ allowEmpty: false }
type

The type of value should be equal to the expected value

{ type: 'string' }
{ type: 'number' }
{ type: 'integer' }
{ type: 'array' }
{ type: 'boolean' }
{ type: 'object' }
{ type: 'null' }
{ type: 'any' }
{ type: ['boolean', 'string'] }
pattern

The expected value regex needs to be satisfied by the value

{ pattern: /^[a-z]+$/ }
maxLength

The length of value must be greater than or equal to expected value

{ maxLength: 8 }
minLength

The length of value must be lesser than or equal to expected value

{ minLength: 8 }
minimum

Value must be greater than or equal to the expected value

{ minimum: 10 }
maximum

Value must be lesser than or equal to the expected value

{ maximum: 10 }
allowEmpty

Value may not be empty

{ allowEmpty: false }
exclusiveMinimum

Value must be greater than expected value

{ exclusiveMinimum: 9 }
exclusiveMaximum

Value must be lesser than expected value

{ exclusiveMaximum: 11 }
divisibleBy

Value must be divisible by expected value

{ divisibleBy: 5 }
{ divisibleBy: 0.5 }
minItems

Value must contain more than expected number of items

{ minItems: 2 }
maxItems

Value must contain fewer than expected number of items

{ maxItems: 5 }
uniqueItems

Value must hold a unique set of values

{ uniqueItems: true }
enum

Value must be present in the array of expected values

{ enum: ['month', 'year'] }
format

Value must be a valid format

{ format: 'url' }
{ format: 'email' }
{ format: 'ip-address' }
{ format: 'ipv6' }
{ format: 'date-time' }
{ format: 'date' }
{ format: 'time' }
{ format: 'color' }
{ format: 'host-name' }
{ format: 'utc-millisec' }
{ format: 'regex' }
conform

Value must conform to constraint denoted by expected value

{ conform: function (v) {
    if (v%3==1) return true;
    return false;
  }
}
dependencies

Value is valid only if the dependent value is valid

{
  town: { required: true, dependencies: 'country' },
  country: { maxLength: 3, required: true }
}
Nested Schema

We also allow nested schema

{
  properties: {
    title: {
      type: 'string',
      maxLength: 140,
      required: true
    },
    author: {
      type: 'object',
      required: true,
      properties: {
        name: {
          type: 'string',
          required: true
        },
        email: {
          type: 'string',
          format: 'email'
        }
      }
    }
  }
}
Custom Messages

We also allow custom messages for different constraints

{
  type: 'string',
  format: 'url'
  messages: {
    type: 'Not a string type',
    format: 'Expected format is a url'
  }
{
  conform: function () { ... },
  message: 'This can be used as a global message'
}

Tests

All tests are written with vows and should be run with npm:

  $ npm test
Author: Charlie Robbins, Alexis Sellier
Contributors: Fedor Indutny, Bradley Meck, Laurie Harper, Martijn Swaagman
License: Apache 2.0
, required: true }, challenge: { description: 'a means of protecting data (insufficient for production, used as example)', type: 'string', minLength: 5 }, body: { description: 'what to store at the url', type: 'any', default: null } } }));

This will return with a value indicating if the __INLINE_CODE_2__ conforms to the __INLINE_CODE_3__. If it does not, a descriptive object will be returned containing the errors encountered with validation.

__CODE_BLOCK_1__

In the browser, the validation function is exposed on __INLINE_CODE_4__ by simply including __INLINE_CODE_5__.

Installation

Installing npm (node package manager)
__CODE_BLOCK_2__
Installing revalidator
__CODE_BLOCK_3__

Usage

__INLINE_CODE_6__ takes json-schema as input to validate objects.

revalidator.validate (obj, schema, options)

This will return with a value indicating if the __INLINE_CODE_7__ conforms to the __INLINE_CODE_8__. If it does not, a descriptive object will be returned containing the errors encountered with validation.

__CODE_BLOCK_4__
Available Options
  • validateFormats: Enforce format constraints (default true)
  • validateFormatsStrict: When __INLINE_CODE_9__ is true treat unrecognized formats as validation errors (default false)
  • validateFormatExtensions: When __INLINE_CODE_10__ is true also validate formats defined in __INLINE_CODE_11__ (default true)
  • additionalProperties: When __INLINE_CODE_12__ is true allow additional unvisited properties on the object. (default true)
  • cast: Enforce casting of some types (for integers/numbers are only supported) when it's possible, e.g. __INLINE_CODE_13__, but __INLINE_CODE_14__ for the __INLINE_CODE_15__ type.
Schema

For a property an __INLINE_CODE_16__ is that which is given as input for validation where as an __INLINE_CODE_17__ is the value of the below fields

required

If true, the value should not be undefined

__CODE_BLOCK_5__
allowEmpty

If false, the value must not be an empty string

__CODE_BLOCK_6__
type

The __INLINE_CODE_18__ should be equal to the expected value

__CODE_BLOCK_7__
pattern

The expected value regex needs to be satisfied by the value

__CODE_BLOCK_8__
maxLength

The length of value must be greater than or equal to expected value

__CODE_BLOCK_9__
minLength

The length of value must be lesser than or equal to expected value

__CODE_BLOCK_10__
minimum

Value must be greater than or equal to the expected value

__CODE_BLOCK_11__
maximum

Value must be lesser than or equal to the expected value

__CODE_BLOCK_12__
allowEmpty

Value may not be empty

__CODE_BLOCK_13__
exclusiveMinimum

Value must be greater than expected value

__CODE_BLOCK_14__
exclusiveMaximum

Value must be lesser than expected value

__CODE_BLOCK_15__
divisibleBy

Value must be divisible by expected value

__CODE_BLOCK_16__
minItems

Value must contain more than expected number of items

__CODE_BLOCK_17__
maxItems

Value must contain fewer than expected number of items

__CODE_BLOCK_18__
uniqueItems

Value must hold a unique set of values

__CODE_BLOCK_19__
enum

Value must be present in the array of expected values

__CODE_BLOCK_20__
format

Value must be a valid format

__CODE_BLOCK_21__
conform

Value must conform to constraint denoted by expected value

__CODE_BLOCK_22__
dependencies

Value is valid only if the dependent value is valid

__CODE_BLOCK_23__
Nested Schema

We also allow nested schema

__CODE_BLOCK_24__
Custom Messages

We also allow custom messages for different constraints

__CODE_BLOCK_25__ __CODE_BLOCK_26__

Tests

All tests are written with vows and should be run with npm:

__CODE_BLOCK_27__
Author: Charlie Robbins, Alexis Sellier
Contributors: Fedor Indutny, Bradley Meck, Laurie Harper, Martijn Swaagman
License: Apache 2.0