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
Soo this is a bit tricky. If you have a Schema defining an object with a nested array of schemas and you perform a direct assignent of the nested object, the validator are called twice:
Once with this set as the parent object
Once with this set as the array schema (desiderata)
This is the minimum code I was able to write to reproduce the issue:
constmongoose=require('mongoose');// Creating the inner schema with custom validatorconstInnerSchema=newmongoose.Schema({myfield: {type: String,validate: {validator: myValidator,message: 'Message'}},sibling: String});// Creating the main schema. The array is nested inside an objectconstMySchema=newmongoose.Schema({nest: {myarray: [InnerSchema]},rootSibling: String});constModel=mongoose.model('MyModel',MySchema);constinst=newModel({rootSibling: 'This is the root sibling'});// Direct object assignmentinst.nest={myarray: [{myfield: 'This is my field',sibling: 'This is the nested sibling'}]};inst.save();// Expected output:// This is my field , This is the nested sibling , undefined// Actual output:// This is my field , undefined , This is the root sibling// This is my field , This is the nested sibling , undefined// My validator prints the value, this.sibling and this.rootSiblingfunctionmyValidator(v){console.log(v,',',this.sibling,',',this.rootSibling);}
The text was updated successfully, but these errors were encountered:
Soo this is a bit tricky. If you have a Schema defining an object with a nested array of schemas and you perform a direct assignent of the nested object, the validator are called twice:
this
set as the parent objectthis
set as the array schema (desiderata)This is the minimum code I was able to write to reproduce the issue:
The text was updated successfully, but these errors were encountered: