getValibotConstraint
A helper that returns an object containing the validation attributes for each field by introspecting the valibot schema.
1const constraint = getValibotConstraint(schema);#Parameters
schema
The valibot schema to be introspected.
#Example
1import { getValibotConstraint } from '@conform-to/valibot';
2import {
3 maxLength,
4 minLength,
5 object,
6 optional,
7 pipe,
8 regex,
9 string,
10} from 'valibot';
11
12const schema = object({
13 title: pipe(string(), minLength(5), maxLength(20)),
14 description: optional(pipe(string(), minLength(100), maxLength(1000))),
15 password: pipe(
16 string(),
17 regex(/[A-Z]/, 'Must contain an uppercase letter'),
18 regex(/[0-9]/, 'Must contain a number'),
19 ),
20});
21const constraint = getValibotConstraint(schema);
22// {
23// title: { required: true, minLength: 5, maxLength: 20 },
24// description: { required: false, minLength: 100, maxLength: 1000 },
25// password: {
26// required: true,
27// pattern: '^(?=.*(?:[A-Z]))(?=.*(?:[0-9])).*$',
28// },
29// }#Tips
Pattern limitations
The pattern generation is best-effort with the following limitations:
- Regex flags: The
i(case-insensitive) flag is not supported. Nopatternis generated for case-insensitive regexes or when the serialized regex is not a valid HTMLpatternunder the browser'svflag. - Backreferences: Numbered backreferences (
\1,\2, etc.) may break when multiple regex validators for the same field are combined into onepattern. Named backreferences (\k<name>) are more reliable, but each regex combined into the samepatternmust use unique group names.