How to set the error message in zod refine method?

I have the following Zod schema: const createApSchema = z .object({ name: z.string().min(1).max(32), isActive: z.boolean().default(true), description: z.string().max(200).optional(), ip: z.string().refine(validator.isIP), accessMode: AccessModeEnum, apiUsername: z.string().optional(), apiPassword: z.string().optional(), apiVersion: ApiVersionEnum, community: z.string().optional(), }) .refine((data) => { // check to see if AP type is snmp to force the community field const isSnmp = data.accessMode !== AccessModeEnumMap[AccessModeEnum.enum.mikrotikApi] if (isSnmp… Read More How to set the error message in zod refine method?

zod validation in javaScript

export type ResetPassword = { password: string confirmPassword: string } export const resetPasswordSchema: ZodType<ResetPassword> = z.object({ password: z.string().refine((value) => value !== ”, { message: ‘Password is required’ }), confirmPassword: z.string().refine((value) => value !== ”, { message: ‘Password is required’ }).refine((data) => data.password === data.confirmPassword, { message: "Passwords don’t match", path: ["confirmPassword"], // path of error… Read More zod validation in javaScript

Zod validation does not seem to trigger for required input

I have the following schema in Zod that requires a valid string input. However, I’m not sure the validation is actually kicking in? Just wondering what I’m doing wrong… const nameSchema = z.string({ required_error: "Name is required", invalid_type_error: "Name must be a string", }); console.log(nameSchema.safeParse("")); //Return the following object { "success": true, "data": "" }… Read More Zod validation does not seem to trigger for required input

How to merge multiple Zod (object) schema

I’ve 3 schema for a blog post. Image post Video post Text post Schema look like this // will be used for form validation // text can be added but image required const imagePostSchema = z.object({ body: z.string().max(500).optional().or(z.literal(”)), attachmentType: z.literal(‘img’), attachment: // z… image validation is here }); // text can be added but video… Read More How to merge multiple Zod (object) schema