本文介绍了如何仅在不保存 nodejs 的情况下验证记录 |sailsjs |吃水线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我寻求这种性质的东西
//validation rules in model "User"
attributes: {
age: {
required: true,
type: 'numeric'
}
},
//now in controller, i want to be able to do this
Recipe.validate({age: 'An invalid age because it is a string. I except a validation error as response'});
问题是,它不起作用..它抱怨 beforeValidate 不可用,例如
Problem is, it doesn't work.. it complains about beforeValidate not being available, e.t.c
推荐答案
您需要将回调传递到 .validate
中:
You need to pass a callback into .validate
:
Recipe.validate({age: 'blah'}, function(err){
if (err && err.invalidAttributes) {
console.log(err.invalidAttributes);
} else {
// model is valid
}
});
这篇关于如何仅在不保存 nodejs 的情况下验证记录 |sailsjs |吃水线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!