Make .parse an assertion function
See original GitHub issueconst Post = z.object({slug: z.string()})
const thing = JSON.parse('{"slug": "hi"}')
// thing is any
Post.parse(thing)
// thing is {slug: string}
I realize that I could do const thing = Post.parse(JSON.parse(...))
or make a second variable, but I don’t really want to do that. My use case is in Remix.
Here’s what I have to do now:
const data = LoaderData.parse(useLoaderData())
But some people are freaked out when they see me passing a hook call in an arguments list. So I’d prefer to be able to do this:
const data = useLoaderData()
LoaderData.parse(data)
But currently that leaves data as any
.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:9
Top Results From Across the Web
Assertions - JavaScript - MDN Web Docs
Assertions include boundaries, which indicate the beginnings and endings of lines and words, and other patterns indicating in some way that ...
Read more >How to unit and/or integration test parsers? - Stack Overflow
Actually, your first solution is "better" than your second idea. However, don't check for exact equality. Do "contains" type tests.
Read more >Assertions — Postman Quick Reference Guide Version 1.9.0
¶ You want to assert that a part of the reponse has a specific value. For example you are not interested in the...
Read more >Assertions in C/C++ - GeeksforGeeks
Assertions are statements used to test assumptions made by programmers. For example, we may use assertion to check if the pointer returned ...
Read more >Assert in Python - TutorialsTeacher
The assert statement can optionally include an error message string, which gets displayed along with the AssertionError . Consider the following assert ......
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
Hey @scotttrinh,
Thanks for the kind words! I’m glad I’ve been helpful to you 😃
If this were just for my own apps then a custom hook would be fine, but I’m thinking about using this in official Remix docs and we want to have something that feels nicer than telling people to make their own hook. Something you can add without much work.
I think an
assertInput
method would be perfect. I’ve never heard ofpreprocess
, but I think I would prefer thatpreprocess
is ignored for theassertInput
if possible.Having a separate function that applies the preprocess is fine for my use case 👍