Comments (9)
Seems there is a problem with the regexp... :(
from mongoose-beautiful-unique-validation.
Hello! Is this problem fixed? I have a similar situation, but the output differs a little:
{
"errors": {},
"_message": "Validation failed",
"message": "Validation failed",
"name": "ValidationError"
}
My schema looks like this:
const schema = new mongoose.Schema({
// other properties
email: { type: String, required: true, unique: 'Email must be unique: ({VALUE})' },
});
schema.plugin(beautifyUnique);
from mongoose-beautiful-unique-validation.
Hello! Is this problem fixed? I have a similar situation, but the output differs a little:
{ "errors": {}, "_message": "Validation failed", "message": "Validation failed", "name": "ValidationError" }
My schema looks like this:
const schema = new mongoose.Schema({ // other properties email: { type: String, required: true, unique: 'Email must be unique: ({VALUE})' }, }); schema.plugin(beautifyUnique);
i got the same problem
from mongoose-beautiful-unique-validation.
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 MongoError
an the code as 11000
for duplicate values it should work in any version.
from mongoose-beautiful-unique-validation.
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
at ValidationError.Object..ValidationError.inspect (/node_modules/mongoose/lib/error/validation.js:59:24)
at formatValue (internal/util/inspect.js:521:31)
at inspect (internal/util/inspect.js:196:10)
at formatWithOptions (util.js:84:12)
at format (util.js:72:10)
at CustomConsole.log (/node_modules/jest-util/build/CustomConsole.js:156:41)
at Object.log (/database/user.test.js:81:17)
at processTicksAndRejections (internal/process/next_tick.js:81:5)
errors: {},
_message: 'Validation failed',
name: 'ValidationError' }
Mongoose version: 5.4.10
from mongoose-beautiful-unique-validation.
Is there any update on this issue?
from mongoose-beautiful-unique-validation.
Any Update?
from mongoose-beautiful-unique-validation.
The bug is still there <3. (at least for MySQL)
from mongoose-beautiful-unique-validation.
hey @matteodelabre are you still maintaining this library?
from mongoose-beautiful-unique-validation.
Related Issues (20)
- Unique custom message on nested objects. HOT 1
- Doesn't work with the new Mongoose 5.x RCs HOT 4
- Incorrect regex for compound keys HOT 1
- Self-referenced schemas trigger an exception
- Support for compound indexes HOT 1
- Global error HOT 1
- mongoose-beautiful-unique-validation schema tree traversal can reach Mongo driver through $$context
- Custom message detection in nested schema failed - max call stack size exceeded
- Custom message detection in nested schema failed - max call stack size exceeded
- Adding updateOne and updatedMany postHooks HOT 1
- Plugin validating unique on a non unique field
- Not working with mongoose latest ( 5.7.0 ) HOT 1
- ValidationError contains empty errors property HOT 3
- Not working with mongoose 5.9.3 HOT 2
- Are all errors supposed to be returned? HOT 1
- Doesn't work with mongoose 5.12.5 and nestjs
- Version 7.1.1 Doesn't work mongoose 6.0.9 && mongo version 4.2 HOT 1
- Working with mongoose v6 HOT 1
- Mongoose unique error is now a MongoServerError not MongoError
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoose-beautiful-unique-validation.