Skip to content

Commit 619555f

Browse files
committed
Change Zod schemas to Valibot in website
1 parent 7f4f31f commit 619555f

File tree

10 files changed

+172
-129
lines changed

10 files changed

+172
-129
lines changed

playgrounds/qwik/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
},
2626
"dependencies": {
2727
"@modular-forms/qwik": "workspace:*",
28-
"clsx": "^1.2.1"
28+
"clsx": "^1.2.1",
29+
"valibot": "^0.1.0"
2930
},
3031
"devDependencies": {
3132
"@builder.io/qwik": "1.1.5",

playgrounds/qwik/src/routes/actions/login/index.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@ import { component$ } from '@builder.io/qwik';
22
import {
33
globalAction$,
44
routeLoader$,
5-
z,
65
type DocumentHead,
76
} from '@builder.io/qwik-city';
87
import {
98
formAction$,
109
type InitialValues,
1110
useForm,
12-
zodForm$,
11+
valiForm$,
1312
} from '@modular-forms/qwik';
13+
import { email, type Input, minLength, object, string } from 'valibot';
1414
import { FormHeader, TextInput, FormFooter, Response } from '~/components';
1515

16-
const loginSchema = z.object({
17-
email: z
18-
.string()
19-
.min(1, 'Please enter your email.')
20-
.email('The email address is badly formatted.'),
21-
password: z
22-
.string()
23-
.min(1, 'Please enter your password.')
24-
.min(8, 'You password must have 8 characters or more.'),
16+
const LoginSchema = object({
17+
email: string([
18+
minLength(1, 'Please enter your email.'),
19+
email('The email address is badly formatted.'),
20+
]),
21+
password: string([
22+
minLength(1, 'Please enter your password.'),
23+
minLength(8, 'You password must have 8 characters or more.'),
24+
]),
2525
});
2626

27-
type LoginForm = z.input<typeof loginSchema>;
27+
type LoginForm = Input<typeof LoginSchema>;
2828

2929
const getInitFormValues = (): InitialValues<LoginForm> => ({
3030
email: '',
@@ -45,14 +45,14 @@ export const useResetFormAction = globalAction$(() => {
4545
export const useFormAction = formAction$<LoginForm>((values) => {
4646
// Runs on server
4747
console.log(values);
48-
}, zodForm$(loginSchema));
48+
}, valiForm$(LoginSchema));
4949

5050
export default component$(() => {
5151
// Use login form
5252
const [loginForm, { Form, Field }] = useForm<LoginForm>({
5353
loader: useFormLoader(),
5454
action: useFormAction(),
55-
validate: zodForm$(loginSchema),
55+
validate: valiForm$(LoginSchema),
5656
});
5757

5858
// Use reset form action

playgrounds/qwik/src/routes/actions/special/index.tsx

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
import { component$, noSerialize, type NoSerialize } from '@builder.io/qwik';
1+
import { component$, type NoSerialize } from '@builder.io/qwik';
22
import {
33
type DocumentHead,
44
routeLoader$,
5-
z,
65
globalAction$,
76
} from '@builder.io/qwik-city';
87
import {
98
type InitialValues,
109
useForm,
11-
zodForm$,
10+
valiForm$,
1211
formAction$,
1312
} from '@modular-forms/qwik';
13+
import {
14+
array,
15+
boolean,
16+
type Input,
17+
number,
18+
object,
19+
optional,
20+
special,
21+
string,
22+
} from 'valibot';
1423
import {
1524
FormHeader,
1625
TextInput,
@@ -22,30 +31,26 @@ import {
2231
Response,
2332
} from '~/components';
2433

25-
const isBlob = (value: unknown) =>
26-
!!value && typeof value === 'object' && 'size' in value && 'type' in value;
27-
28-
const isBlobArray = (value: unknown) =>
29-
Array.isArray(value) && value.every(isBlob);
34+
const isBlob = (input: unknown) => input instanceof Blob;
3035

31-
const specialSchema = z.object({
32-
number: z.number(),
33-
range: z.number(),
34-
checkbox: z.object({
35-
array: z.array(z.string()),
36-
boolean: z.boolean(),
36+
const SpecialSchema = object({
37+
number: number(),
38+
range: number(),
39+
checkbox: object({
40+
array: array(string()),
41+
boolean: boolean(),
3742
}),
38-
select: z.object({
39-
array: z.array(z.string()),
40-
string: z.string().optional(),
43+
select: object({
44+
array: array(string()),
45+
string: optional(string()),
4146
}),
42-
file: z.object({
43-
list: z.custom<NoSerialize<Blob[]>>(isBlobArray),
44-
item: z.custom<NoSerialize<Blob>>(isBlob).optional(),
47+
file: object({
48+
list: array(special<NoSerialize<Blob>>(isBlob)),
49+
item: optional(special<NoSerialize<Blob>>(isBlob)),
4550
}),
4651
});
4752

48-
type SpecialForm = z.input<typeof specialSchema>;
53+
type SpecialForm = Input<typeof SpecialSchema>;
4954

5055
const getInitFormValues = (): InitialValues<SpecialForm> => ({
5156
number: 0,
@@ -59,7 +64,7 @@ const getInitFormValues = (): InitialValues<SpecialForm> => ({
5964
string: undefined,
6065
},
6166
file: {
62-
list: noSerialize([]),
67+
list: [],
6368
item: undefined,
6469
},
6570
});
@@ -81,7 +86,7 @@ export const useFormAction = formAction$<SpecialForm>(
8186
console.log(values);
8287
},
8388
{
84-
validate: zodForm$(specialSchema),
89+
validate: valiForm$(SpecialSchema),
8590
arrays: ['checkbox.array', 'file.list', 'select.array'],
8691
booleans: ['checkbox.boolean'],
8792
files: ['file.item', 'file.list'],
@@ -94,6 +99,7 @@ export default component$(() => {
9499
const [specialForm, { Form, Field }] = useForm<SpecialForm>({
95100
loader: useFormLoader(),
96101
action: useFormAction(),
102+
validate: valiForm$(SpecialSchema),
97103
});
98104

99105
// Use reset form action

playgrounds/qwik/src/routes/actions/todos/index.tsx

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
Form as ActionForm,
44
globalAction$,
55
routeLoader$,
6-
z,
76
type DocumentHead,
87
} from '@builder.io/qwik-city';
98
import {
@@ -14,10 +13,19 @@ import {
1413
replace,
1514
swap,
1615
useForm,
17-
zodForm$,
16+
valiForm$,
1817
formAction$,
1918
getValues,
2019
} from '@modular-forms/qwik';
20+
import {
21+
array,
22+
type Input,
23+
maxLength,
24+
minLength,
25+
object,
26+
string,
27+
isoDate,
28+
} from 'valibot';
2129
import {
2230
TextInput,
2331
InputLabel,
@@ -28,26 +36,24 @@ import {
2836
Response,
2937
} from '~/components';
3038

31-
const todoSchema = z.object({
32-
heading: z.string().min(1, 'Please enter a heading.'),
33-
todos: z
34-
.array(
35-
z.object({
36-
label: z.string().min(1, 'Please enter a label.'),
37-
deadline: z
38-
.string()
39-
.min(1, 'Please enter a deadline.')
40-
.regex(
41-
/^([0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])$/,
42-
'The specified date is invalid.'
43-
),
44-
})
45-
)
46-
.min(1, 'Please add at least one todo.')
47-
.max(4, 'You can add a maximum of 4 todos.'),
39+
const TodoSchema = object({
40+
heading: string([minLength(1, 'Please enter a heading.')]),
41+
todos: array(
42+
object({
43+
label: string([minLength(1, 'Please enter a label.')]),
44+
deadline: string([
45+
minLength(1, 'Please enter a deadline.'),
46+
isoDate('The specified date is invalid.'),
47+
]),
48+
}),
49+
[
50+
minLength(1, 'Please add at least one todo.'),
51+
maxLength(4, 'You can add a maximum of 4 todos.'),
52+
]
53+
),
4854
});
4955

50-
type TodoForm = z.input<typeof todoSchema>;
56+
type TodoForm = Input<typeof TodoSchema>;
5157

5258
const getInitFormValues = (): InitialValues<TodoForm> => ({
5359
heading: 'Shopping list',
@@ -119,7 +125,7 @@ export const useFormAction = formAction$<TodoForm>(
119125
todoFormValues = values;
120126
},
121127
{
122-
validate: zodForm$(todoSchema),
128+
validate: valiForm$(TodoSchema),
123129
arrays: ['todos'],
124130
}
125131
);
@@ -129,7 +135,7 @@ export default component$(() => {
129135
const [todoForm, { Form, Field, FieldArray }] = useForm<TodoForm>({
130136
loader: useFormLoader(),
131137
action: useFormAction(),
132-
validate: zodForm$(todoSchema),
138+
validate: valiForm$(TodoSchema),
133139
fieldArrays: ['todos'],
134140
});
135141

pnpm-lock.yaml

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)