New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return ValidationError from post findOneAndUpdate middleware does not contain message #6171
Comments
hi @jeremyml. From what I can tell, it looks like @vkarpov15 was just helping with the async portion of the hook you suggested. After looking at the source for ValidationError, it would appear that if you give it a string instead of an instance of a model, your message will only ever be 'Validation failed'
|
If you can live with an instance of Error the following works:
output:
|
@jeremyml you're right, my example was incorrect schema.post('findOneAndUpdate', (result, next) =>{
if (result.docField == 'invalidValue'){
var validationError = new mongoose.Error.ValidationError(null);
validationError.addError('docField', new mongoose.Error.ValidatorError({ message: 'Invalid' }));
return next(validationError);
}
// ...
}); A ValidationError is a mapping of paths to ValidatorError instances, so you need to add one of those |
@vkarpov15 suggested the code below in this comment
But the
ValidationError
does not contain the custom messageInstead the message is simply the canned message
This is the ValidationError in string from:
I tested it in v 4.13.8 and v 5.0.7
The text was updated successfully, but these errors were encountered: