Zod

Z

Zod

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

Join
Aadxvcasas5/3/2024

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. ```...
Sshadi4/28/2024

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
Ssquibler.4/21/2024

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 => {...
Aadxvcasas4/11/2024

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...
GGludek4/8/2024

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.
Kk93/29/2024

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:...
Kk93/26/2024

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
Rramblings3/19/2024

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
VAViệt An3/13/2024

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.
AAidan6473/11/2024

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...
AAidan6473/10/2024

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
RRhys2/15/2024

Rhys - const formSchema = z .object({ veh...

```const formSchema = z .object({ vehicleReg: z.string(), depot: z.string(), replacing: z.boolean().optional(),...
Solution:
If the flaky types from the refine approach is not a problem for you, it is probably the easier solution to maintain. By flaky types I am referring to the fact that you cannot do exhaustive checks with TS like you can with discriminated unions: ```ts // Refine approach const data = formSchema.parse(...);...
Message Not Public
Sign In & Join Server To View
Jjanglad2/8/2024

janglad - I may be missinng something here or m...

I may be missinng something here or maybe it's a TS limitation but I would expect the last line to give a TS error saying that type can never be "LISTING" (since owner.type is company). Why is this not happening? ```ts import z from "zod" ...
Solution:
Yeah, this is a typescript thing: an object's discriminant needs to be at the base level of that object.
Jjanglad2/7/2024

janglad - More of a general question? How do yo...

More of a general question? How do you guys organise your zod schemas? As my app grows I'm starting to get a lottt of schemas. Some of them are the basic schemas that closely relate to the database, but a lot of them are quite complex due to nested forms and often build on and reference many other schemas....
AAlex2/7/2024

Alex - Hi there, I am trying to validate a numb...

Hi there, I am trying to validate a number using zod. The number is first received as a string and could possibly be anything because it comes from a user input.
z.coerce.number().int("Invalid Value").positive("Must be a positive number").safeParse(maxQuantity);
z.coerce.number().int("Invalid Value").positive("Must be a positive number").safeParse(maxQuantity);
` This is my current schema, where maxQuantity is the said string. Now when I pass "d" as maxQuantity, I receive the error "Must be a positive number". How do I give an error that this is not even a number?...
Solution:
``` z .string() .regex(/^[-]?\d*.?\d+$/, "Must be a number") .pipe(z.coerce.number().int("Must be an integer").positive("Must be a positive number"))...
YYamereee1/29/2024

Yamereee - Does anyone know how I can type this...

Does anyone know how I can type this using zod? ```typescript export const SOME_MAPPING = { A1: ['AAA'],...
Ssegfault1/18/2024

segfault - Hey! I'm currently testing my schema...

Hey! I'm currently testing my schemas and have encountered what I believe to be a typescript type definition issue. I am examining error cases where parsing a string with a regex results in an error. My goal is to verify that the failure validation was due to the regex and not some other reason. However, when accessing the error.issues field, which has the ZodIssue type, it does not recognize the validation field. I can see that it is present when I use a simple console.log. Is there a workaround to access validation with type checking activated? Here is an example of the schema. It works fine in JavaScript or when TypeScript type checking is disabled. ``` import { z } from "zod";...
Solution:
Okay, I found the specific type definition for my case. The link to the error variants in the documentation page is broken (https://zod.dev/ERROR_HANDLING?id=zodissue), so it was a bit tricky.
Jjanglad1/2/2024

janglad - This seems like it might be worth a b...

This seems like it might be worth a bug report/request but does anyone know if there's a good way around this? I assume internally message is being evaluated as truthy/falsy and thus an empty string isn't picked up ```ts import z from 'zod'; ...
Jjanglad1/1/2024

janglad - For Next JS users, how do you guys ha...

For Next JS users, how do you guys handle setting a global error map? I'm not sure what the most elegant solution is here. .setErrorMap() does what I want it do but it'd ideally run once before anything else on both server boot/before first render on the client. Is it a bad idea to just call the function in my main layout (server side) and then have an empty client component above children there that also just calls it on every render (putting in a useEffect won't work as that runs after first render)...