Skip to content

Commit

Permalink
Merge pull request #8192 from birdofpreyru/fix-8093-1
Browse files Browse the repository at this point in the history
[#8093] Fixes performance of update validator, and flatten function logic
  • Loading branch information
vkarpov15 committed Sep 26, 2019
2 parents c371500 + c76e062 commit da77b8d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
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

0 comments on commit da77b8d

Please sign in to comment.