Missing Multiple Field Validation Hook (Schema Validation)
See original GitHub issueHi everybody. I have previous experience using other validation libraries and in all of them I have a hook to put my code where I want validate multiple fields. This hook is executed after successful validation of all individual fields. I know that is possible achieve the same using mongoose
, but I end up executing the same field validation for each field dependency. E.g. field2
validation depends on field1
and field3
depends on field1
. Did you see? field1
validation executed two times.
After search a lot, my only solution to avoid this was override schema.methods.validate
, inside it call validate
and then put my multiple field validation code. To improve this, I plan do a monkey patch on mongoose.Document.prototype.validate
.
There is another solution for this? Please tell me.
Issue Analytics
- State:
- Created 7 years ago
- Comments:10
I just found a way to workaround this issue: Create a method
schema.validateTogether
and put there multi-field validation code, then create another methodschema.validateAll
that callsschema.validate
andschema.validateTogether
. Thenschema.pre('save')
callsschema.validateTogether
. If I want validate data, insteadschema.validate
I doschema.validateAll
. Yeah, that’s it.That’s a good workaround 👍