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.

Support Zod's "parseAsync" method

See original GitHub issue

I’ve added an async transform to as Zod validator - and looks like tRPC does not support that.

Whan I use: z.string().nullable().transform(async (base64string) => { return await uploadToCloud(base64string).id; })

I get: “TRPCClientError: Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.”

Any solution?

This is very important for situations like uploading files to external services.

Thanks!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
KATTcommented, Oct 19, 2021

Let me know if you need any further guidance on how to navigate the codebase when adding the parseAsync, @danielyogel. It should just be a matter of turning some functions async.

1reaction
danielyogelcommented, Oct 18, 2021

Do you mean that the input of the parsing can be different from the output? Does zod support inferring that?

No, this feature is already supported by Zod using “transform” / “transformAsync”. I mean, that the result of the validation (+ transform), can be “encoded” back into a different type that is still defined in the same schema. This can be used for example, for “encoding” back data that the tRPC’s resolver returns, using the same shema. Maybe io-ts explains this better than me: io-ts. Note that io-ts support this, but does not support async encoders/decoders 😦

I guess it is more related to Zod than tRPC for now - and I saw more people ask for this in Zod’s repo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

colinhacks/zod: TypeScript-first schema validation ... - GitHub
Zod is a TypeScript-first schema declaration and validation library. I'm using the term "schema" to broadly refer to any data type, from a...
Read more >
Schema Validation with Zod and Express.js
const validate = (schema: AnyZodObject) => async (req: Request, res: Response, next: NextFunction) => { try { await schema.parseAsync({ body: ...
Read more >
Schema validation in TypeScript with Zod - LogRocket Blog
In this article, you will learn about schema design and validation in Zod and how to run it in a TypeScript codebase at...
Read more >
TypeScript-First Schema Validation with Static Type Inference
parseAsync method to parse data! Otherwise Zod will throw an error. Relationship to transforms. Transforms and refinements can be interleaved: z.string() .
Read more >
/README.md | zod@v3-snapshot-2021-01-21 | Deno
parseAsync method to parse data! Otherwise Zod will throw an error. As you can see, .refine takes two arguments. The first is the...
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