Skip to content
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

error name is missing #10166

Closed
hasinoorit opened this issue Apr 21, 2021 · 7 comments · Fixed by #10210
Closed

error name is missing #10166

hasinoorit opened this issue Apr 21, 2021 · 7 comments · Fixed by #10210
Assignees
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone

Comments

@hasinoorit
Copy link

Do you want to request a feature or report a bug?
Bug

What is the current behavior?

Error name is missing for CastError

If the current behavior is a bug, please provide the steps to reproduce.

My Schema code is
const contentSchema = Schema({ text: { type: String, required: [true, "Text is required"] }, number: { type: Number, required: [true, "Number is required"] }, })
My Parameters are
{ "text": false, "number": "fsfsf" }

What is the expected behavior?
err.errors['number'].name should show 'CastError'

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

  1. Node Version: 14.16.0
  2. mongooser version: 5.12.4
  3. mongodb version: 3.6.8
@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Apr 21, 2021
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');
const {Schema} = mongoose;

const testSchema = new Schema({text: {type:String, required: [true, "Text is required"]}, number: {
    type: Number, required: [true, "Number is required"]}, });

    const Test = new mongoose.model('Test', testSchema);

    async function run() {
        await mongoose.connect('mongodb://localhost:27017/test', {
            useNewUrlParser: true,
            useUnifiedTopology: true
          });
        
        await mongoose.connection.dropDatabase();

        const entry = await Test.create({"text": false, "number": "fsfsf"});
        await entry.save();
    }
    run();

@IslandRhythms
Copy link
Collaborator

try new mongoose.Schema instead of Schema

@hasinoorit
Copy link
Author

Code to reproduce

const mongoose = require("mongoose")
const {Schema} = mongoose

const testSchema = new Schema({text: {type:String, required: [true, "Text is required"]}, number: {
  type: Number, required: [true, "Number is required"]}, })

const Test = new mongoose.model("Test", testSchema)

async function run() {
  try {
    await mongoose.connect("mongodb://localhost:27017/test", {
      useNewUrlParser: true,
      useUnifiedTopology: true
    })
        
    await mongoose.connection.dropDatabase()

    const entry = await Test.create({"text": false, "number": "fsfsf"})
    await entry.save()
  } catch (error) {
    console.log(JSON.parse(JSON.stringify(error)))
  }
}
run()

Current output

{
  errors: {
    number: {
      stringValue: '"fsfsf"',
      kind: 'Number',
      value: 'fsfsf',
      path: 'number',
      reason: [Object]
    }
  },
  _message: 'Test validation failed',
  name: 'ValidationError',
  message: 'Test validation failed: number: Cast to Number failed for value "fsfsf" at path "number"'
}

@IslandRhythms
Copy link
Collaborator

Why are you expecting it to show a cast error vs a validation error? Number is expecting a number but you are passing a string.

@hasinoorit
Copy link
Author

Why are you expecting it to show a cast error vs a validation error? Number is expecting a number but you are passing a string.

According to documention https://mongoosejs.com/docs/validation.html#cast-errors
errors['number'] should have "message" & "name" property. But these properties are missing. I need these property for developing a plugin.

@IslandRhythms
Copy link
Collaborator

So it's possible the documentation could be outdated, I'll need to go confirm that. However, in your output, there is a name and message property, it is just not in the exact place the documentation says so in the meantime you can use that.

@hasinoorit
Copy link
Author

So it's possible the documentation could be outdated, I'll need to go confirm that. However, in your output, there is a name and message property, it is just not in the exact place the documentation says so in the meantime you can use that.

That name & message property are not of field level error. Document is not outdated, indeed its updated. My old plugin is also using these property for setting custom CastError message, and unique error message. I am using my old plugin with mongoose 5.9.25. Its definitely a new bug.

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Apr 23, 2021
@vkarpov15 vkarpov15 added this to the 5.12.8 milestone Apr 27, 2021
@IslandRhythms IslandRhythms linked a pull request May 6, 2021 that will close this issue
vkarpov15 added a commit that referenced this issue May 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants