Muntasim
Muntasim

Reputation: 6786

Sails js :: How to add custom validation error

Like Rails is there any way to add custom error message to validator?

Like:

if(this.password != this.passwordConfirmation){
   this.errors.add('password', {rule: 'invalid'})
} 

Upvotes: 2

Views: 2619

Answers (3)

Armadeus
Armadeus

Reputation: 37

Out of the box, Sails.js does not support custom validation messages. But there's a workaround, using hooks.

http://sailsjs.org/documentation/concepts/models-and-orm/validations#?custom-validation-rules

Says the official site.

Upvotes: 0

Meeker
Meeker

Reputation: 5979

You can create custom validations on your models. Or create custom objects and inject them into your models to resusable code. Its actually in the docs!

http://sailsjs.org/#/documentation/concepts/ORM/Validations.html?q=custom-validation-rules

Upvotes: 3

Martin Malinda
Martin Malinda

Reputation: 1608

You can create a custom config file for error handling. You can reach that global config object by sails.config.error for example. Advantage of this solution is, that you can access this object in services and other places, where you have no access to the res object.

Next step would be creating a policy which would pass this config error object to res.locals. Or it could be handled in a response file, but I have no experience with that.

Upvotes: 2

Related Questions