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

[#8093] Fixes performance of update validator, and flatten function logic #8192

Merged
merged 2 commits into from Sep 26, 2019
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
7 changes: 3 additions & 4 deletions lib/helpers/common.js
Expand Up @@ -33,15 +33,14 @@ function flatten(update, path, options, schema) {
result[path + key] = val;

// Avoid going into mixed paths if schema is specified
if (schema != null && schema.paths[path + key] != null && schema.paths[path + key].instance === 'Mixed') {
continue;
}
const keySchema = schema && schema.path && schema.path(path + key);
if (keySchema && keySchema.instance === 'Mixed') continue;

if (shouldFlatten(val)) {
if (options && options.skipArrays && Array.isArray(val)) {
continue;
}
const flat = flatten(val, path + key, options);
const flat = flatten(val, path + key, options, schema);
for (const k in flat) {
result[k] = flat[k];
}
Expand Down
11 changes: 2 additions & 9 deletions lib/helpers/updateValidators.js
Expand Up @@ -4,7 +4,6 @@
* Module dependencies.
*/

const Mixed = require('../schema/mixed');
const ValidationError = require('../error/validation');
const cleanPositionalOperators = require('./schema/cleanPositionalOperators');
const flatten = require('./common').flatten;
Expand Down Expand Up @@ -54,7 +53,7 @@ module.exports = function(query, schema, castedDoc, options, callback) {
continue;
}
modifiedPaths(castedDoc[keys[i]], '', modified);
const flat = flatten(castedDoc[keys[i]]);
const flat = flatten(castedDoc[keys[i]], null, null, schema);
const paths = Object.keys(flat);
const numPaths = paths.length;
for (let j = 0; j < numPaths; ++j) {
Expand All @@ -79,7 +78,7 @@ module.exports = function(query, schema, castedDoc, options, callback) {

if (!hasDollarUpdate) {
modifiedPaths(castedDoc, '', modified);
updatedValues = flatten(castedDoc);
updatedValues = flatten(castedDoc, null, null, schema);
updatedKeys = Object.keys(updatedValues);
}

Expand All @@ -97,12 +96,6 @@ module.exports = function(query, schema, castedDoc, options, callback) {
return;
}

// gh-4305: `_getSchema()` will report all sub-fields of a 'Mixed' path
// as 'Mixed', so avoid double validating them.
if (schemaPath instanceof Mixed && schemaPath.path !== updates[i]) {
return;
}

if (v && Array.isArray(v.$in)) {
v.$in.forEach((v, i) => {
validatorsToExecute.push(function(callback) {
Expand Down