ragulka
ragulka

Reputation: 4342

How can I create an instance of a model and validate it without saving to DB in Sails?

I am wondering if and how I can do something like below in Sails. Basically, I am trying to handle complex validations - I have a Client model and ContactPerson model. The Client model has ContactPersons nested inside it, and I want to validate that they are all correct before continuing.

// client.js

module.exports = {

  attributes: {
    name: {
      type: 'string',
      required: true
    }
  },

  beforeValidation: function(values, cb) {
    values.contactPerson.forEach( function( person ) {
      var contactPerson = new sails.model.person( person );
      var results = contactPerson.validate();
      // If valid, continue
      // if not valid, invalidate the client Model (cause validationerror)
    });
  }
}

Upvotes: 0

Views: 120

Answers (1)

Travis
Travis

Reputation: 636

// if not valid
if(err) return cb(err);
// if valid
cb();

Upvotes: 1

Related Questions