Socket
Socket
Sign inDemoInstall

backbone-validator

Package Overview
Dependencies
1
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    backbone-validator

A super simple validator module for Backbone.


Version published
Weekly downloads
21
decreased by-64.41%
Maintainers
1
Install size
74.1 kB
Created
Weekly downloads
 

Readme

Source

backbone-validator Build Status

A super simple validator module for Backbone. It works both on the browser and nodejs.

Installing

In node:

npm install backbone-validator --save

In the browser make sure that you add the backbone-validator-min.js script after you have loaded both Underscore and Backbone. The minimised file is 2k.

Usage

var Backbone = require('Backbone');
var validator = require('validator');

var MyModel = Backbone.Model.extend({
  validate: validator.create({
    type: { equal: 'user', msg: "type must be `user`" },
    firstname: { type: 'string', minLength: 3, maxlength: 20 },
    email: { type: 'email' }
  })
});

var model = new MyModel();
model.on('invalid', function (m, err) {
  // Validation failed
  // `err` will be an object with the error message {type:'message'}.
});
model.set({ type: 'not user' }, { validate: true });

API

validator.create( schema )

To use this module you basically invoke validator.create() passing it a schema object. This will return a function, and we set the model's validate property to this function, so that Backbone can use when setting attribute values (ie: when model.save() is invoked).

Defining a schema

A schema object contains a property for each attribute we want to validate, the property name is the attribute name and the value is an object containing a set of rules.

In the example below we want to validate the ctime, status and message attribues in our model, so our schema will look something like this:

validator.create({
  ctime: { type: 'date' },
  status: { oneOf: [ 1, 2, 3 ] },
  message: { type: string, minLength: 5 }
});

Rules

Eache rule is declared passing it options. This options depend on each of the rules (ie: for the required rule options is just a boolean, for the oneOf its an array, for custom its a function and so on.

  • required
validator.create({
  message: { required: true }
});
  • equal
validator.create({
  type: { equal: 'user' }
});
  • regexp
validator.create({
  birthday: { regexp: /^\d{2}\/\d{2}\/\d{4}$/ }
});
  • oneOf
validator.create({
  colour: { oneOf: [ 'red', 'green', 'blue' ] }
});
  • type. Types: boolean, number, string, date, array, email, model, collection, url and domain.
validator.create({
  balance: { type: 'number' }
});
  • minLength. Can be used with strings or arrays.
validator.create({
  firstname: { type: 'string', minLength: 3 }
});
  • maxLength. Can be used with strings or arrays.
validator.create({
  firstname: { type: 'string', maxLength: 20, minLength: 2 }
});
  • recurse. Can be used to do submodel validation.
validator.create({
  submodel: { type: 'model', recurse: true }
});

Custom validation rules

var MyModel = Backbone.Model.extend({
  validate: validator.create({
    phone: {
      custom: function (value) {
        // This function will be called with the value that needs to be
        // validated. If you want validation to fail simply return a string with
        // the error message. If nothing is returned validation for this
        // attribute is consider to have passed.
      }
    }
  })
});

Custom error messages

backbone-validator comes with default error messages that can be overriden.

validator.create({
  field: { regexp: /aregex/, msg: "A custom message." }
});

TODO

  • Add browser tests.

FAQs

Last updated on 13 Dec 2013

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc