Skip to content
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

Closed
2 tasks done
MarijnMensinga opened this issue Sep 18, 2023 · 2 comments · Fixed by #13912
Closed
2 tasks done

pre('validate') hook does not trigger when using set + save since 7.1.1 #13876

MarijnMensinga opened this issue Sep 18, 2023 · 2 comments · Fixed by #13912
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@MarijnMensinga
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

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 using set and save 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

console.log('STARTING TEST')

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 = model('testInstance', instanceSchema)
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 SAFE')
await doc.save()

console.log('TEST DONE')

Expected Behavior

Console output expected 7.1.0 (and below 6.x):

[SERVER] STARTING TEST
[SERVER] BEFORE CREATE
[SERVER] pre validate of instanceSchema
[SERVER] pre validate of brandingSchema
[SERVER] pre validate of richImageSchema
[SERVER] BEFORE SAFE
[SERVER] pre validate of instanceSchema
[SERVER] pre validate of brandingSchema
[SERVER] pre validate of richImageSchema
[SERVER] pre validate of attachmentSchema 
[SERVER] TEST DONE

Actual console output 7.1.1+ (7.5.2):

[SERVER] STARTING TEST
[SERVER] BEFORE CREATE
[SERVER] pre validate of instanceSchema
[SERVER] pre validate of brandingSchema
[SERVER] pre validate of richImageSchema
[SERVER] BEFORE SAFE
[SERVER] pre validate of instanceSchema
[SERVER] pre validate of brandingSchema
[SERVER] pre validate of richImageSchema
     === MISSING ATTACHEMENT_SCHEMA HERE ===
[SERVER] TEST DONE


@MarijnMensinga MarijnMensinga changed the title pre('validate') hook does not trigger when using set + save since 7.1.1 pre('validate') hook does not trigger when using set + save since 7.1.1 Sep 18, 2023
@MarijnMensinga MarijnMensinga changed the title pre('validate') hook does not trigger when using set + save since 7.1.1 pre('validate') hook does not trigger when using set + save since 7.1.1 Sep 18, 2023
@IslandRhythms
Copy link
Collaborator

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();

@vkarpov15
Copy link
Collaborator

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
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
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
3 participants