Skip to content

Commit

Permalink
Merge pull request #13682 from Automattic/vkarpov15/gh-13626-2
Browse files Browse the repository at this point in the history
fix(document): correctly set index when casting subdocs for validation re: #13626
  • Loading branch information
vkarpov15 committed Aug 1, 2023
2 parents 622fa1c + 1a9f1b3 commit d5784d8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
23 changes: 16 additions & 7 deletions lib/document.js
Expand Up @@ -2642,7 +2642,7 @@ function _evaluateRequiredFunctions(doc) {
*/

function _getPathsToValidate(doc) {
const skipSchemaValidators = {};
const doValidateOptions = {};

_evaluateRequiredFunctions(doc);
// only validate required fields when necessary
Expand Down Expand Up @@ -2679,8 +2679,17 @@ function _getPathsToValidate(doc) {
!doc.isDirectModified(fullPathToSubdoc) &&
!doc.$isDefault(fullPathToSubdoc)) {
paths.add(fullPathToSubdoc);
if (doc.$__.pathsToScopes == null) {
doc.$__.pathsToScopes = {};
}
doc.$__.pathsToScopes[fullPathToSubdoc] = subdoc.$isDocumentArrayElement ?
subdoc.__parentArray :
subdoc.$parent();

skipSchemaValidators[fullPathToSubdoc] = true;
doValidateOptions[fullPathToSubdoc] = { skipSchemaValidators: true };
if (subdoc.$isDocumentArrayElement && subdoc.__index != null) {
doValidateOptions[fullPathToSubdoc].index = subdoc.__index;
}
}
}
}
Expand Down Expand Up @@ -2793,7 +2802,7 @@ function _getPathsToValidate(doc) {
}

paths = Array.from(paths);
return [paths, skipSchemaValidators];
return [paths, doValidateOptions];
}

/*!
Expand Down Expand Up @@ -2864,7 +2873,7 @@ Document.prototype.$__validate = function(pathsToValidate, options, callback) {
let paths = shouldValidateModifiedOnly ?
pathDetails[0].filter((path) => this.$isModified(path)) :
pathDetails[0];
const skipSchemaValidators = pathDetails[1];
const doValidateOptionsByPath = pathDetails[1];
if (typeof pathsToValidate === 'string') {
pathsToValidate = pathsToValidate.split(' ');
}
Expand Down Expand Up @@ -2937,16 +2946,16 @@ Document.prototype.$__validate = function(pathsToValidate, options, callback) {
_this;

const doValidateOptions = {
skipSchemaValidators: skipSchemaValidators[path],
...doValidateOptionsByPath[path],
path: path,
validateModifiedOnly: shouldValidateModifiedOnly
};

schemaType.doValidate(val, function(err) {
if (err) {
const isSubdoc = schemaType.$isSingleNested ||
schemaType.$isArraySubdocument ||
schemaType.$isMongooseDocumentArray;
schemaType.$isArraySubdocument ||
schemaType.$isMongooseDocumentArray;
if (isSubdoc && err instanceof ValidationError) {
return --total || complete();
}
Expand Down
10 changes: 5 additions & 5 deletions test/schema.validation.test.js
Expand Up @@ -1233,7 +1233,7 @@ describe('schema', function() {
});
});

it('enums on arrays (gh-6102) (gh-8449)', function() {
it('enums on arrays (gh-6102) (gh-8449)', async function() {
assert.throws(function() {
new Schema({
array: {
Expand Down Expand Up @@ -1264,10 +1264,10 @@ describe('schema', function() {
Model = mongoose.model('gh6102', MySchema);
const doc2 = new Model({ array: [1, 2, 3] });

return doc1.validate().
then(() => assert.ok(false), err => assert.equal(err.name, 'ValidationError')).
then(() => doc2.validate()).
then(() => assert.ok(false), err => assert.equal(err.name, 'ValidationError'));
let err = await doc1.validate().then(() => null, err => err);
assert.equal(err.name, 'ValidationError');
err = await doc2.validate().then(() => null, err => err);
assert.equal(err.name, 'ValidationError', err);
});

it('skips conditional required (gh-3539)', function(done) {
Expand Down

0 comments on commit d5784d8

Please sign in to comment.