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.

Make .parse an assertion function

See original GitHub issue
const 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

1reaction
kentcdoddscommented, Dec 3, 2021

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 of preprocess, but I think I would prefer that preprocess is ignored for the assertInput if possible.

0reactions
kentcdoddscommented, Dec 5, 2021

Having a separate function that applies the preprocess is fine for my use case 👍

Read more comments on GitHub >

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

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