Zod

Z

Zod

Join the community to ask questions about Zod and get answers from other members.

Join

Dawson - Can someone sanity check this for me? ...

Can someone sanity check this for me? I'm trying to use an async refinement to validate name uniqueness, but the parse is always succeeding: ```ts z.string() .min(1)...

Torbjørn - Hi!We use zod to validate data com...

Hi! We use Zod to validate data coming from sanity CMS. Sanity has this fancy preview mode that uses @vercel/stega a package that encodes invisible strings into its output....
Solution:
if your goal is to keep the types as if the stega matched ones aren't there you can do something like this ```ts const MyStringSchema = z.union([...

Haddok - hi , i have defined a schema where i a...

hi , i have defined a schema where i am accessing a few fields using shape for some specific scenario i tried using myschema.shape[fieldname].safeparse(value) ...

unnreaal - tsconst schema = z.coerce.string()....

```ts const schema = z.coerce.string().optional(); // type Schema = string | undefined; type Schema = z.infer<typeof schema>;...

Steve - Is there any data on performance compar...

Is there any data on performance compared to @effect/schema preferably specifically to recursive types? ^[1] ^[2]

Steve - Would this be valid, or would it possib...

Would this be valid, or would it possibly parse data as unknown while there could be a more specific match? ```ts import { ContentType } from '@/api/ContentType' import { TipTapDocumentDto } from '@/tiptap/schemas/translations/Document'...
Solution:
order matters in union definitions (they're run in series one after the other), so as long as you put the more specific ones first, it'll match the more specific ones and not the unknown one.

Bence (Arzen) - Hi guys 🙂 Is there a way to cr...

Hi guys 🙂 Is there a way to create a recursive record schema? I know how to make recursive schema using an object, but I am curious if it is currently possible to do with records. The keys can be anything in my data, that's why I'm trying to use a record.

Steve - We have some performance issues with bi...

We have some performance issues (in browser) with big blobs of json using transform() and lazy(). Any general tips and tricks to make it more performant?
Solution:
I don't think there is much that can be done there: if you are making big transforms to large blobs of JSON data, Zod is an expensive way to do it (since we're doing checking and transforming).

Z4RM - Hello! I'm going to write a fullstack ap...

Hello! I'm going to write a fullstack application, and for types consistency between my backend and frontend, I'm looking at Zod and implementing it in my app. However, I'm running into an issue: I want to include the error object returned from the parse method, but I don't find how I can "include" it in a z.object. I'm not sure if what I say is very clear, so here is an explanation in code: ```ts...
Solution:
You could use z.custom for this

Whimsy - is there any function or way to make s...

is there any function or way to make schema fully optional, like literally every property, every nested lazy model? this does work, but is it like correct, is there anything seriously wrong with it or ```ts export function deepPartialify(schema: z.ZodTypeAny): z.ZodTypeAny {...

Steve - How much overhead does Zod have? How pe...

How much overhead does Zod have? How performant is it actually?

Steve - I was wondering if there is a certain w...

I was wondering if there is a certain way to further optimize the .refine. And if possible, also improve the typing of it. Basically also wondering I would be able to type TipTapTextDto['marks'] In a way that I define it to be an array of TipTapMarkDto but also it to have at least one IdentifierMarkDto, no matter the order in which the TipTapMarkDto present themselves. ```ts...
Solution:
You could use something like a brand to indicate to the consumer that it has been verified to contain a certain element, but the type system still wouldn't know how to encode that information, so you'd still have to code defensively

Steve - I have this unique case where I want to...

I have this unique case where I want to filter out elements that do not match my union. Is there any way to do that using zod? Basically: z.union([ ... ]).strip()...
Solution:
```ts export const TipTapDocumentDto = z .object({ type: z.literal("doc"), attrs: z.object({...

Discriminated union default value on the discriminator

Discriminated union default value on the discriminator
Solution:
```ts const BaseReservation = z.object({ reservationId: z.number(), reservedBy: z.string(), });...

Stephan Meijer - What was the deal with union a...

What was the deal with union and transform again? ```ts import { Heading, Paragraph, Text } from '@toegang-voor-iedereen/document-spec-types'; import { v4 as uuidv4 } from 'uuid';...

Whimsy - .optional() makes the type optional? l...

.optional() makes the type optional? like using it with z.boolean().default(false) makes it boolean | undefined

Stephan Meijer - How does Zod's transform funct...

How do Zod's transform work with schema's inside other schema's that also have transform on them?
Solution:
If that’s what you’re asking, inner transforms get run first

CommandMC - z.function().args() seems to forget...

z.function().args() seems to forget brands? Hello. Using a branded schema as an argument to a Zod function (z.function()) & then inferring the type of that function doesn't seem to be working correctly, the argument type just reverts to its non-branded type TS Playground example of my issue...

Stephan Meijer - When exporting myschema's and ...

When exporting my schema's as a package and then importing them, I get errors...
ESLint: Unsafe member access .parseAsync on an `error` typed value.(@typescript-eslint/no-unsafe-member-access)
ESLint: Unsafe member access .parseAsync on an `error` typed value.(@typescript-eslint/no-unsafe-member-access)
...
Solution:
The basic issue is that typescript-eslint is getting an error from its own internal typescript compiler message. Usually restarting can help, but if it persists there might be a configuration issue in eslint or in the ide plugin

Whimsy - hey is there a way to tell zod what ki...

hey is there a way to tell zod what kind of schema shape i want from type, instead of opposite infering type from schema?
No description
Next