From a455d4a95df4c5b575e77a73e699bdbafe06331a Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 19 Oct 2022 16:52:05 -0400 Subject: [PATCH 1/2] fix(cast): make schema-level strictQuery override schema-level strict for query filters Fix #12508 --- lib/cast.js | 7 +++++-- test/cast.test.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/cast.js b/lib/cast.js index ecb96e74fdf..e38739ab5fc 100644 --- a/lib/cast.js +++ b/lib/cast.js @@ -268,8 +268,11 @@ module.exports = function cast(schema, obj, options, context) { options.strictQuery : 'strict' in options ? options.strict : - 'strict' in schema._userProvidedOptions ? schema._userProvidedOptions.strict : - schema.options.strictQuery; + 'strictQuery' in schema._userProvidedOptions ? + schema._userProvidedOptions.strictQuery : + 'strict' in schema._userProvidedOptions ? + schema._userProvidedOptions.strict : + schema.options.strictQuery; if (options.upsert && strict) { if (strict === 'throw') { throw new StrictModeError(path); diff --git a/test/cast.test.js b/test/cast.test.js index d0dea887104..51b97518ff3 100644 --- a/test/cast.test.js +++ b/test/cast.test.js @@ -178,4 +178,19 @@ describe('cast: ', function() { roles: { $ne: 'super' } }); }); + + it('uses schema-level strictQuery over schema-level strict (gh-12508)', function() { + const schema = new Schema({}, { + strict: 'throw', + strictQuery: false + }); + + const res = cast(schema, { + name: 'foo' + }); + + assert.deepEqual(res, { + name: 'foo' + }); + }); }); From d59554479e5e538ddc8a535adeae446b0983bbcf Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 20 Oct 2022 13:44:14 -0400 Subject: [PATCH 2/2] refactor: create separate helper to get strictQuery re: code review comments --- lib/cast.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/cast.js b/lib/cast.js index e38739ab5fc..1cb88406de4 100644 --- a/lib/cast.js +++ b/lib/cast.js @@ -264,15 +264,7 @@ module.exports = function cast(schema, obj, options, context) { } const strict = 'strict' in options ? options.strict : schema.options.strict; - const strictQuery = 'strictQuery' in options ? - options.strictQuery : - 'strict' in options ? - options.strict : - 'strictQuery' in schema._userProvidedOptions ? - schema._userProvidedOptions.strictQuery : - 'strict' in schema._userProvidedOptions ? - schema._userProvidedOptions.strict : - schema.options.strictQuery; + const strictQuery = getStrictQuery(options, schema._userProvidedOptions, schema.options); if (options.upsert && strict) { if (strict === 'throw') { throw new StrictModeError(path); @@ -381,3 +373,19 @@ function _cast(val, numbertype, context) { } } } + +function getStrictQuery(queryOptions, schemaUserProvidedOptions, schemaOptions) { + if ('strictQuery' in queryOptions) { + return queryOptions.strictQuery; + } + if ('strict' in queryOptions) { + return queryOptions.strict; + } + if ('strictQuery' in schemaUserProvidedOptions) { + return schemaUserProvidedOptions.strictQuery; + } + if ('strict' in schemaUserProvidedOptions) { + return schemaUserProvidedOptions.strict; + } + return schemaOptions.strictQuery; +}