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

ValidatorError not catched by try/catch #14101

Closed
2 tasks done
bennaaym opened this issue Nov 20, 2023 · 3 comments · Fixed by #14128
Closed
2 tasks done

ValidatorError not catched by try/catch #14101

bennaaym opened this issue Nov 20, 2023 · 3 comments · Fixed by #14128
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@bennaaym
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

^7.5.0

Node.js version

18.18.0

MongoDB server version

MongoDB Atlas

Typescript version (if applicable)

^5.2.2

Description

The ValidatorError thrown by mongoose is not catched by try/catch block

try {
    const activity = new Activity({
      <object with invalid schema>
    });

    await activity.save();
    return activity;
  } catch (err) {
    console.log(err);
  }

Error:

ValidatorError: Path `value` is required.
    at validate (***\node_modules\mongoose\lib\schematype.js:1365:13)
    at SchemaString.SchemaType.doValidate (***\node_modules\mongoose\lib\schematype.js:1349:7)
    at ***\node_modules\mongoose\lib\document.js:2968:18
    at processTicksAndRejections (node:internal/process/task_queues:77:11)
[ERROR] 09:23:29 ValidatorError: Path `value` is required.

Steps to Reproduce

try {
    const activity = new Activity({
      <object with invalid schema>
    });

    await activity.save();
    return activity;
  } catch (err) {
    console.log(err);
  }

Expected Behavior

The error should be catched by the closest try/catch block

@bennaaym
Copy link
Author

The Validator error is not catched when there is an array of arrays .

options: {
    type: [[optionSchema]],
    required: true
  }

if there is an error in one of the element a Validator error is thrown and is not catched by the try/catch block around it

@vkarpov15 vkarpov15 added this to the 7.6.7 milestone Nov 25, 2023
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Nov 25, 2023
@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Nov 27, 2023
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const optionsSchema = new mongoose.Schema({
  val: {
    type: Number,
    required: true
  }
});

const testSchema = new mongoose.Schema({
  name: String,
  options: {
    type: [[optionsSchema]],
    required: true
  }
});



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

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  await Test.create({ name: 'Test' });
  console.log(await Test.findOne())
  console.log('done');
  
}
run();

@vkarpov15
Copy link
Collaborator

Correct repro script:

const mongoose = require('mongoose');

const optionsSchema = new mongoose.Schema({
  val: {
    type: Number,
    required: true
  }
});

const testSchema = new mongoose.Schema({
  name: String,
  options: {
    type: [[optionsSchema]],
    required: true
  }
});



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

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  await Test.create({ name: 'Test', options: [[{ val: null }]] });
  console.log(await Test.findOne())
  console.log('done');
  
}
run().catch(err => console.log('caught', err));

@vkarpov15 vkarpov15 modified the milestones: 7.6.7, 7.6.6 Nov 27, 2023
vkarpov15 added a commit that referenced this issue Nov 29, 2023
… array so nested document arrays use correct constructor

Fix #14101
vkarpov15 added a commit that referenced this issue Dec 1, 2023
fix(schema): avoid creating unnecessary clone of schematype in nested array so nested document arrays use correct constructor
vkarpov15 added a commit that referenced this issue Dec 1, 2023
… array so nested document arrays use correct constructor

Fix #14101
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.
Projects
None yet
3 participants