Zod Schema: How to make a field optional OR have a minimum string contraint?

In your case, you consider “” to be the same as undefined (i.e.: when the string is empty, it’s like there’s no string at all). It’s implementable in Zod this way: import { z } from “zod”; import { strict as assert } from “node:assert”; // `myString` is a string that can be either optional … Read more

How to validate a string literal type using zod

I commented that this might be a duplicate since the core of the question could be solved with z.literal, but it is a bit different. Just to illustrate what you can do: import { z } from ‘zod’; const PaymentTypeSchema = z.union([ z.literal(‘CHECK’), z.literal(‘DIRECT DEPOSIT’), z.literal(‘MONEY ORDER’), ]); type PaymentType = z.infer<typeof PaymentTypeSchema>; const schema … Read more

How to check confirm password with zod

You can achieve this by tacking on a superRefine export const registerUserSchema = z.object({ firstName: z.string(), lastName: z.string(), userName: z.string(), email: z.string().email(), phone: z.string().transform(data => Number(data)), password: z.string().min(4), confirmPassword: z.string().min(4), avatar: z.string().optional(), isVerified: z.boolean().optional() }).superRefine(({ confirmPassword, password }, ctx) => { if (confirmPassword !== password) { ctx.addIssue({ code: “custom”, message: “The passwords did not match” … Read more

deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş