Created
March 24, 2019 18:11
-
-
Save deepal/f313396230cf3afb32e2b3c6ccd7ee82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| schema.pre('findOneAndUpdate', function (next) { | |
| const MongooseValidationError = mongoose.Error.ValidationError, | |
| model = this.model(), | |
| immutableFields = ['email', 'password'], | |
| update = this.getUpdate(); | |
| immutableFields.forEach((field) => { | |
| if (update.hasOwnProperty(field)) { | |
| model.invalidate(field, `${field} cannot be modified`, update[field], 'immutable'); | |
| } | |
| }); | |
| if (model.errors) { | |
| const validationError = new MongooseValidationError(null); | |
| validationError.errors = model.errors; | |
| return next(validationError); | |
| } | |
| return next(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment