Code Monkey home page Code Monkey logo

Comments (9)

chumager avatar chumager commented on June 19, 2024

Seems there is a problem with the regexp... :(

from mongoose-beautiful-unique-validation.

NoMercy235 avatar NoMercy235 commented on June 19, 2024

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.

Maxtermax avatar Maxtermax commented on June 19, 2024

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.

Maxtermax avatar Maxtermax commented on June 19, 2024

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.

ntrabue avatar ntrabue commented on June 19, 2024

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.

AdamBCo avatar AdamBCo commented on June 19, 2024

Is there any update on this issue?

from mongoose-beautiful-unique-validation.

trylovetom avatar trylovetom commented on June 19, 2024

Any Update?

from mongoose-beautiful-unique-validation.

TheMarshalMole avatar TheMarshalMole commented on June 19, 2024

The bug is still there <3. (at least for MySQL)

from mongoose-beautiful-unique-validation.

tubbo avatar tubbo commented on June 19, 2024

hey @matteodelabre are you still maintaining this library?

from mongoose-beautiful-unique-validation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.