Validating your models

While JSData provides Model Lifecycle Hooks like validate, it does not do validation for you, but allows you to use whatever validation library you'd like.


This example uses Validate.js.

npm install --save js-data validate.js or bower install --save js-data validate

var store = new JSData.DS();

validate.validators.isString = function(value, options, key, attributes) {
  return validate.isString(value) ? null : key + ' must be a string!';

var productSchema = {
  id: {
    numericality: true
  name: {
    presence: true,
    isString: true
  price: {
    numericality: true

var Product = store.defineResource({
  name: 'product',
  idAttribute: '_id',
  validate: function (Product, product, cb) {
    var err = validate(product, productSchema);
    if (err) {
    } else {
      // pass the product along 
      cb(null, product);


Need Help?

Want more examples or have a question? Ask on the Slack channel or post on the mailing list then we'll get your question answered and probably update this wiki.