You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
constmongoose=require('mongoose');constschemaOpts={storeSubdocValidationError: false};constinnerSchema=mongoose.Schema({name: {type: String,required: [true,'Missing']},val: {type: mongoose.Schema.Types.Mixed,required: [true,'Missing']}},schemaOpts);constfieldSchema=mongoose.Schema({subFieldName: {type: [innerSchema]}},schemaOpts);constparentSchema=mongoose.Schema({parentFieldName: {type: fieldSchema}},schemaOpts);consttemplates={INVALID: {parentFieldName:
{subFieldName: [{}]}},VALID: {parentFieldName:
{subFieldName: [{name: 'testName',val: 'testVal'}]}}};constmain=async()=>{constconnectionString=%YOURCONNECTIONSTRING%;constconnOpts={%YOURCONNECTIONOPTIONS%};console.log('Connecting...');constdb=awaitmongoose.createConnection(connectionString,connOpts);constmodelName=%YOURMODELNAME%;constParent=db.model(modelName,parentSchema,modelName);console.log('Creating...');letdoc;try{doc=awaitnewParent(templates.INVALID).save();}catch(err){constfound=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=awaitnewParent(templates.VALID).save();console.log('Updating...');try{constupdateResult=awaitParent.findOneAndUpdate({_id: doc._id},{$set: templates.INVALID},{new: true,runValidators: true,fields: {_id: 0},context: 'query'}).exec();}catch(err){constfound=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
The text was updated successfully, but these errors were encountered:
@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
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
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:
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
The text was updated successfully, but these errors were encountered: