2012-01-24 43 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); 

問候,李啓