What is the best way to validate updators using multiple fields upon calling findOneAndUpdate
?
#15525
Unanswered
richardnguyen99
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a Mongoose schema with NestJS:
I have an endpoint that allows callers to update a basic entity as following:
I want to make sure that
endYear
if not null is valid only if its value is greater thanstartYear
.I want to use
Update validators
to isolate my validations so this is what I came up with.It works but it feels clunky and anti pattern because I have to use
@ts-ignore
and many evil type casting. Is there any better way to achieve what I want?Here what I have tried:
Using
this.get("startYear")
orthis.startYear
in my validator function does not work because both returnundefined
. In the case ofthis.get()
, Mongoose states the following:So basically,
this.get("endYear")
returns a value whilethis.get("startYear")
does not.I avoid using
pre
hook middleware because I want to isolate the validations. You can assume that there are many more fields in the schema and there are multiple checks so I don't want to have a bloating hook. However, I'm opened to every argument that makes the hook function cleaner and more organized.Beta Was this translation helpful? Give feedback.
All reactions