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

Validation failure not caught in some circumstances #10968

Closed
callumgare opened this issue Nov 10, 2021 · 2 comments
Closed

Validation failure not caught in some circumstances #10968

callumgare opened this issue Nov 10, 2021 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@callumgare
Copy link

Using the latest version of Mongoose (6.0.12), in the circumstances documented in "Test 3" document validation failures are not caught in a try/catch statement.

import mongoose from "mongoose";

mongoose.connect(process.env.MONGODB_URL, { useNewUrlParser: true });
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", async function () {
  const Schema = mongoose.Schema;

  const TestSchema = new Schema({
    url: {
      type: String,
      required: function () {
        return Boolean(new URL(this.url));
      },
    },
  });

  const Test = mongoose.model("Test", TestSchema);

  // No errors here
  try {
    console.log("Test 1");
    await Test.create({ url: "http://example.com" });
    console.log("No error");
  } catch (error) {
    console.error("Error caught:", error);
  }

  // This will fail but will be caught properly
  try {
    console.log("Test 2");
    await Test.create({ url: "invalid url" });
    console.log("No error");
  } catch (error) {
    console.error("Error caught:", error);
  }

  // This will fail but won't be caught properly
  try {
    console.log("Test 3");
    await Test.create({});
    console.log("No error");
  } catch (error) {
    console.error("Error caught:", error);
  }
});
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Nov 15, 2021
@IslandRhythms
Copy link
Collaborator

I think it has to do with new URL() as VScode is yelling at me because of it

import * as mongoose from "mongoose";

mongoose.connect('mongodb://localhost:27017/test');
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", async function () {
    console.log('open')
  const Schema = mongoose.Schema;

  const TestSchema = new Schema({
    url: {
      type: String,
      required: function () {
        return Boolean(new URL(this.url));
      },
    },
  });

  const Test = mongoose.model("Test", TestSchema);

  // No errors here
  try {
    console.log("Test 1");
    await Test.create({ url: "http://example.com" });
    console.log("No error");
  } catch (error) {
    console.error("Error caught:", error);
  }

  // This will fail but will be caught properly
  try {
    console.log("Test 2");
    await Test.create({ url: "invalid url" });
    console.log("No error");
  } catch (error) {
    console.error("Error caught:", error);
  }

  // This will fail but won't be caught properly
  try {
    console.log("Test 3");
    await Test.create({});
    console.log("No error");
  } catch (error) {
    console.error("Error caught:", error);
  }
});

@vkarpov15 vkarpov15 added this to the 6.0.14 milestone Nov 17, 2021
@callumgare
Copy link
Author

@IslandRhythms what error is VS code giving you? I’m guessing it’s complaining about ‘new URL()’ not being caught in a try/catch statement. But of course in this case we want it not to be caught because we want the statement to throw in order to ensure mongoose fails validation.

vkarpov15 added a commit that referenced this issue Nov 19, 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.
Projects
None yet
Development

No branches or pull requests

3 participants