These are chat archives for Automattic/mongoose

14th
Nov 2017
John Slay
@johnslay
Nov 14 2017 17:54

Hi all, I have a 'Role' model that can have an array of refs to a 'Policy' model... My problem is that when the user creates/updates that model, I need to verify that the array refs on the 'Role' model actually reference an objectID that exists in the policies collection... I have it working just fine using custom validators when the user creates the 'Role' model. However, when updating, I'm having some problems...

One problem is that, lets say I pass 2 objectIDs in the array on the 'Role' model. 1 of them is valid, but the other doesn't exist.. I would like to save the model but filter out the invalid one. How can achieve this? My validator works because I can reference the model and override the property during the validation, but when updating I can't reference the model...

My other technique was to disable validation on updates, and use a middleware pre hook on 'findOneAndUpdate' but I can't quite figure out how to validate it from there