如何在不保存nodejs |的情况下验证记录sailsjs |水线
问题描述:
我寻求这种性质如何在不保存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等
答
你需要一个回调传递到.validate
:
Recipe.validate({age: 'blah'}, function(err){
if (err && err.invalidAttributes) {
console.log(err.invalidAttributes);
} else {
// model is valid
}
});
难道你不能建立一个食谱实例,并且根本就不调用它的保存函数吗? – 2014-09-13 16:25:14