Defining custom validation message in sails js

Sails JS is a great MVC framework for node js and provides some great functionalities (e.g., blueprint REST api) out of the box. As the framework is still in active development it lacks some features that need some hacking to make them work.

Currently, there is no way to define custom validation messages for the models. The validator always returns default messages which are not much user-friendly‚Äč. Here is an ugly fix to the problem:

Create a module in node_modules named my-validation-utils. create an index.js file there. and put the following content there:

// define custom validation messages for each of the rule for each model
var user = {  
    required:'Email Required',
    email:'Should be an email'
    required:'name required'


var product={  
         required:'Product name is required'

var validationMessages = {  

 * This function expects the name of the model and error.validationError
 * and puts the user defined messages in error.validationError 
module.exports = function(model, validationError) {  
  var messages = validationMessages[model];
  for (key in messages) {
    var element = messages[key];
    if (validationError[key]) {
      for (i in validationError[key]) {
        var err = validationError[key][i];
        err.message = element[err.rule];
  return validationError;

Now in the controller doing something like following should work:

// import the validator
var validator = require('my-validation-utils');

User.create(user).done(function(error, user) {  
  if (error) {
    if (error.ValidationError) {
      // puts the messages for model user
      var errors = validator('user', error.ValidationError);
      //now errors contains the validationErrors with user defined messages
  } else {
    // user is saved

While Sails JS team is busy developing the feature, this solves the issue for now!

Moinul Hossain

Read more posts by this author.