-
Notifications
You must be signed in to change notification settings - Fork 38
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
Custom message not working #54
Comments
Seems there is a problem with the regexp... :( |
Hello! Is this problem fixed? I have a similar situation, but the output differs a little:
My schema looks like this:
|
i got the same problem |
I did tries with different version of this plugin and the issue seems to be present in mongo version 3.6 to up, with mongo 3.4 works!! but my solution was drop the plugin and focus in parse the error throwed by mongodb and so base on the example provide in the mongoosejs 5 website i did this code mimic the exact same response of this plugin. UsersSchema.post("save", function (error, doc, next) {
let duplicateError = error.name === "MongoError" && error.code === 11000;
if (duplicateError) {
let result = {
name: "ValidationError",
message: "Model validation failed",
errors: {}
}
//Object.keys(this._doc) = ["email", "name", "password", ......]
Object.keys(this._doc).forEach(path => {
let isDuplicate = error.errmsg.includes(`$${path}_1`);//match with $email_1
//Check if any attribute of the document is included in the error message as an unique field example:
//11000 E11000 duplicate key error index: mydb.users.$email_1 dup key: { : "[email protected]" }
if (isDuplicate) {
let value = this._doc[path];
result.errors[path] = {
name: "ValidatorError",
kind: "unique",
message: `Path ${path} (${value}) is not unique.`,
path,
value
}
}
})
next(result);
} else {
next()
}
}) So as long mongoose return the error name as |
Having the same problem here in my tests using mongodb-memory-server at least. Instead of returning my unique message it just returns. { ValidationError: Validation failed Mongoose version: 5.4.10 |
Is there any update on this issue? |
Any Update? |
The bug is still there <3. (at least for MySQL) |
hey @matteodelabre are you still maintaining this library? |
Hello,
I have the following schema:
When I try to create a document with a duplicate e-mail, I don't get the custom message, instead I only get the following:
I am using mongoose 5.0.13 and mongoose-beautiful-unique-validation 7.1.1.
Am I missing anything?
The text was updated successfully, but these errors were encountered: