Zod Schema / getZodConstraint

getZodConstraint

A helper that returns an object containing the validation attributes for each field by introspecting the zod schema.

1const constraint = getZodConstraint(schema);

#Parameters

schema

The zod schema to be introspected.

#Example

1import { getZodConstraint } from '@conform-to/zod';
2// If you are using Zod v4, update the imports:
3// import { getZodConstraint } from '@conform-to/zod/v4';
4import { z } from 'zod';
5
6const schema = z.object({
7  title: z.string().min(5).max(20),
8  description: z.string().min(100).max(1000).optional(),
9  password: z
10    .string()
11    .regex(/[A-Z]/, 'Must contain an uppercase letter')
12    .regex(/[0-9]/, 'Must contain a number'),
13});
14const constraint = getZodConstraint(schema);
15// {
16//   title: { required: true, minLength: 5, maxLength: 20 },
17//   description: { required: false, minLength: 100, maxLength: 1000 },
18//   password: {
19//     required: true,
20//     pattern: '^(?=.*(?:[A-Z]))(?=.*(?:[0-9])).*$',
21//   },
22// }

#Tips

Pattern limitations

The pattern generation is best-effort with the following limitations:

  • Regex flags: The i (case-insensitive) flag is not supported. No pattern is generated for case-insensitive regexes or when the serialized regex is not a valid HTML pattern under the browser's v flag.
  • Backreferences: Numbered backreferences (\1, \2, etc.) may break when multiple regex validators for the same field are combined into one pattern. Named backreferences (\k<name>) are more reliable, but each regex combined into the same pattern must use unique group names.