0

I would like to respond with a 406, when the user tries to set their password to a length less than 8 characters. I am doing this validation in the beforeCreate function on my model. But Sails responds with a 500 and an Internal Server Error message. Is there anyway to access the response object and respond with my own response?

module.exports = {
  beforeCreate: function (attrs, next) {
    if (attrs.password.length < PASS_MIN_LENGTH) {
      return next({status: 406, message: "Password must be at least 8 characters"});
    }
    next();
  },
 ... // Model definition below
};
jww
  • 97,681
  • 90
  • 411
  • 885
justspamjustin
  • 1,730
  • 3
  • 19
  • 30

1 Answers1

0

It depends if your using blueprints? On an error you can set the response to whatever back in the controller that called the model. If your using blueprints, then you will need to edit them to fit your needs. (create a blueprints folder in /api and copy sails blueprints there making sure to lower case findOne.js and you can modify as neccessary)

But you should know that there are places for validations.

For this example you can place a minLength attribute on your model.

https://github.com/balderdashy/waterline-docs/blob/master/models.md#validations

Or you can create a custom validations as well. If if your using a beforeCreate call back because PASS_MIN_LENGTH is a variable you want to use, then you can use that in a validation as well.

Meeker
  • 5,979
  • 2
  • 20
  • 38