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 🙏

© 2025 – Pkg Stats / Ryan Hefner

bookshelf-fields

v0.4.3

Published

Bookshelf fields

Downloads

132

Readme

bookshelf-fields

Bookshelf plugin for simpler model validation and field format convertion.

Please look at bookshelf-schema - plugin that does the same and many more!

What exactly it does

First of all when plugged in with call to db.plugin it (re)defines several db.Model methods:

  • validate - perform validation of model with CheckIt. It uses instance array 'validations' for field-level validation and array 'model_validations' for validating the whole model.

  • format/parse - for each field defined in model applys its format/parse method

Then when field is applyed to a model it

  • stores the itself in the models __meta.fields array

  • may add some validation rules to validations of model_validations arrays

  • defines property with the same name. By default this property only calls basic set and get methods. You may prevent creation of property by passing dont_create_properties: true or create_properties: false to Fields.plugin or as an option of the field

And finally when called to enable_validation it redefines models initialize method, adding subscription to event 'saving' to perform validation.

Basic usage

First you need to require bookshelf-fields and apply exported plugin to an initialized database instance:

Fields = require 'bookshelf-fields'
db.plugin Fields.plugin(options)

Now you are ready to add fields information to models. There are two equivalent ways to do it: with exported functions 'field', 'fields' and 'enableValidation' and with the same methods, mixed into a Model prototype. If you choose the second way you need to pass option augement_model: true to plugin.

Provided helpers

  • db.Checkit - Checkit module used for validation

  • plugin(options) - method that mixes Fields functionality into a Bookshelf Model

    db.plugin Fields.plugin()

  • enable_validation(model, options) - actually turn on validation for a specified model. Options are stored in Model.prototype.validation_options and passed to Checkit when validation applied.

    enable_validation(User)

  • field(model, field_class, name, options) - add field to a model

    field(User, Fields.StringField, 'username', {max_length: 64})

  • fields(model, field_definitions...) - add a bunch of fields to a model. field_definitions is one or more arrays like [field_class, name, options]

Fields

Common options

  • required: boolean - field must be provided and not empty

  • exists: boolean - field must not be undefined

  • choices: [array or hash] - field must have one of provided values

    choices may be defined as an array (['foo', 'bar']) or as a hash ({foo: 'foo description', bar: bar description'}). If hash used then field value is compared with hash keys.

  • comparator: function - used with choices to provide custom equality checker.

    Useful if fields value is an object and simple '==' is not adequate.

  • message: used as a default error message.

  • label: used as a default field label and substituted to error message. Look at tgriesser/checkit for details.

StringField

  • min_length | minLength: integer
  • max_length | maxLength: integer

EmailField

StringField with simple check that value looks like a email address

NumberField

Does no any validation - use IntField or FloatField instead!

  • gt | greater_than | greaterThan: integer
  • gte | greater_than_equal_to | greaterThanEqualTo | min: integer
  • 'lt|less_than|lessThan`: integer
  • lte | less_than_equal_to | lessThanEqualTo | max: integer

IntField

NumberField checked to be Integer. Applys parseInt when loaded.

FloatField

NumberField checked to be Float. Applys parseFloat when loaded.

BooleanField

Casts value to Boolean on parse and format.

DateTimeField

Validates that value is a Date or a string than can be parsed as Date. Converts value to Date on parse and format.

DateField

DateTimeField with stripped Time part.

JSONField

Validates that value is object or a valid JSON string. Parses string from JSON when loaded and stringifies to JSON when formatted.

Advanced validation

  • you may assign object instead of value to validation options:

    mix_length: {value: 10, message: '{{label}} is too short to be valid!'}

    Additional options will be passed to checkit.

  • you may add complete Checkit validation rules to field with validations option:

    @field StringField 'username', validations: [{rule: 'minLength:5'}]

Add custom behaviour

You can add extra validations to models arrays validations and model_validations. Just make sure that you doesn't throw away validations added by fields. If you redefine initialize method call parent initialize or manage calling validate method on model saving. You can also redefine validate method.

Examples

coffeescript

Bookshelf = require 'bookshelf'
Fields = require 'bookshelf-fields'

db = Bookshelf.initialize
    client: 'sqlite'
    connection:
        filename: './test.db'

db.plugin Fields.plugin(augement_model: true)

class User extends db.Model
    tableName: 'users'

    @enable_validation()
    @field Fields.StringField, 'username', max_length: 32
    @field Fields.EmailField, 'email'

new User(username: 'bogus', email: '[email protected]').save()
    .otherwise (errors) ->
        console.log errors.toJSON()
        throw errors
    .then (user) ->
        console.log user.id
        console.log user.username # username is a property, calling @get('username') in getter
        console.log user.email

        user.email = 'invalid-email' # calls @set('email', 'invalid-email') in setter

        user.save().otherwise (errors) ->
            console.log errors.toJSON() # { email: 'The email must contain a valid email address' }

###javascript

Bookshelf = require('bookshelf');
Fields = require('bookshelf-fields');

var db = Bookshelf.initialize({
    client: 'sqlite',
    connection: { filename: './test.db' }
});

db.plugin(Fields.plugin);

User = db.Model.extend({ tableName: 'users' });
Fields.enable_validation(User);

Fields.fields(User,
    [Fields.StringField, 'username', {max_length: 32}],
    [Fields.EmailField, 'email']
);

new User({username: 'bogus', email: '[email protected]'}).save()
    .otherwise(function(errors) {
        console.log(errors.toJSON());
        throw errors;
    }).then(function(user) {
        console.log(user.id);
        console.log(user.username); // username is a property, calling @get('username') in getter
        console.log(user.email);

        user.email = 'invalid-email'; // calls @set('email', 'invalid-email') in setter

        user.save().otherwise(function(errors) {
            console.log(errors.toJSON()); // { email: 'The email must contain a valid email address' }
        });
});