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
I find that the result is wrong if define a single nested sub-document field by syntax as shown below.
Unknown fields are defined in the sub-document.
Wrong type value can be input to the field of sub-document.
Below is the test code to reproduce the problem:
constchai=require("chai");constexpect=chai.expect;constmongoose=require('mongoose');constSchema=mongoose.Schema;consttestModel=mongoose.model('test',newSchema({singleNested: {type: {field1: Boolean,field2: Boolean},default: {field1: false,field2: false}},}));describe('test',()=>{it('Should not define fields that not exist',()=>{expect(testModel.schema.path('singleNested.fgfggsdfgdf')).to.undefined;expect(testModel.schema.path('singleNested.sfdfsfds')).to.undefined;expect(testModel.schema.path('singleNested.dsfsfdsf')).to.undefined;});it('Should not allow set the wrong type value',()=>{consttestDoc=newtestModel();testDoc.singleNested.field1='String value to boolean field';testDoc.validateSync('singleNested');expect(testDoc.errors['singleNested.field1']).to.be.an('Error');});});
And the test result is:
$ mocha index.js
test
1) Should not define fields that not exist
2) Should not allow set the wrong type value
0 passing (30ms)
2 failing
Should not define fields that not exist:
AssertionError: expected { Object (path, instance, ...) } to be undefined
at Context.<anonymous> (index.js:24:65)
at processImmediate (internal/timers.js:456:21)
2) test
Should not allow set the wrong type value:
TypeError: Cannot read property 'singleNested.field1' of undefined
at Context.<anonymous> (index.js:34:26)
at processImmediate (internal/timers.js:456:21)
We're changing this behavior in 6.0 with #7181 because this is likely at least the 100th time someone has reported this behavior as a bug, and I agree this behavior is exceedingly confusing.
Hi,
I find that the result is wrong if define a single nested sub-document field by syntax as shown below.
Below is the test code to reproduce the problem:
And the test result is:
nodejs: 12.18.2
mongoose: 5.9.21
chai: 4.2.0
mocha: 8.0.1
The text was updated successfully, but these errors were encountered: