diff --git a/lib/helpers/common.js b/lib/helpers/common.js index b0a8f1b1fd9..3398f4cbe09 100644 --- a/lib/helpers/common.js +++ b/lib/helpers/common.js @@ -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]; } diff --git a/lib/helpers/updateValidators.js b/lib/helpers/updateValidators.js index 6b68ebc9b99..199ef0677f7 100644 --- a/lib/helpers/updateValidators.js +++ b/lib/helpers/updateValidators.js @@ -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; @@ -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) { @@ -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); } @@ -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) {