Validation in pre save or at least after all other validation

5 views
Skip to first unread message

Matthias Tylkowski

unread,
Jun 8, 2015, 4:59:58 AM6/8/15
to mongoo...@googlegroups.com
What is the best way to validate a value after the automatic validation is done?
I have a model (Project) and I want that the isPublic property can only be set true when some other properties are filled.
 I could check the values in pre save but, I don't  know how to trigger a validation error from there.

Matthias Tylkowski

unread,
Jun 8, 2015, 5:34:07 AM6/8/15
to mongoo...@googlegroups.com
My solution now is:

Project.schema.pre('save', function(next) {

    this.isCompleted = this.checkCompleted();
    if(this.isCompleted === false && this.isPublic === true) {

        this.isPublic = false;
        this.invalidate('isPublic', 'The Project can not be set public if not all required information have been filled', this.isPublic);

        return next(this.$__.validationError);
    }

    next();
});
Reply all
Reply to author
Forward
0 new messages