Reputation: 932
How can I define some validation rules depending on some conditions in request body.
For example, I want to validate that post description field is set only if the post is published (isPublished flag equals true), something like:
module.exports = function(Post) {
if(req.body.isPublished === true) {
Post.validatesPresenceOf('description');
}
}
Upvotes: 2
Views: 419
Reputation: 76
Simply, you can use options
parameter
Post.validatesPresenceOf('description', {if: 'isPublished'});
Ref: #validatable-validatespresenceof
Upvotes: 2
Reputation: 1152
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();
})
Upvotes: 2