Zod

Z

Zod

Discussion for Zod, a runtime type library for TypeScript, and the surrounding ecosystem of libraries.

Join

semajy - Hello! I'm sort of new to typescript a...

Hello! I'm sort of new to typescript and Zod and I'm at the point where I have types that were generated based on my mysql schema (using kysely-codegen). Of course I also want to validate types at runtime made from server requests. Is it really the best way that I manually create Zod schemas based on these types?
Solution:
In actuality I only need Zod primarily for input validation from sources I don't have control over, which also means primarily validating a version of an entity or "type" that is either for updating or selecting

Tim Marcus Moore - I found that z.record evalua...

I found that z.record evaluates non-enumerable property keys. I'm not sure whether this is a bug or expected behaviour. It was unexpected to me. I ran into this validating a MobX observable object using a z.record with z.string() keys. MobX adds non-enumerable symbol properties, which caused parsing to fail. If it's a bug, I can open an issue on GitHub, but if it's intentional, I'll just adjust my expectations 🙂 This test demonstrates the problem....

MattIPv4 - :WaveDoggo: Dependeabot has opened a...

:Wave_Doggo: Dependeabot has opened a PR to update us to 3.25.x, but ESLint is failing due to import-x/no-unresolved being unable to resolve zod. Have others seen this and know how to get around it?
Solution:
3.25.13 fixed this, thanks

CrisOG - Hey! I just read Zod stable is coming ...

Hey! I just read Zod stable is coming out pretty soon - just wanted to know ultimately whats going to be the behavior for the case shown here? https://github.com/colinhacks/zod/issues/4251...
Solution:
This has been settled and the outcome was maintaining the backwards compatibility

Tango - I have the need to handle discriminated...

I have the need to handle discriminatedUnion and enums with an "Unkown" option. Is there a way to do something like that in a native way? ```typescript const Cat = z.object({ type: z.literal("Cat"), mood: z.string() }); const Dog = z.object({ type: z.literal("Dog") });...
Solution:
Thats fine, I guess either way, we need a bunch of dynamic code, if we like it to behave as desired. I think within code generation we have some options, but nothing really streight forward. Thanks again....

MHD Alaa - Hi everyone, first time here.I’m ju...

Hi everyone, first time here. I’m just wondering is there is a place to contribute to v4.zod.dev website. Like if there is typos in the provided examples, what is the proper way to point it out? ...
Solution:
Hi, you need to fork the repo and then raise MR to the zod repo. All the work is being done under v4 branch (have a look on github) thanks for trying to contribute to Zod
Message Not Public
Sign In & Join Server To View

MattIPv4 - :WaveDoggo: I think this might be a ...

:Wave_Doggo: I think this might be a Zod Q at this point rather than TypeScript itself -- I have a custom type that relies on import() types. I've figured out how to partially preserve them when generating declaration files, but I'm still getting some unknowns included instead of references to the import() types.
Solution:
Ahah! ```ts type ImagePng = (typeof import(".png"))["default"]; type ImageJpg = (typeof import(".jpg"))["default"];...

Maxiviper117 - I'm working on validating an obj...

I'm working on validating an object where: - Some property keys are known and expected. - Other extra/unknown keys are allowed only if they follow a specific pattern. ...
Solution:
If you wish to get more type safety, here is something: https://tsplay.dev/N5oVdW

MattIPv4 - :WaveDoggo: Hey Zod experts. Given a...

:Wave_Doggo: Hey Zod experts. Given an arbitrary Zod schema, is there a way I can strip refinements off any string types?

Gludek - Hey,Quick question, is there any way ...

Hey, Quick question, is there any way to validate blobs/file uploads? TBH I'm not even sure if it even makes sense to do....

Leozinn - const AuthorSchema = z.object({ ...

``` const AuthorSchema = z.object({ name: z.string().max(256), url: z.string().url().optional().nullable(), icon_url: z.string().url().optional().nullable(),...
No description

Leozinn - i'm compiled my project, and interfac...

i'm compiled my project, and interfaces.d.ts return this... How am I going to use typing in my project, if it is like a zod object?...
No description

hinogi - tsconst a = z.object({ foo: z.strin...

```ts const a = z.object({ foo: z.string() }) ...
Solution:
ok so foo has to be foo: null | undefined and {} is not a valid solutions. also, if null, the key will persist, if undefined the key is gone 😄 thanks playground

versace - hey!I am currently investigating wh...

hey! I am currently investigating what the best approach for using header validation through zod would be in regards to key case sensitivity. i'm currently in a situation where I need to have support for all headers to be supported regardless of how the casing looks when sent in from the request. any suggestions?...
Solution:
Do you need to preserve the case of the incoming headers? You could transform them at runtime with .toLowerCase() and then build your schema on the lowercased keys.

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([...