2012-01-24 41 views
11

我真的是新来的猫鼬,所以我想知道是否有一种方法来设置custom error message而不是像Validator "required" failed for path password这样的默认设置。自定义(用户友好)ValidatorError消息

我想设置一个像Password is required.这是更方便用户。

我写了一些自定义验证器,并设置了type属性,这个用户友好的错误消息,但我不知道type是错误消息的正确的占位符。也没有办法设置自定义邮件预定义的验证像min, max, required, enum...

一种解决方案是检查每一次抛出错误的type财产和手动分配错误消息,但认为这是验证的工作:

save model 
    if error 
     check error type (eg. "required") 
     assign fancy error message (eg. "Password is required.") 

这显然不是理想的解决方案。

我看着express-formnode-validator但仍想使用猫鼬验证功能。

+0

有也表示,验证效果很好。 – chovy

回答

16

我通常对这样的事情使用辅助函数。只是嘲笑这个比我使用的更普遍一点。这个人将采取所有“默认”验证器(必需的,最小值,最大值等),并使它们的信息更漂亮一些(根据下面的messages对象),并且只提取您在验证器中传递的消息验证。

function errorHelper(err, cb) { 
    //If it isn't a mongoose-validation error, just throw it. 
    if (err.name !== 'ValidationError') return cb(err); 
    var messages = { 
     'required': "%s is required.", 
     'min': "%s below minimum.", 
     'max': "%s above maximum.", 
     'enum': "%s not an allowed value." 
    }; 

    //A validationerror can contain more than one error. 
    var errors = []; 

    //Loop over the errors object of the Validation Error 
    Object.keys(err.errors).forEach(function (field) { 
     var eObj = err.errors[field]; 

     //If we don't have a message for `type`, just push the error through 
     if (!messages.hasOwnProperty(eObj.type)) errors.push(eObj.type); 

     //Otherwise, use util.format to format the message, and passing the path 
     else errors.push(require('util').format(messages[eObj.type], eObj.path)); 
    }); 

    return cb(errors); 
} 

它可以像这样(快速路由器为例)使用:

function (req, res, next) { 
    //generate `user` here 
    user.save(function (err) { 
     //If we have an error, call the helper, return, and pass it `next` 
     //to pass the "user-friendly" errors to 
     if (err) return errorHelper(err, next); 
    } 
} 

前:

{ message: 'Validation failed', 
    name: 'ValidationError', 
    errors: 
    { username: 
     { message: 'Validator "required" failed for path username', 
     name: 'ValidatorError', 
     path: 'username', 
     type: 'required' }, 
    state: 
     { message: 'Validator "enum" failed for path state', 
     name: 'ValidatorError', 
     path: 'state', 
     type: 'enum' }, 
    email: 
     { message: 'Validator "custom validator here" failed for path email', 
     name: 'ValidatorError', 
     path: 'email', 
     type: 'custom validator here' }, 
    age: 
     { message: 'Validator "min" failed for path age', 
     name: 'ValidatorError', 
     path: 'age', 
     type: 'min' } } } 

后:

[ 'username is required.', 
    'state not an allowed value.', 
    'custom validator here', 
    'age below minimum.' ] 

编辑:快照,只是意识到这是一个CoffeeScript问题。不是CoffeeScript的人,我不想在CS中重写这个。您可以随时将它作为js文件加入您的CS?

+0

谢谢男人:)。我已经分叉猫鼬项目,并认为解决了这个问题。我发送[pull request](https://github.com/LearnBoost/mongoose/pull/753)给猫鼬家伙。附:这不是CoffeScript ...我只想写一些伪代码,但版主添加了CoffeScript标签:) – ManInTheBox

+0

真棒感谢的人 – Unitech

0

如果你需要得到的第一个错误信息,请看下面的例子:

var firstError = err.errors[Object.keys(err.errors)[0]]; 
return res.status(500).send(firstError.message); 

问候,李启