Skip to content


Subversion checkout URL

You can clone with
Download ZIP
A minimalistic schema validator for Backbone models.
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


Build Status

A minimalistic and lightweight schema validator for Backbone models. Loosely inspired by Mongoose's schema validation. Feel free to fork, modify, and contribute to your needs. Pull requests and issues are welcome.

npm install backbone-schema


Can be used in the browser or the server. Just include backbone-schema in your project and define a schema property in your models.

See Backbone's documentation for more information.


See tests.js for more examples.

Basic example

var Backbone = require('backbone');

var User = Backbone.Model.extend({

  schema: {
      nickname: String
    , username: { type: String, required: true }
    , joined: Date

  // All your other normal Backbone.Model code...


var liam = new User();        // Emits an error because "username" is not defined.
liam.set('username', 'liam'); // Good to go
liam.set('nickname', 1);      // Emits an error because "nickname" must be a string
liam.set('nickname', 'Liam'); // Good to go
liam.set('joined', 'Tues');   // Emits an error because "joined" must be a date.

Extended example

var Backbone = require('backbone');

var Message = Backbone.Model.extend({

    schema: {
        nickname: String      // Types can be specified as functions...
      , text: {
          type: 'string'      // ...or they can be specified as strings.
        , required: true      // Makes the attribute required for the model to validate.
      , timestamp: {
          type: Date
        , validators: [       // Custom validator functions can be set in an array
            function (val) {  // Each validator will be given the value of the field
              if (val > new Date()) {
                return 'Timestamp can not be set to a future date.';
      , kind: {
          required: true
        , type: String
        , choices: [          // Enum-esque functionality. Forces the attribute to be
            'message'         // one of these choices in order to be valid.
          , 'left'
          , 'joined'
      , _isStrict: true       // Enables strict mode, which disallows arbitrary attributes



Below is a list of options available for each attribute in the schema.


If true, the field must be defined.

Type: Boolean, Default: false


An array of validator functions that will be passed the value of this field and ran. You can access the current model within these functions using this.

Type: Array, Default: []


If defined, the field must be of the specified type. May be a string or a function.

Type: Function or String, Default: null


An array of objects. If defined, then the value of this field must be in this array.

Type: Array, Default: []

Strict mode

Strict mode will invalidate the model if there are attributes in it that don't exist in the schema. It is off by default, but you can enable it by defining _isStrict in the top-level of your schema.

Something went wrong with that request. Please try again.