-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pre('validate') hook does not trigger when using set
+ save
since 7.1.1
#13876
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
MarijnMensinga
changed the title
pre('validate') hook does not trigger when using
Sep 18, 2023
set
+ save
since 7.1.1pre('validate')
hook does not trigger when using set
+ save
since 7.1.1
MarijnMensinga
changed the title
pre('validate') hook does not trigger when using Sep 18, 2023
pre('validate')
hook does not trigger when using set
+ save
since 7.1.1set
+ save
since 7.1.1
const mongoose = require('mongoose');
const { Schema } = mongoose;
const attachmentSchema = new Schema({name: String})
attachmentSchema.pre('validate', () => { console.log('pre validate of attachmentSchema') })
const richImageSchema = new Schema({attachment: {type: attachmentSchema, required: false} })
richImageSchema.pre('validate', () => { console.log('pre validate of richImageSchema') })
const brandingSchema = new Schema({logo: richImageSchema})
brandingSchema.pre('validate', () => { console.log('pre validate of brandingSchema') })
const instanceSchema = new Schema({branding: brandingSchema})
instanceSchema.pre('validate', () => { console.log('pre validate of instanceSchema') })
const TestInstanceModel = mongoose.model('testInstance', instanceSchema)
async function run() {
console.log('mongoose version', mongoose.version)
await mongoose.connect('mongodb://127.0.0.1:27017');
await mongoose.connection.dropDatabase();
console.log('STARTING TEST');
console.log('BEFORE CREATE');
const instance = await TestInstanceModel.create({branding: {logo: {} } });
const doc = await TestInstanceModel.findById(instance._id);
doc.set('branding.logo.attachment', {name: 'coolLogo'});
console.log('BEFORE SAVE');
await doc.save();
console.log('TEST DONE');
}
run(); |
IslandRhythms
added
the
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
label
Sep 18, 2023
I did some digging, it looks like this is related to #13339, specifically this change. However, undoing that change breaks the tests that PR is supposed to fix. I'm going to have to look into this issue some more. |
vkarpov15
added a commit
that referenced
this issue
Oct 1, 2023
…neath triply nested subdoc Fix #13876
vkarpov15
added a commit
that referenced
this issue
Oct 3, 2023
fix(document): call pre('validate') hooks when modifying a path underneath triply nested subdoc
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
Mongoose version
7.1.1+
Node.js version
18.7.1
MongoDB server version
6.x
Typescript version (if applicable)
5.1.6
Description
pre('validate')
hook does not get triggered anymore on nested paths when usingset
andsave
combo since version 7.1.1. In versions 7.1.0 and below (also 6.x) it still works as expected. All versions from 7.1.1 and above (at least 7.5.2) fail.The culprit is probably this PR although I cannot understand myself why this results in the demonstrated the issue
Steps to Reproduce
Expected Behavior
Console output expected 7.1.0 (and below 6.x):
Actual console output 7.1.1+ (7.5.2):
The text was updated successfully, but these errors were encountered: