question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Complex type coercion

See original GitHub issue

We 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:closed
  • Created 2 years ago
  • Comments:17 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
mildfuzzcommented, May 26, 2021

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?

1reaction
nicholaschiangcommented, Jun 15, 2021

@mildfuzz I encountered something similar and just used what @scotttrinh mentioned. I found that if you simply reuse your complex refine and transform types, your definitions can stay pretty readable:

const date = z.string().refine((d) => !Number.isNaN(Date.parse(d))).transform((d) => new Date(d));
const Timeslot = z.object({
  id: z.string(),
  from: date,
  to: date,
  exdates: z.array(date).optional(),
  recur: z.string().optional(),
  last: date.optional(),
});
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found