Using Zod for API response validation #3631
Unanswered
MobinAskari
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
-
Hello, I hope all is well.
The API that I'm using in my project returns a response body which is shaped in either of these two shapes:
The Zod schemas that I defined for validating these responses look like this:
As for my questions:
1- Is using
z.ZodTypeAny
valid? I understand I could usez.AnyZodObject
since the data is currently an object but I want to future proof it2- I tried this current implementation with different data and it's working as expected, but could there potentially be an edge case that I'm not aware of?
3- the data is typed as
any
, although the provided Zod schema for thedata
property is a Zod object; Is there a way to get the correct type inference when the dataSchema is provided by the user?As in:
I was able to get the desired results by using generics, not sure if it's the correct approach
Beta Was this translation helpful? Give feedback.
All reactions