【问题标题】:How can I validate a record only without saving in nodejs | sailsjs | waterline如何只验证记录而不保存在 nodejs |帆 |吃水线
【发布时间】:2014-09-13 15:55:52
【问题描述】:

我寻求这种性质的东西

//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 不可用,等等

【问题讨论】:

  • 你不能构建一个Recipe实例,并且根本不调用它的保存函数吗?

标签: javascript node.js sails.js waterline sails-mongo


【解决方案1】:

您需要将回调传递给.validate:

Recipe.validate({age: 'blah'}, function(err){
  if (err && err.invalidAttributes) {
    console.log(err.invalidAttributes); 
  } else {
    // model is valid
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 2014-01-11
    相关资源
    最近更新 更多