0.3.2 • Published 9 years ago

bookshelf-revalidator v0.3.2

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

Validation plugin for Bookshelf ORM

Plugin built atop the revalidator package and JSON Schema compatible.

Installation

Install package

$ npm install bookshelf-revalidator

Add plugin to Bookshelf instance

// Setup Bookshelf
var knex = require('knex')(config.db);
var bookshelf = require('bookshelf')(knex);

// Add plugin
bookshelf.plugin(require('bookshelf-revalidator'));

Using

Add validation rules to model

var Stuff = bookshelf.Model.extend({
  tableName: 'stuff',
  
  // Define validation rules
  rules: {
    name: { type: 'string', maxLength: 80, allowEmpty: false, required: true },
    gender: { enum: ['male', 'female'] },
    email: { format: 'email', maxLength: 40 },
    about: { type: ['string', 'null'] },
    last_visited: { type: 'object', conform: function(v) { return v instanceof Date }}
  }
  
});

For the full rules syntax see revalidator docs.

Try to save model

var stuff = new Stuff({ name: 'John', email: 'bad email' });
stuff.save()
  .then(function() {
    // All is ok, do something
  })
  .catch(Stuff.ValidationError, function(err) {
    // Validation error!
    console.error(stuff.errors); // or err.errors 
  })
  .done();

Set only specific fields

Method below sets only attributes listed in the model rules. It can be useful to prevent DB error or to prevent setting of read-only attributes.

var stuff = new Stuff();
stuff.setOnlyListed(req.body);