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

bookshelf-validate

v2.0.3

Published

Validation for the Model objects of Bookshelf.js

Downloads

50

Readme

bookshelf-validate

Version (npm)

Validation for the Model objects of Bookshelf.js

Installation

npm install --save bookshelf-validate

Configuration

To initialize the plugin, call bookshelf.plugin('bookshelf-validate'[, config]); with your Bookshelf instance.

The config object is optional, and the defaults are:

{
  validator: require('validator'), // node-validator
  validateOnSave: false // Automatically validate when Bookshelf emits 'saving' event
}

config.validator

By default, the validation methods are provided by the validator npm package.

You can pass a different validator object, or customize node-validator by adding methods to it before passing it to the configuration. The only requirement is that every method on the validator object takes the value to be validated as the first argument.

Example by customizing validator:

var validator = require('validator');

validator.isPrime = function (str) {
  var value = parseInt(str);
  if (value === NaN || value < 2) return false;

  for (var i = 2; i <= value >> 1; i++) {
    if (value % i === 0) {
      return false;
    }
  }
};

bookshelf.plugin('bookshelf-validate', {
  validator: validator
});

config.validateOnSave

If validateOnSave is true, the validations will automatically be called when Bookshelf emits a 'saving' event. If any validations fail, an error will be thrown and the model will not be saved.

API

'isRequired'

Since node-validator does not look at undefined or null values, an optional validator isRequired is provided with the library. It is off by default.

Model.validations

To use bookshelf-validator in your models, you must add a validations key to your Bookshelf model. Each key of the validations object must be an attribute of the Bookshelf model. There are four ways to write the validations for each attribute, depending on the level of specifity you need to provide.

Option 1: Validation method name as string

var User = bookshelf.Model.extend({
  validations: {
    username: 'isRequired'
  }
});

Option 2: Validation method name as key in object

With this option, the values of the validation method name keys will be passed to the validation methods as arguments. An array of values may be passed for arbitrary-arity methods.

var User = bookshelf.Model.extend({
  validations: {
    username: {
      isRequired: true,
      isLength: [2, 32] // will call validator.isLength(value, 2, 32)
    }
  }
});

Option 3: Validation method with custom error message

With this option, if the validation fails, your custom error message will be returned instead of the default (the name of the method which failed validation). This is good if you want to send messages to the user about why validation failed.

var User = bookshelf.Model.extend({
  validations: {
    username: {
      method: 'isLength',
      error: 'Your username must be between 4 and 32 characters long.',
      args: [4, 32]
    }
  }
});

The error and args attributes are optional.

Option 4: An array with a combination of any of the above

For more complicated validations, you may pass an array that has a combination of strings and objects. If an object has an attribute called method it will be assumed to be type (3), otherwise it will be type (2). Since you will probably not name a validation method with the name method, it is unlikely there will be any problems.

var User = bookshelf.Model.extend({
  validations: {
    email: [
      'isRequired',
      { isEmail: {allow_display_name: true} }, // Options object passed to node-validator
      { method: 'isLength', error: 'Username 4-32 characters long.', args: [4, 32] } // Custom error message
    ]
  }
});

Model#validationErrors

Call validationErrors() on a model instance to see the results of your validation.

If there are any invalid attributes on your model, the result will be a ValidationError object containing an object of attributes and their errors on the data property.

If the validateOnSave: true option was configured, validationErrors() will be called automatically when you attempt to save the model. If not, you can call it yourself to see the errors before you save the model.

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

validator.isRequired = function (val) {
  return val != null;
}

bookshelf.plugin('bookshelf-validate', {
  validator: validator,
  validateOnSave: true
});

var User = bookshelf.Model.extend({
  tableName: 'users',
  validations: {
    // Username is required, and its length must be between 2 and 32 characters
    username: [
      'isRequired',
      { method: 'isLength', error: 'Username must be between 2 and 32 characters.', args: [2, 32] }
    ],

    // Email is required, and must be a valid e-mail address
    email: [
      'isRequired',
      { method: 'isEmail', error: 'Not a valid email address' }
    ],

    // Birthday is not required, but must be a date if given
    birthday: { isDate: true } // Same as `birthday: 'isRequired'`
  }
});

var user = new User({
  username: 'x', // Invalid length
  birthday: '1997-11-21', // Valid
  color: 'green' // Not validated
});

let errors = user.validationErrors();
console.log(errors);
/*
  [ValidationError]:
  {
    data: {
      username: ['Username must be between 2 and 32 characters'],
      email: ['isRequired']
    }
  }
/*

And, if validateOnSave is true:

user.save(); // Throws a ValidationError