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: is not triggert on all sub documents when set via _.set() with a nested path #14591

Closed
2 tasks done
MarijnMensinga opened this issue May 14, 2024 · 1 comment · Fixed by #14604
Closed
2 tasks done
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@MarijnMensinga
Copy link

MarijnMensinga commented May 14, 2024

Prerequisites

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

Mongoose version

8.3.4

Node.js version

v20

MongoDB server version

5.0

Typescript version (if applicable)

5.4.5

Description

If I have a multi nested document like this: doc = {a: {b: {c: {d: {e: string} } } } }
And then I change a value via await doc._set({'a.b.c.d.e': 'value'}).save() the value is saved correctly in the nested e subDocument. But during the saving process not all the pre('validate', () => ..) hooks get triggered.

Only on the first (a) and second subdocument (b) the pre validate hook is triggered.

Steps to Reproduce

const createSchema = (path,subSchema) => {
    const schema = new Schema({[path]: subSchema})
    schema.pre('validate', function () {
        console.debug(' - pre.validate ::',path)
    })
    schema.pre('save', function () {
        console.debug(' - pre.save ::',path)
    })
    return schema
}

const e = createSchema('e', {type: String})
const d = createSchema('d', {type: e})
const c = createSchema('c', {type: d})
const b = createSchema('b', {type: c})
const a = createSchema('a', {type: b})

const NestedModelHooksTestModel = model('NestedModelHooksTest', a)

// Variant 1
console.debug('Creating doc: {a: {b: {c: {d: {e: "SomeDate"}}}}}')
const newData = {a: {b: {c: {d: {e: new Date().toString()}}}}}
const doc = await new NestedModelHooksTestModel(newData).save()

// Variant 2
console.warn('Updating via new value: {a: {b: {c: {d: {e: "Some override value"}}}}}')
doc.set({a: {b: {c: {d: {e: "Some override value"}}}}})
await doc.save()

// Variant 3
console.warn('Updating via doc: doc.a.b.c.d.e = \'updated nested value 2\'')
doc.a.b.c.d.e = 'updated nested value 2'
await doc.save()

// Variant 4
console.warn('Updating via set: {\'a.b.c.d.e\': \'updated nested value\'}')
doc.set({'a.b.c.d.e': 'updated nested value'})
await doc.save()

Expected Behavior

For every variant I expected the following output:

 - pre.validate :: a
 - pre.validate :: b
 - pre.validate :: c
 - pre.validate :: d
 - pre.validate :: e
 - pre.save :: e
 - pre.save :: d
 - pre.save :: c
 - pre.save :: b
 - pre.save :: a

This is indeed the output for variants 1, 2 and 3

But when using doc.set({'a.b.c.d.e': 'updated nested value'}), aka variant 4, the value is saved correctly but not all the pre validate hooks are triggered:

 - pre.validate :: a
 - pre.validate :: b
 - pre.save :: e
 - pre.save :: d
 - pre.save :: c
 - pre.save :: b
 - pre.save :: a

I was expecting to see the following pre validate hooks get triggered as well:

 - pre.validate :: c
 - pre.validate :: d
 - pre.validate :: e
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label May 15, 2024
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const { Schema, model } = mongoose;

const createSchema = (path,subSchema) => {
  const schema = new Schema({[path]: subSchema})
  schema.pre('validate', function () {
      console.debug(' - pre.validate ::',path)
  })
  schema.pre('save', function () {
      console.debug(' - pre.save ::',path)
  })
  return schema
}

const e = createSchema('e', {type: String})
const d = createSchema('d', {type: e})
const c = createSchema('c', {type: d})
const b = createSchema('b', {type: c})
const a = createSchema('a', {type: b})

const NestedModelHooksTestModel = model('NestedModelHooksTest', a)


async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  // Variant 1
  console.debug('Creating doc: {a: {b: {c: {d: {e: "SomeDate"}}}}}')
  const newData = {a: {b: {c: {d: {e: new Date().toString()}}}}}
  const doc = await new NestedModelHooksTestModel(newData).save()

  // Variant 2
  console.warn('Updating via new value: {a: {b: {c: {d: {e: "Some override value"}}}}}')
  doc.set({a: {b: {c: {d: {e: "Some override value"}}}}})
  await doc.save()

  // Variant 3
  console.warn('Updating via doc: doc.a.b.c.d.e = \'updated nested value 2\'')
  doc.a.b.c.d.e = 'updated nested value 2'
  await doc.save()

  // Variant 4
  console.warn('Updating via set: {\'a.b.c.d.e\': \'updated nested value\'}')
  doc.set({'a.b.c.d.e': 'updated nested value'})
  await doc.save()
}

run();

vkarpov15 added a commit that referenced this issue May 17, 2024
@vkarpov15 vkarpov15 added this to the 8.4.1 milestone May 17, 2024
vkarpov15 added a commit that referenced this issue May 24, 2024
fix(document): fire pre validate hooks on 5 level deep single nested subdoc when modifying after save()
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