One line param checking question
in javascript, you want to:
1. inside of a function (let's call this
functionA
), run a function once (let's cal this function functionB
)
2. check the return value of functionB
3. IF the return value of functionB
is truthy or meets a certain criteria, you return from the parent scope that called it (functionA
).
one example could be checking required params for an API endpoint and returning early with an error message if any are missing.
it's trivial to abstract out of the route handler a generic param checker. I just wish there was a way to make param checking just one line in the route handler.
are there any clever tricks I'm not thinking of?1 Reply
you mean this?
but continuing on you idea, I would create a generic function that takes in the URL and a zod schema... then use JS method with zod to validate if all is good.
Something like:
and then you have something like:
Although this might be a bit better since you will probably reuse the params: