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.

`parse.typed(...)` or some kind of typed parse method

See original GitHub issue

It’d be nice to have zod help the developer max out their chances of parsing a valid object, when it’s manually constructed. Example:

const User = z.object({
  name: z.string().regex(/^\w+ \w+$/),
  age: z.number(),
})

User.parse({
  name: 'Bob',
  birthdate: new Date('1970-01-01'),
})

The above code compiles, but we have enough information to know that it’ll throw a ZodError at runtime. It’d be nice if there was a method like

User.parse.typed({
  name: 'Bob',
  birthdate: new Date('1970-01-01'),
})

Where the expected input type was Input (from the ZodType<Output, Def, Input>) rather than unknown. So the above code would error because the developer used birthdate instead of age.

This would be especially useful for types with a custom .refine or .regex method or similar where it could be misleading to use just a type declaration:

const user: z.infer<typeof User> = {
  name: "Robert'; -- DROP TABLE Students; --"
  age: 40,
}

The above will compile, and implies that the const user is a validated User instance, but it isn’t because name doesn’t match the regex. The current options are to do the above, which is safe-looking at compile time, but unsafe at runtime, or to use .parse(...) which is unsafe at compile time and safe-ish at runtime (but will throw errors).

Issue Analytics

  • State:open
  • Created 9 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
JacobWeisenburgercommented, Dec 24, 2022

I don’t think that this will ever be added to Zod, because it’s my understanding that parse is meant to take something that is unknown and turn it into something that is known at compile time. So when you are trying to parse an object that is statically in the code base, that’s something that is already known at compile time. In example code we do this all the time for learning/teaching purposes, but I don’t know that I have ever done that in a real app. In real apps I use Zod for checking user input or a response from an api, which are things that can’t be known at compile time. I hope this makes sense. Please let me know if you have questions or if I am misunderstanding what you are talking about.

0reactions
mmkalcommented, Dec 24, 2022

Yes it’s a slightly different use case than validating API inputs, and I usually want this when using refinement types, or regex strings, or similar, which perform runtime validation which is not reflected at compile time. And yes that helper function does the trick for most cases - what I’m asking is whether it could become part of zod. There are some edge cases like .transform types with a different input typearg which it’d be sensible to solve in one official place.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON.parse() - JavaScript - MDN Web Docs
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver ...
Read more >
Parse strings in .NET - Microsoft Learn
Understand string parsing in .NET. Parsing converts a string representing a .NET base type into that base type. Parsing is the reverse ...
Read more >
How do I parse a string to a specific object type based on Type?
In your case, instead of calling the ParseValue method, you can do something like this: parsedValue = Convert.ChangeType(type, argValue).
Read more >
How to Convert and Parse Different Data Types in C#
You can parse different data types such as integers, doubles, boolean types, and datetime types. Strings to Integers. To convert a string to...
Read more >
11 parse() methods in Java with Examples - CodeGym
1. Period parse() method · The method has one parameter — a text value. · Parse() returns a Period value where the value...
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