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

validate doesn't throw any error with nested object in schema #7143

Closed
abarriel opened this issue Oct 17, 2018 · 2 comments
Closed

validate doesn't throw any error with nested object in schema #7143

abarriel opened this issue Oct 17, 2018 · 2 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@abarriel
Copy link
Contributor

abarriel commented Oct 17, 2018

Hey!
I am wondering why the script below doesn't throw any validation error.
I set strict to throw, if I tried to add a key who doesn't exist in the schema it should throw an error which is not the case here.

Solution Found:
If I remove the required for jerome, it works fine. I think the issue may come in the way mongoose handle nested object, for example in this case if I create a subSchema like that, it's works fine:

   jerome: {
      type: new Schema({
        ayoub: String,
      }, { strict: throw }),
      required: true,
    },

standalone strict:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const TestSchema = new Schema(
  {
    jerome: {
      type: {
        ayoub: String,
      },
      required: true,
    },
  },
  { strict: "throw", timestamps: true },
);

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

mongoose.connect("mongodb://localhost:27017/test");

const data = {
  jerome: {
    ayoub: "string",
    fake: "D",
  },
};
// should throw an error
async function main() {
  const res = await Test.create(data);
  console.log(res);
}
main()
  .then(() => process.exit(0))
  .catch((err) => {
	console.log(err);
	process.exit(1)
});

"mongoose": "5.2.18",
nodejs version: v10.6.0

Thank you 👍

@abarriel abarriel changed the title bug validate doesn't throw any error with nested object Oct 17, 2018
@abarriel abarriel changed the title validate doesn't throw any error with nested object validate doesn't throw any error with nested object in schema Oct 17, 2018
@vkarpov15 vkarpov15 added this to the 5.3.6 milestone Oct 18, 2018
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Oct 18, 2018
@vkarpov15
Copy link
Collaborator

This looks like a bug, will investigate ASAP

@vkarpov15
Copy link
Collaborator

Upon closer investigation, this isn't a bug. When you do type: { ayoub: String }, Mongoose interprets it as type: Mixed. We'll add more details about this to the FAQ. The below is how you should do this:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const TestSchema = new Schema(
  {
    jerome: {
      ayoub: { type: String, required: true }
    },
  },
  { strict: "throw", timestamps: true },
);

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

mongoose.connect("mongodb://localhost:27017/test");

const data = {
  jerome: {
    ayoub: "string",
    fake: "D",
  },
};
// should throw an error
async function main() {
  const res = await Test.create(data);
  console.log(res);
}
main()
  .then(() => process.exit(0))
  .catch((err) => {
        console.log(err);
        process.exit(1)
});

@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Oct 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

2 participants