Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/helpers/updateValidators.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ module.exports = function(query, schema, castedDoc, options, callback) {
return callback(null);
}
}
if (schemaPath.$isSingleNested) {
Comment thread
vkarpov15 marked this conversation as resolved.
alreadyValidated.push(updates[i]);
}

schemaPath.doValidate(v, function(err) {
if (schemaPath.schema != null &&
Expand Down Expand Up @@ -246,4 +249,3 @@ module.exports = function(query, schema, castedDoc, options, callback) {
};
}
};

46 changes: 46 additions & 0 deletions test/model.updateOne.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3143,6 +3143,52 @@ describe('model: updateOne: ', function() {
assert.equal(r2.testArray[0].key, 'Type2');
assert.equal(r2.testArray[0].field2, field2update);
});

it('only calls validators under single nested subdocs once (gh-15436)', async function() {
let validateDetailsCalls = 0;
let validateNameCalls = 0;

const kittySchema = new mongoose.Schema({
informations: {
type: {
details: {
validate: function() {
validateDetailsCalls++;
return true;
},
type: {
name: {
type: String,
validate: function() {
validateNameCalls++;
return true;
}
}
}
}
}
}
});

const Kitten = db.model('Test', kittySchema);

// Update the document with validation enabled
await Kitten.updateOne(
{ _id: new mongoose.Types.ObjectId() },
{
informations: {
details: {
name: 'Zohra'
}
}
},
{ runValidators: true }
);

// Assert that each validator was only called once
assert.equal(validateDetailsCalls, 1);
assert.equal(validateNameCalls, 1);
});
});

async function delay(ms) {
Expand Down
23 changes: 23 additions & 0 deletions test/types/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1010,3 +1010,26 @@ async function gh15369() {
throw error;
}
}

async function gh15437() {
interface Person {
name: string;
age: number;
address: string;
}

const schema = new mongoose.Schema({
name: String,
age: Number,
address: String
});
const PersonModel = model<Person>('Person', schema);

const data = { name: 'John Doe', age: 30, address: '123 Main St' };

// Test hydrating with string projection
const doc1 = PersonModel.hydrate(data, 'name age');
expectType<string>(doc1.name);
expectType<number>(doc1.age);
expectAssignable<undefined | null | string>(doc1.address);
}
2 changes: 1 addition & 1 deletion types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ declare module 'mongoose' {
* Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
* The document returned has no paths marked as modified initially.
*/
hydrate(obj: any, projection?: AnyObject, options?: HydrateOptions): THydratedDocumentType;
hydrate(obj: any, projection?: ProjectionType<TRawDocType>, options?: HydrateOptions): THydratedDocumentType;

/**
* This function is responsible for building [indexes](https://www.mongodb.com/docs/manual/indexes/),
Expand Down