Complex type coercion
See original GitHub issueWe have an issue where we are using Zod to apply strong typing practices to our API layer
Trouble we have is using complex object over GET, as everything becomes a string over the wire.
Are we able to attempt deep object coercion with Zod? Something like:
const schema = z.object({example: z.number()})
const data = {example: '100'}
const coersedData = scheme.coerce(data) // {example: 100}
const brokenData = {example: 'lemon'}
scheme.coerce(brokenData) // Error
Issue Analytics
- State:
- Created 2 years ago
- Comments:17 (1 by maintainers)
Top Results From Across the Web
Type Coercion in JavaScript - Medium
Type Coercion Definition. As per MDN, Type Coercion refers to the automatic or implicit conversion of values from one type to another.
Read more >Type coercion - MDN Web Docs Glossary
Type coercion is the automatic or implicit conversion of values from one data type to another (such as strings to numbers).
Read more >JavaScript's Wild Type Coercion
Type coercion is the act of automatically or implicitly converting a value from one data type to another. The age-old beginner's introduction to ......
Read more >Type coercion - Make
Type coercion. This document describes how Make behaves in situations when it receives values in expected and unexpected data formats.
Read more >Type casting of complex object in derived class - Stack Overflow
To do this, I need to type cast each object to its base class. But when I typecast the object. it shows error...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I guess for me, it’s less about the false positives, and more about the usefulness of type coercion. I know that the object I sent over the wire confirms to my type, because I tested it before I made the request, but I also know that all my values will become strings in transit, and I want a convenient way to re-use the schema I have already defined to coerce the data back into the state I wish it to be in, if that makes sense?
@mildfuzz I encountered something similar and just used what @scotttrinh mentioned. I found that if you simply reuse your complex
refine
andtransform
types, your definitions can stay pretty readable: