Skip to content

Commit

Permalink
fix(document): skip validating array elements that aren't modified wh…
Browse files Browse the repository at this point in the history
…en `validateModifiedOnly` is set

Fix #9963
  • Loading branch information
vkarpov15 committed Mar 2, 2021
1 parent 91581d5 commit 9896ee2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -2500,7 +2500,8 @@ Document.prototype.$__validate = function(pathsToValidate, options, callback) {

const doValidateOptions = {
skipSchemaValidators: skipSchemaValidators[path],
path: path
path: path,
validateModifiedOnly: shouldValidateModifiedOnly
};
schemaType.doValidate(val, function(err) {
if (err && (!schemaType.$isMongooseDocumentArray || err.$isArrayValidatorError)) {
Expand Down Expand Up @@ -2626,7 +2627,8 @@ Document.prototype.validateSync = function(pathsToValidate, options) {
const val = _this.$__getValue(path);
const err = p.doValidateSync(val, _this, {
skipSchemaValidators: skipSchemaValidators[path],
path: path
path: path,
validateModifiedOnly: shouldValidateModifiedOnly
});
if (err && (!p.$isMongooseDocumentArray || err.$isArrayValidatorError)) {
if (p.$isSingleNested &&
Expand Down
11 changes: 10 additions & 1 deletion lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ DocumentArrayPath.prototype.doValidate = function(array, fn, scope, options) {
doc = array[i] = new Constructor(doc, array, undefined, undefined, i);
}

if (options.validateModifiedOnly && !doc.isModified()) {
--count || fn(error);
continue;
}

doc.$__validate(callback);
}
}
Expand All @@ -267,7 +272,7 @@ DocumentArrayPath.prototype.doValidate = function(array, fn, scope, options) {
* @api private
*/

DocumentArrayPath.prototype.doValidateSync = function(array, scope) {
DocumentArrayPath.prototype.doValidateSync = function(array, scope, options) {
const schemaTypeError = SchemaType.prototype.doValidateSync.call(this, array, scope);
if (schemaTypeError != null) {
schemaTypeError.$isArrayValidatorError = true;
Expand Down Expand Up @@ -299,6 +304,10 @@ DocumentArrayPath.prototype.doValidateSync = function(array, scope) {
doc = array[i] = new Constructor(doc, array, undefined, undefined, i);
}

if (options != null && options.validateModifiedOnly && !doc.isModified()) {
continue;
}

const subdocValidateError = doc.validateSync();

if (subdocValidateError && resultError == null) {
Expand Down

0 comments on commit 9896ee2

Please sign in to comment.