Validating fields that depend on each other in Keystone.js Validating fields that depend on each other in Keystone.js mongoose mongoose

Validating fields that depend on each other in Keystone.js


You shouldn't be ! negating the second part of your validation condition, as you're currently flagging a validation error when it's not empty.

So change it to:

Sample.schema.pre('validate', function(next) {    if (this.isPublished && _.isEmpty(this.thumbnailImage.image)) {        next(Error('Thumbnail Image is required when publishing a sample'));    }    else {        next();    }});

Note that you also need to wrap your error string in an Error object when calling next to report a validation failure.