validate travis-ci npm

Validate nested object properties in javascript

validate

Validate object properties in javascript.

npm version Build Status

Example

var schema = require('validate');
var user = schema({
  name: {
    type: 'string',
    required: true,
    message: 'Name is required.'
  },
  email: {
    type: 'string',
    required: true,
    match: /+\@.+\..+/,
    message: 'Email must be valid.'
  },
  address: {
    street: {
      type: 'string',
      required: true,
      message: 'Street is required.'
    },
    city: {
      type: 'string',
      required: true,
      message: 'City is required.'
    }
  }
});

var errors = user.validate(obj);

Each error has a .path, describing the full path of the property that failed validation, and a .message property.

errors[0].path //=> 'address.street'
errors[0].message //=> 'Street is required.'

You can also add paths to a schema by using the chainable API

user
  .path('username')
  .type('string')
  .required()
  .match(/[a-z]{2,16}/)
  .message('Username must be 2-16 chars.');

user
  .path('address.zip')
  .type('string')
  .required()
  .match(/[0-9]+/)
  .message('Zip is required.');

Typecasting

Values can be automatically typecasted before validation. To enable typecasting, pass an options object to the schema constructor with typecast set to true.

var user = schema({ name: 'string', age: 'number' }, { typecast: true });

You can override this setting by passing options to .validate()

user.validate(obj, { typecast: false });

Property stripping

By default, all values not defined in the schema will be stripped from the object. Set .strip = false on the options object to disable this behavior.

API

schema(paths, [opts])

Creates a new Schema with the given paths.

Schema#path(path, [rules])

Add path to schema with optional rules. Returns a Property.

Schema#validate(obj, [opts])

Validate given object. Returns an array of errors.

Schema#assert(obj, [opts])

Validate given object and throw if the validation fails.

Property#use(fn, [msg])

Use the given validation function with and optional error message. fn should accept a value and return true if the value is considered valid.

Property#type(name, [msg])

Property should be of type name.

Property#required(bool, [msg])

Property is required.

Property#match(regexp, [msg])

Proprety should match given regexp.

Property#each(fn, [msg])

Validate each value in array against given function fn.

Property#message(msg)

Set default error message for property.

Licence

MIT

Related Repositories

Validate

Validate

The jQuery Validate is an advanced plugin for an easily and quickly form validation. ...

koa-validate

koa-validate

validate koa request params and format request params ...

validate

validate

Validate nested object properties in javascript ...

validate

validate

A Go package to automatically validate fields with tags ...

validate-framework

validate-framework

validate-framework:一款轻量、无依赖的 JavaScript 验证组件 ...


Top Contributors

eivindfjeldstad Raynos narsic

Releases

-   3.0.1 zip tar
-   3.0.0 zip tar
-   2.1.6 zip tar
-   2.1.5 zip tar
-   2.1.4 zip tar
-   2.1.3 zip tar
-   2.1.2 zip tar
-   2.1.1 zip tar
-   2.1.0 zip tar