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

Custom validator on nested subdocument array field is not running on save() #11672

Closed
francescov1 opened this issue Apr 15, 2022 · 3 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@francescov1
Copy link
Contributor

Do you want to request a feature or report a bug?

Report a bug

What is the current behavior?

When setting the field of a nested subdocument array, the field validator does not run when calling save(). This only happens when the document already exists in the DB and is being updated.

If the current behavior is a bug, please provide the steps to reproduce.

Note that the repro below requires a DB connection

import mongoose, { Schema } from 'mongoose';

const ChildSchema = new Schema(
  {
    price: {
      type: Number,
      validate: function (val: number) {
          return val > 0;
        }
    },
  }
);
const Child = mongoose.model('Child', ChildSchema);

const ParentSchema = new Schema(
  {
    // Note the nesting. If `nestedSubdocArray` is at the root level, the problem does not occur.
    rootField: {
      nestedSubdocArray: [Child.schema]
    }
  }
);
const Parent = mongoose.model('Parent', ParentSchema);

it('should throw if price is less than 0', async () => {
  const parentDoc = new Parent({
    rootField: {
      nestedSubdocArray: [
        {
          price: -1
        }
      ],
    }
  });

  // Throws error as expected (price must be greater than 0)
  await expect(parentDoc.save()).rejects.toThrow()
  
  // Now we set the price to a valid value and save
  parentDoc.rootField.nestedSubdocArray[0].price = 1;
  await parentDoc.save();

  // Now we try editing to an invalid value which should throw
  parentDoc.rootField.nestedSubdocArray[0].price = -1;
  // This test fails, the document saves successfully
  await expect(parentDoc.save()).rejects.toThrow()
})

What is the expected behavior?

Validation runs properly.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Mongoose: 6.3.0
Node.js: 16.8
MongoDB: 5.0

Note that this started happening when I upgraded from Mongoose 6.1.6 to 6.3.0

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Apr 18, 2022
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const {Schema} = mongoose;

const childSchema = new Schema({
    price: { type: Number, validate: function (val) {
        return val > 0
    }}
});

const Child = mongoose.model('Child', childSchema);

const parentSchema = new Schema({
    rootField: {
        nestedSubdocArray: [Child.schema]
    }
});

const Parent = mongoose.model('Parent', parentSchema);

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

    const doc = new Parent({
        rootField: {
            nestedSubdocArray: [{
                price: -1
            }]
        }
    });
    
    await doc.save().catch((err) => console.log(err));
    console.log('next');
    doc.rootField.nestedSubdocArray[0].price = 1;
    await doc.save();
    console.log('correct save complete');
    doc.rootField.nestedSubdocArray[0].price = -1;
    await doc.save();
    console.log('after incorrect save')
}

run();

@francescov1
Copy link
Contributor Author

Is there a timeline on when this will be fixed? It's blocking us from upgrading Mongoose versions

@Uzlopak
Copy link
Collaborator

Uzlopak commented Apr 22, 2022

If you provide a PR it could be part of the next release :). But usually a ticket gets a milestone to it and it gets solved when there is capacity. I guess if you have a tidelift subscription, than @vkarpov15 would prioritize it as high and solve it asap.

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
Development

No branches or pull requests

4 participants