环回条件数据验证
Loopback conditional data validation
如何根据请求正文中的某些条件定义一些验证规则。
例如,我想验证仅当 post 已发布(isPublished 标志等于 true)时才设置 post 描述字段,例如:
module.exports = function(Post) {
if(req.body.isPublished === true) {
Post.validatesPresenceOf('description');
}
}
May be you are looking for something like this
Post.observe('before save',(ctx,next)=>{
//if post is created
if(ctx.isNewInstance) {
if(ctx.instance.isPublished)
Post.validatesPresenceOf('description');
}
//if post is updated
else{
if(ctx.data.isPublished)
Post.validatesPresenceOf('description');
}
return next();
})
简单地说,你可以使用options
参数
Post.validatesPresenceOf('description', {if: 'isPublished'});
如何根据请求正文中的某些条件定义一些验证规则。
例如,我想验证仅当 post 已发布(isPublished 标志等于 true)时才设置 post 描述字段,例如:
module.exports = function(Post) {
if(req.body.isPublished === true) {
Post.validatesPresenceOf('description');
}
}
May be you are looking for something like this
Post.observe('before save',(ctx,next)=>{
//if post is created
if(ctx.isNewInstance) {
if(ctx.instance.isPublished)
Post.validatesPresenceOf('description');
}
//if post is updated
else{
if(ctx.data.isPublished)
Post.validatesPresenceOf('description');
}
return next();
})
简单地说,你可以使用options
参数
Post.validatesPresenceOf('description', {if: 'isPublished'});