npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

backbone-schema

v0.8.0

Published

Forced schemas for Backbone models.

Downloads

142

Readme

backbone-schema

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

Usage

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.

Example

See tests.js for more examples.

Basic example

var Backbone = require('backbone');
require('backbone-schema');

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');
require('backbone-schema');

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
    }

});

Options

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

required

If true, the field must be defined.

Type: Boolean, Default: false

validators

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: []

type

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

Type: Function or String, Default: null

choices

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.