Zod

Z

Zod

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

Join

Arthur - tsexport const getServicesSchema = se...

```ts export const getServicesSchema = serviceSchema .omit({quantity: true, pinned: true}) .extend({ quantity: z.number(), pinned: z.stringbool().or(z.boolean()) }) .partial()...

neurotech - Hi everyone. I'm struggling with zo...

Hi everyone. I'm struggling with zod 4 and how it handles coercing values. This is my scenario: I have a form that contains a "Port" input that has a type of number. AFAICT, the typing of this input's value is a string, so I need zod to coerce it to number in order to validate properly against this schema:...
Solution:
Thanks @janglad - I ended up doing this:
z.coerce.number<number>()
z.coerce.number<number>()
...

Josh - hey, why the heck is this invalid?ts ...

hey, why the heck is this invalid? ```ts type ResponseSchema = (typeof API)[T]["responseSchema"]; const responseSchema: ResponseSchema = API[methodName].responseSchema; ...

neurotech - Similar to the above, I have an inp...

Similar to the above, I have an input element with a type of "number". How do I validate it's value as a number when using zod 4 in combination with react-hook-form? z.coerce's input type change to unknown seems to have made this more difficult.
Solution:
Indeed the same thing. Also same answer 😄 Either omit the genrics completely (@hookform/resolvers properly infers input/output now) or pass both input and output types

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

neurotech - Hi everyone, I just updated to zod ...

Hi everyone, I just updated to zod 4.0.15 and everything's working well except for one last remaining file in my project. I have a generic component, InputFormField, that can be imbued with a schema, i.e.: ```tsx <InputFormField<typeof FormSchema> control={form.control}...
Solution:
Ahhh... wondering if this might be a react-hook-form issue

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
Next