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

fix(cast): make schema-level strictQuery override schema-level strict for query filters #12570

Merged
merged 3 commits into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'
});
});
});