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

Document.prototype.validate() does not validate subdocument required fields with validateModifiedOnly option #14677

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

Comments

@lcrosetto
Copy link

lcrosetto commented Jun 19, 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.4.3

Node.js version

20.14.0

MongoDB server version

7.0.11

Typescript version (if applicable)

No response

Description

In versions 8.2.4 and prior, a call to Document.prototype.validate(null, {validateModifiedOnly: true}) would throw an error if required fields in a subdocument were missing. In 8.3.0 and subsequent versions, validate() no longer checks for required fields in subdocuments if the validateModifiedOnly option is set to true.

If subdocuments are added, the required fields should be validated as was done in 8.2.4 and prior.

Steps to Reproduce

The following code will throw an error in mongoose versions 8.2.4 and prior, and succeed in 8.3.0 and subsequent:

      const embedSchema = new mongoose.Schema({
        field1: {
          type: String,
          required: true,
        },
        field2: String,
      });
      const testSchema = new mongoose.Schema({
        testField: {
          type: String,
          required: true,
        },
        testArray: [embedSchema],
      });
      const TestModel = mongoose.model('Test', testSchema);
      const m = new TestModel({testArray: [{field2: 'test'}]});
      await m.validate(null, {validateModifiedOnly: true});

With the validateModifiedOnly option, testField is not required, however it also skips the check for field1 in the subdocument (which is missing) starting in version 8.3.0.

Expected Behavior

If a subdocument is added, validate() should check that all required fields are present when the validateModifiedOnly is set to true.

@IslandRhythms IslandRhythms added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Jun 20, 2024
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Jun 20, 2024

'use strict';

const mongoose = require('mongoose');

const embedSchema = new mongoose.Schema({
    field1: {
      type: String,
      required: true,
    },
    field2: String,
  });
  const testSchema = new mongoose.Schema({
    testField: {
      type: String,
      required: true,
    },
    testArray: [embedSchema],
  });
  const TestModel = mongoose.model('Test', testSchema);

  async function run() {
    await mongoose.connect('mongodb://localhost:27017');
    await mongoose.connection.dropDatabase();
    const m = new TestModel({testArray: [{field2: 'test'}]});
    await m.validate(null, {validateModifiedOnly: true});
    console.log('done');
  }

  run().catch(err => {
    console.log(err);
    process.exit(-1)
  });

@vkarpov15 vkarpov15 added this to the 8.4.4 milestone Jun 25, 2024
vkarpov15 added a commit that referenced this issue Jun 25, 2024
fix(document): avoid passing validateModifiedOnly to subdocs so subdocs get fully validating if they're directly modified
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