Zod

Z

Zod

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

Join

ménard - I'm thinking of something like:typesc...

Hi, is there a way without going out of band to make one field always have the same value as another even when it's not part of the input object? I'm thinking of something like: ```typescript const vpm = z.object({...

Haddok - Hi everyone , i have to use zod in my ...

Hi everyone , i have to use zod in my javascript environment i have defined a schema , that has multiple fields the entire schema validation works perfectly , but for javascript i am not sure how to use ...
Solution:
Hi there , thank you for your answer So basically i have a schema like this ```js ...

doug4794 - I have this login schema and want to...

I have this login schema and want to display my own error message. ```ts export const loginSchema = z.object({ password: z.string().min(8, { message: i18n("errors.password-too-short") }),...

Luan Mário - how to make an input of type date ...

how to make an input of type date optional?

QBit - Form should not accept decimal nos. For...

Form should not accept decimal nos. For <=0 numbers code is written to not accept them. Now it accepting positive decimal also but I don't want. What to do? ```ts "use client" ...
Solution:
found the solution
ID: z.coerce.number().int().positive().finite()
ID: z.coerce.number().int().positive().finite()
...

Bence (Arzen) - Hey guys 🙂I have a (might be)...

Hey guys 🙂 I have a (might be) silly question. I have a schema exported as module: ```ts // schema.ts...

Gludek - Hey,I have objects like this (there's...

Hey, I have objects like this (there's little bit more nesting), but for some reason the superRefine doesn't trigger for guardian's BaseData, only for the patient ```js const baseData = z .object({...

adxvcasas - How to set a default value for opti...

How to set a default value for optional values? Hello, so I'm trying to set a default value for my schema, is this approach acceptable? ```...

Gludek - Just to confirm z.string().email() can...

Just to confirm z.string().email() can't be combined with .nullish() right?
Solution:
Perhaps you're looking for:
const schema = z.literal("").or(z.string().email());
const schema = z.literal("").or(z.string().email());
...
Message Not Public
Sign In & Join Server To View

adxvcasas - Hello, so right now I have checkbox...

Hello, so right now I have checkbox wherein I can store multiple strings inside an array, but the problem i'm encountering right now is when I'm storing it to my database mysql using prisma orm , I get a warning of Field "doesHaveTCETAssitance" in model "AppoinmentSchedule" can't be a list. The current connector does not support lists of primitive types.Prisma, because I set doesHaveTCETAssitance to doesHaveTCETAssitance String[] which is not supported by prisma mysql. ```...

shadi - hey everyone, i've been struggling with...

hey everyone, i've been struggling with building a recursive zod schema. I tried following the docs: https://github.com/colinhacks/zod?tab=readme-ov-file#recursive-types but still can't figure it out for my use case. here is what I'm trying to achieve: ```ts const BranchSchema = z.object({ prompt: BranchPromptSchema, // regular object schema...
Solution:
You have to manually type either direction of the types, for example: ```ts const ActionSchema = z.object({ ... }); const BranchPromptSchema = z.object({ ... });...
Message Not Public
Sign In & Join Server To View

squibler. - Hi folks. I'm looking to validate a...

Hi folks. I'm looking to validate that a user input is not in a blacklist - like for example swear words. Enum does this for allowed strings, but I'm struggling to find something that will work for disallowed strings. I have tried a regex - but my datasource has over 2000 disallowed words and regex would be too slow. Basically I'm looking for something like:...
Solution:
Using this now: ```ts import { blacklist } from '~/blacklist'; export const notBlacklisted = (input: string): boolean => {...

adxvcasas - Hello everyone, How can I make the ...

Hello everyone, How can I make the dryRunDate ,dryRunStart and dryRunEnd dynamic? so I have a type who have a value of yes and no. Is it possible to make dryRunDate ,dryRunStart and dryRunEnd required if the user select yes from type? this is my zod...

Gludek - Heey, can I somehow extend/merge discr...

Heey, can I somehow extend/merge discriminated union with object? my union ```js z .discriminatedUnion('ssnMode', [...
Solution:
Not currently possible. There are work arounds though, but it requires you to redefine the union every time.

k9 - Is it expected that Zod would cause such a...

Is it expected that Zod would cause such a spike in build memory consumption? This is an interesting one. I've been able to deploy to Heroku for years. Now Heroku won't build my application because the Typescript compiler runs out of memory. The only meaningful change I can think of is introducing Zod. I deploy once every few days, so it might not be the case. Heroku configuration seems to use the same environment variables for compilation and deployment, (Wow do I have security questions about that one.) The important thing is that our runtime app caps memory at 350: NODE_OPTIONS=--max-old-space-size=350. But now the compilation phase dies with this error:...

k9 - Hello! I'm getting up to speed. The Comple...

Hello! I'm getting up to speed. The Complex Schema Validation section of zod-tutorial talks about how to preresent what would be a union type in a schema. That doesn't quite do what I'm looking for I have defined types with a string template type e.g. ```...
Solution:
Should just be the following: ``ts type GameId = g${string}`; const isGameId = (value: unknown): value is GameId => {...
Message Not Public
Sign In & Join Server To View

ramblings - Is there a way to make one field re...

Is there a way to make one field readonly instead of the entire schema? After I parse a data with the schema, I don't want to allow changing the ID.
No description

Việt An - How to get better error messages?I h...

How to get better error messages? I have an union z.union([z.literal('Conan'), z.literal('Jordan'), z.literal('Sona')]), when the value is not any of that, the default error message I get is Invalid literal value, expected "Conan", but I want it to say something like ...expected "Conan", "Jordan" or "Sona". How can I do that?...
Solution:
Hmm, I actually don't know the answer off the top of my head. z.union is similar to z.object or z.array in that is a kind of "container" schema that runs the interior schemas and collects any errors.

Aidan647 - how can I validate other1 and other2...

how can I validate other1 and other2? (function validation is optional) ```ts type demoFunction = { (data: string): number[] other1: number...

Aidan647 - how can I do validation of zod in zo...

how can I do validation of zod in zod ```ts z.object({ description: z.string(), example: z.string(),...
Solution:
I think something like this should work: ```ts z.object({ description: z.string(), example: z.string(),...
Message Not Public
Sign In & Join Server To View
Next