diff --git a/lib/cast.js b/lib/cast.js index ecb96e74fdf..1cb88406de4 100644 --- a/lib/cast.js +++ b/lib/cast.js @@ -264,12 +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 : - '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); @@ -378,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; +} 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' + }); + }); });