我正在验证来自下面的另一个模型的模型
Model.addFavorite = function (data, callbackFn) {
if (data) {
var faviroteModel = this.app.models.Favorite;
var objFavorite = new faviroteModel(data);
objFavorite.isValid(function (isValid) {
if (isValid) {
callbackFn(null, objFavorite);
}
else {
callbackFn(objFavorite.errors);
}
});
}
else callbackFn("Post data required", {});
}
如果我这样做,那么我会收到如下错误
{
"error": {
"statusCode": 500,
"t": [
"is not a valid date"
]
}
}
应该出现如下错误消息
{
"error": {
"statusCode": 422,
"name": "ValidationError",
"message": "The `Favorite` instance is not valid. Details: `t` is not a valid date (value: Invalid Date).",
"details": {
"context": "Favorite",
"codes": {
"t": [
"date"
]
},
"messages": {
"t": [
"is not a valid date"
]
}
}
}
}
谁能告诉我这里缺少什么。
我怎样才能实现这一点。
答案 0 :(得分:1)
https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/validations.js#L843
您可能遇到需要提出验证的情况 错误自己,例如在"之前"钩子或自定义模型 方法
if (model.isValid()) {
return callback(null, { success: true });
}
// This line shows how to create a ValidationError
var err = new MyModel.ValidationError(model);
callback(err);
}