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

Option strict is used before Schema and global level strictQuery #12508

Closed
2 tasks done
abarriel opened this issue Oct 3, 2022 · 3 comments · Fixed by #12570
Closed
2 tasks done

Option strict is used before Schema and global level strictQuery #12508

abarriel opened this issue Oct 3, 2022 · 3 comments · Fixed by #12570
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@abarriel
Copy link
Contributor

abarriel commented Oct 3, 2022

Prerequisites

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

Mongoose version

6.63

Node.js version

16

MongoDB server version

4.9.1

Description

Hello, when strict is set to throw, I cannot set strictQuery to anything else then throw.
On docs, it says
However, strictQuery is tied to strict by default. This means that, by default, Mongoose will filter out query filter properties that are not in the schema.
I cannot untied strictQuery and Query globally on in shcema option, I have to use QueryOption.
This is the expected Behavior ? the doc is unclear.

Steps to Reproduce

const mongoose = require("mongoose");

mongoose.set("debug", true);
mongoose.set("strictQuery", false);
const options = {
  strict: "throw",
  strictQuery: false
};

const eventSchema = new mongoose.Schema({ time: Date }, options);
const Event = mongoose.model("Event", eventSchema);

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

  // 1. Not working
  await Event.find({ url: "google.com" },
}

run();

Expected Behavior

Should be able to set strictQuery to anything else then throw in schema option or globally not only in query opt

@abarriel
Copy link
Contributor Author

abarriel commented Oct 3, 2022

mongoose/lib/cast.js:283:17

     const strictQuery = 'strictQuery' in options ?
        options.strictQuery :
        'strict' in options ?
        options.strict :
        'strict' in schema._userProvidedOptions ? schema._userProvidedOptions.strict :
        schema.options.strictQuery;

It should be like that imo ( strictQuery always as the priority then strict )

strict in options and strictQuery is not set then strictQuery to strict
strict in options and strictQuery is set then strictQuery to option.strictQuery
strict is not in options and strictQuery is set than strictQuery
sitrct is not in options and strictQuery is not set than default lib mongoose strictQuery

@hasezoey hasezoey changed the title Untied strict and strictQuery options Option strict is used before Schema and global level strictQuery Oct 4, 2022
@hasezoey
Copy link
Collaborator

hasezoey commented Oct 4, 2022

Updated title to match issue, because function-level strictQuery would overwrite strict from anywhere from what i can tell

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Oct 10, 2022
@IslandRhythms
Copy link
Collaborator

const mongoose = require("mongoose");

mongoose.set("debug", true);
mongoose.set("strictQuery", false);
const options = {
  strict: "throw",
  strictQuery: false
};

const eventSchema = new mongoose.Schema({ time: Date }, options);
const Event = mongoose.model("Event", eventSchema);

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

  // 1. Not working
  const res = await Event.find({ url: "google.com" });
}

run();

@vkarpov15 vkarpov15 added this to the 6.6.6 milestone Oct 17, 2022
vkarpov15 added a commit that referenced this issue Oct 19, 2022
vkarpov15 added a commit that referenced this issue Oct 20, 2022
fix(cast): make schema-level strictQuery override schema-level strict for query filters
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
4 participants