A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/flatiron/revalidator below:

GitHub - flatiron/revalidator: A cross-browser

revalidator

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

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: '^/[^#%&*{}\\:<>?\/+]+$',
        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 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.

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

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 */]
}

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

If true, the value should not be undefined

If false, the value must not be an empty string

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'] }

The expected value regex needs to be satisfied by the value

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

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

Value must be greater than or equal to the expected value

Value must be lesser than or equal to the expected value

Value may not be empty

Value must be greater than expected value

Value must be lesser than expected value

Value must be divisible by expected value

{ divisibleBy: 5 }
{ divisibleBy: 0.5 }

Value must contain more than expected number of items

Value must contain fewer than expected number of items

Value must hold a unique set of values

Value must be present in the array of expected values

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

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' }

Value must conform to constraint denoted by expected value

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

Value is valid only if the dependent value is valid

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

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'
        }
      }
    }
  }
}

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'
}

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4