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

storeSubdocValidationError ignored in update operations #9172

Closed
itayl2 opened this issue Jun 24, 2020 · 3 comments
Closed

storeSubdocValidationError ignored in update operations #9172

itayl2 opened this issue Jun 24, 2020 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@itayl2
Copy link

itayl2 commented Jun 24, 2020

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

What is the current behavior?
Update operations (tested findOneAndUpdate & updateOne) store sub doc validation errors regardless of submitted options (storeSubdocValidationError:false), while .save() handles it properly.

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

const mongoose = require('mongoose');

const schemaOpts = {storeSubdocValidationError: false};
const innerSchema = mongoose.Schema({
    name: {
        type: String,
        required: [true, 'Missing']
    },
    val: {
        type: mongoose.Schema.Types.Mixed,
        required: [true, 'Missing']
    }
}, schemaOpts);

const fieldSchema = mongoose.Schema({
    subFieldName: {
        type: [innerSchema]
    }
}, schemaOpts);

const parentSchema = mongoose.Schema({
    parentFieldName: {
        type: fieldSchema
    }
}, schemaOpts);

const templates = {
    INVALID: {
        parentFieldName:
            {
                subFieldName: [
                    {}
                ]
            }
    },
    VALID: {
        parentFieldName:
            {
                subFieldName: [
                    {
                        name: 'testName',
                        val: 'testVal'
                    }
                ]
            }
    }
};

const main = async () => {
    const connectionString = % YOUR CONNECTION STRING %;
    const connOpts = {
        % YOUR CONNECTION OPTIONS %
    };
    console.log('Connecting...');
    const db = await mongoose.createConnection(connectionString, connOpts);

    const modelName = % YOUR MODEL NAME %;
    const Parent = db.model(modelName, parentSchema, modelName);

    console.log('Creating...');
    let doc;
    try {
        doc = await new Parent(templates.INVALID).save();
    } catch (err) {
        const found = Object.keys(err.errors);
        console.error(`Bad fields when creating (${found.length}): ${found}`);
	//Output: Bad fields when creating (2): parentFieldName.subFieldName.0.val,parentFieldName.subFieldName.0.name
    }

    doc = await new Parent(templates.VALID).save();
    console.log('Updating...');
    try {
        const updateResult = await Parent.findOneAndUpdate({_id: doc._id}, {$set: templates.INVALID}, {new: true, runValidators: true, fields: {_id: 0}, context: 'query'}).exec();
    } catch (err) {
        const found = Object.keys(err.errors);
        console.error(`Bad fields when updating (${found.length}): ${found}`);
	//Output: Bad fields when updating (3): parentFieldName.subFieldName.0.val,parentFieldName.subFieldName.0.name,parentFieldName
    }

    console.log('Done');
};

main();

What is the expected behavior?
Mongoose should only return return unique validation errors rather than one for the sub doc and one for the doc.

When debugging the src it seems like the issue is associated with the fact in lib/document.js #validatePath(), schemaType is of the actual field it's currently validating (i.e "name" / "val" in the attached example).
When using .save(), the schemaType is of the 'parentFieldName' which can then use the storeSubdocValidationError option.

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

@vkarpov15 vkarpov15 added this to the 5.9.22 milestone Jul 2, 2020
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Jul 2, 2020
vkarpov15 added a commit that referenced this issue Jul 3, 2020
@itayl2
Copy link
Author

itayl2 commented Jul 3, 2020

@vkarpov15 , I'd love to know how err.errors['nested.arr.0.name'] would work in the test you wrote for it. Is it a special object that supports accessing nested paths via string?

@vkarpov15 vkarpov15 added 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 labels Jul 3, 2020
@vkarpov15
Copy link
Collaborator

@itayl2 err.errors is a POJO whose keys are the paths that errored out.

@itayl2
Copy link
Author

itayl2 commented Jul 4, 2020

@itayl2 err.errors is a POJO whose keys are the paths that errored out.

Oh right, forgot the structure, thanks!

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

2 participants