Skip to content

Commit

Permalink
Merge pull request #12717 from lpizzinidev/gh-12703
Browse files Browse the repository at this point in the history
fix: setting global `strictQuery` after Schema creation wasn't working
  • Loading branch information
vkarpov15 committed Nov 23, 2022
2 parents 966eebe + 22074a3 commit e6bd823
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/cast.js
Expand Up @@ -264,7 +264,7 @@ module.exports = function cast(schema, obj, options, context) {
}

const strict = 'strict' in options ? options.strict : schema.options.strict;
const strictQuery = getStrictQuery(options, schema._userProvidedOptions, schema.options);
const strictQuery = getStrictQuery(options, schema._userProvidedOptions, schema.options, context);
if (options.upsert && strict) {
if (strict === 'throw') {
throw new StrictModeError(path);
Expand Down Expand Up @@ -374,7 +374,7 @@ function _cast(val, numbertype, context) {
}
}

function getStrictQuery(queryOptions, schemaUserProvidedOptions, schemaOptions) {
function getStrictQuery(queryOptions, schemaUserProvidedOptions, schemaOptions, context) {
if ('strictQuery' in queryOptions) {
return queryOptions.strictQuery;
}
Expand All @@ -387,5 +387,18 @@ function getStrictQuery(queryOptions, schemaUserProvidedOptions, schemaOptions)
if ('strict' in schemaUserProvidedOptions) {
return schemaUserProvidedOptions.strict;
}
if (
context.mongooseCollection &&
context.mongooseCollection.conn &&
context.mongooseCollection.conn.base &&
context.mongooseCollection.conn.base.options) {
const mongooseOptions = context.mongooseCollection.conn.base.options;
if ('strictQuery' in mongooseOptions) {
return mongooseOptions.strictQuery;
}
if ('strict' in mongooseOptions) {
return mongooseOptions.strict;
}
}
return schemaOptions.strictQuery;
}
43 changes: 43 additions & 0 deletions test/query.test.js
Expand Up @@ -4263,4 +4263,47 @@ describe('Query', function() {
);
}, { message: 'Can\'t modify discriminator key "animals.kind" on discriminator model' });
});

it('global strictQuery should work if applied after schema creation (gh-12703)', async() => {
const m = new mongoose.Mongoose();

await m.connect(start.uri);

const schema = new mongoose.Schema({ title: String });

const Test = m.model('test', schema);

m.set('strictQuery', false);

await Test.create({
title: 'chimichanga'
});
await Test.create({
title: 'burrito bowl'
});
await Test.create({
title: 'taco supreme'
});

const cond = {
$or: [
{
title: {
$regex: 'urri',
$options: 'i'
}
},
{
name: {
$regex: 'urri',
$options: 'i'
}
}
]
};

const found = await Test.find(cond);
assert.strictEqual(found.length, 1);
assert.strictEqual(found[0].title, 'burrito bowl');
});
});

0 comments on commit e6bd823

Please sign in to comment.