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

findOneAndReplace throws validation errors with runValidators: false #11559

Closed
eliw00d opened this issue Mar 23, 2022 · 4 comments
Closed

findOneAndReplace throws validation errors with runValidators: false #11559

eliw00d opened this issue Mar 23, 2022 · 4 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@eliw00d
Copy link

eliw00d commented Mar 23, 2022

Do you want to request a feature or report a bug?
Report a bug. But, if it is not a bug, then request a feature.

What is the current behavior?
ValidationErrors are thrown when using the runValidators: false option in findOneAndReplace.

If the current behavior is a bug, please provide the steps to reproduce.
Have a model with required fields and try to findOneAndReplace without required fields and with runValidators set to false.

What is the expected behavior?
ValidationErrors are not thrown when using the runValidators: false option in findOneAndReplace.

Basically, we want to be able to soft delete a document by replacing it with fewer fields than is normally required without triggering validation.

I searched for findOneAndReplace + runValidators in Issues and found this. Although the issue is about overwriteDiscriminatorKey it did include a snippet for findOneAndReplace with runValidators, so unless it was a copy & paste error it would seem it should be supported?

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: 16.13.0
Mongoose: 6.2.8
MongoDB: 4.4.6-8

@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Mar 28, 2022
@IslandRhythms
Copy link
Collaborator

Please modify the script below to demonstrate your issue.

const mongoose = require('mongoose');
const {Schema} = mongoose;

const testSchema = new Schema({
    name: {
        type: String,
        require: true
    }
});

const Test = mongoose.model('Test', testSchema);

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

    const entry = await Test.create({
        name: 'Test'
    });

    await Test.findOneAndReplace({name: 'Test'}, {name: 'Testerson'}, {runValidators: false});

    console.log(await Test.find());
}

run();

@eliw00d
Copy link
Author

eliw00d commented Mar 28, 2022

const mongoose = require('mongoose');
const {Schema} = mongoose;

const testSchema = new Schema({
    name: {
        type: String,
        required: true // you had a typo here
    }
});

const Test = mongoose.model('Test', testSchema);

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

    const entry = await Test.create({
        name: 'Test'
    });

    await Test.findOneAndReplace(
      {name: 'Test'}, 
      {}, // this part is key, I am trying to replace without required fields
      {runValidators: false}
    );

    console.log(await Test.find());
}

run();

throws

ValidationError: test validation failed: name: Path `name` is required.

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Mar 28, 2022
@IslandRhythms
Copy link
Collaborator

Good catch on the typo.

@eliw00d
Copy link
Author

eliw00d commented Mar 28, 2022

It's worth pointing out that switching from findOneAndReplace to replaceOne works as expected.

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

3 participants