#Mongodb doesnot validate reference in Schema
6 messages · Page 1 of 1 (latest)
You are responsible for referential integrity. If you know there could be bad input given, then you need to validate it yourself. There is no quick fix and certainly no workaround. It's part of working with MongoDB. There are several ways to do it, but it depends on how your application works. For instance, if the data for the reference is coming from the client, you'd have maybe a pipe validating it. If the data for the reference is being generated server-side, but via code not in your control, you could create a method in your service or model. Or, you could use a Mongoose hook. All up to you.
There is also a number of plugins, like this one: https://www.npmjs.com/package/mongoose-references-integrity-checker
Package useful for mantaining the references structure of mongoose models. Supporting soft delete.. Latest version: 1.0.9, last published: 3 years ago. Start using mongoose-references-integrity-checker in your project by running npm i mongoose-references-integrity-checker. There are 2 other projects in the npm registry using mongoose-reference...
Sorry, that one is for integrity when deleting. This one is for checking the reference: https://www.npmjs.com/package/mongoose-reference-validator
It is a bit old though. You might have issues with it. Most of the time, these plugins are just hooks anyway. So, you could simply look into the code to get an idea of what they are doing and either do it yourself or fix the code, if there is a compatibility issue.