Skip to content

Commit

Permalink
Merge pull request #12570 from Automattic/vkarpov15/gh-12508
Browse files Browse the repository at this point in the history
fix(cast): make schema-level strictQuery override schema-level strict for query filters
  • Loading branch information
vkarpov15 committed Oct 20, 2022
2 parents 7fb5114 + d595544 commit dcbff09
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/cast.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
15 changes: 15 additions & 0 deletions test/cast.test.js
Expand Up @@ -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'
});
});
});

0 comments on commit dcbff09

Please sign in to comment.