How can I validate a record only without saving in nodejs | sailsjs | waterline

孤者浪人 提交于 2020-01-15 08:41:26

问题


I seek something of this nature

//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'});

Problem is, it doesn't work.. it complains about beforeValidate not being available, e.t.c


回答1:


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
  }
});


来源:https://stackoverflow.com/questions/25824967/how-can-i-validate-a-record-only-without-saving-in-nodejs-sailsjs-waterline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!