Code Monkey home page Code Monkey logo

validate's Introduction

validate

Validate object properties in javascript.

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
    },
    city: {
      type: 'string',
      required: true
    }
  },
});
  
var res = user.validate(obj);
res.errors; // array of error messages
res.accepted; // the accepted object

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. You can override this setting by passing options to .validate()

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

To override

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

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 object containing an array of error messages, .errors, and the accepted object, .accepted.

Schema#assert(obj, [opts])

Validate given object and throw if the validation fails. Returns the accepted object.

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#message(msg)

Set default error message for property.

Licence

MIT

validate's People

Contributors

eivindfjeldstad avatar raynos avatar sajadbahar avatar

Watchers

LiQidong avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.