如何在sails.js中定义错误消息

时间:2013-12-06 09:04:52

标签: node.js sails.js

这是我第一次使用sails并且它看起来很好但我遇到了问题,是否可以在sails模型验证中定义自定义错误消息,因为它看起来像返回的错误消息是技术而不是用户友好。

由于

更新:https://gist.github.com/mikermcneil/8366092

2 个答案:

答案 0 :(得分:6)

这是另一种选择:

/**
 * Takes a Sails Model object (e.g. User) and a ValidationError object and translates it into a friendly
 * object for sending via JSON to client-side frameworks.
 *
 * To use add a new object on your model describing what validation errors should be translated:
 *
 * module.exports = {
 *   attributes: {
 *     name: {
 *       type: 'string',
 *       required: true
 *     }
 *   },
 * 
 *   validation_messages: {
 *     name: {
 *       required: 'you have to specify a name or else'
 *     }
 *   }  
 * };
 *
 * Then in your controller you could write something like this:
 *
 * var validator = require('sails-validator-tool');
 *
 * Mymodel.create(options).done(function(error, mymodel) {
 *   if(error) {
 *     if(error.ValidationError) {
 *       error_object = validator(Mymodel, error.Validation);
 *       res.send({result: false, errors: error_object});
 *     }
 *   }
 * });
 *
 * @param model {Object} An instance of a Sails.JS model object.
 * @param validationErrors {Object} A standard Sails.JS validation object.
 *
 * @returns {Object} An object with friendly validation error conversions.
 */ 
module.exports = function(model, validationError) {
  var validation_response = {};
  var messages = model.validation_messages;
  validation_fields = Object.keys(messages);

  validation_fields.forEach(function(validation_field) {

    if(validationError[validation_field]) {
      var processField = validationError[validation_field];
      //console.log(processField);
      processField.forEach(function(rule) {
        if(messages[validation_field][rule.rule]) {
          if(!(validation_response[validation_field] instanceof Array)) {
            validation_response[validation_field] = new Array();
          }

          var newMessage={};
          newMessage[rule.rule] = messages[validation_field][rule.rule];
          validation_response[validation_field].push(newMessage);
        }
      });

    }
  });

  return validation_response;
};

致记:sfb _

答案 1 :(得分:4)

这是一个丑陋的修复:

创建一个名为my-validation-utils的文件夹。在那里创建一个index.js文件。并将以下内容放在那里:

var user = {
  email:{
    required:'Email Required',
    email:'Should be an email'
  },
  name:{
    required:'name required'
  }

};

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

var validationMessages = {
  user:user,
  product:product
};

/**
 * 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;
};

现在在您的控制器中执行以下操作:

User.create(user).done(function (error, user) {
  if (error) {
    if (error.ValidationError) {
      var validator = require('path/to/my-validation-utils');
      var errors = validator('user',error.ValidationError);// puts the messages for model user
     //now errors contains the validationErrors with user defined messages
    }

  } else {
   //user is saved
  }
});