Skip to content

Commit bd7a65e

Browse files
committed
Tests working for issue 633.
1 parent fc1ef6d commit bd7a65e

File tree

1 file changed

+217
-77
lines changed

1 file changed

+217
-77
lines changed

src/tests/issue-633.test.ts

Lines changed: 217 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,268 @@
11
import { describe, it, expect } from 'vitest';
22
import { superValidate } from '$lib/superValidate.js';
3-
import { zod } from '$lib/adapters/zod.js';
4-
import { z } from 'zod/v3';
3+
import { zod as zod3 } from '$lib/adapters/zod.js';
4+
import { zod as zod4 } from '$lib/adapters/zod4.js';
5+
import { z as z3 } from 'zod/v3';
6+
import { z as z4 } from 'zod/v4';
57

68
describe('Issue #633 - Boolean default value with URL vs FormData validation', () => {
7-
it('should return true for boolean field with .default(true) when validating empty URL', async () => {
8-
// Schema with boolean default set to true
9-
const formSchema = z.object({
10-
orderby: z.boolean().default(true)
9+
describe('Zod v3', () => {
10+
it('should return true for boolean field with .default(true) when validating empty URL', async () => {
11+
// Schema with boolean default set to true
12+
const formSchema = z3.object({
13+
orderby: z3.boolean().default(true)
14+
});
15+
16+
// Create URL with no searchParams
17+
const url = new URL('http://test.com');
18+
19+
// Validate URL with schema
20+
const form = await superValidate(url, zod3(formSchema));
21+
22+
// The boolean field should default to true, not false
23+
expect(form.data.orderby).toBe(true);
1124
});
1225

13-
// Create URL with no searchParams
14-
const url = new URL('http://test.com');
26+
it('should respect explicit false value in URL searchParams', async () => {
27+
// Schema with boolean default set to true
28+
const formSchema = z3.object({
29+
orderby: z3.boolean().default(true)
30+
});
1531

16-
// Validate URL with schema
17-
const form = await superValidate(url, zod(formSchema));
32+
// Create URL with orderby=false
33+
const url = new URL('http://test.com?orderby=false');
1834

19-
// The boolean field should default to true, not false
20-
expect(form.data.orderby).toBe(true);
21-
});
35+
// Validate URL with schema
36+
const form = await superValidate(url, zod3(formSchema));
2237

23-
it('should respect explicit false value in URL searchParams', async () => {
24-
// Schema with boolean default set to true
25-
const formSchema = z.object({
26-
orderby: z.boolean().default(true)
38+
// The explicit false value should be preserved
39+
expect(form.data.orderby).toBe(false);
2740
});
2841

29-
// Create URL with orderby=false
30-
const url = new URL('http://test.com?orderby=false');
42+
it('should use true default when URL has other params but not orderby', async () => {
43+
// Schema with boolean default set to true
44+
const formSchema = z3.object({
45+
orderby: z3.boolean().default(true),
46+
search: z3.string().optional()
47+
});
3148

32-
// Validate URL with schema
33-
const form = await superValidate(url, zod(formSchema));
49+
// Create URL with other params but not orderby
50+
const url = new URL('http://test.com?search=test');
3451

35-
// The explicit false value should be preserved
36-
expect(form.data.orderby).toBe(false);
37-
});
52+
// Validate URL with schema
53+
const form = await superValidate(url, zod3(formSchema));
3854

39-
it('should use true default when URL has other params but not orderby', async () => {
40-
// Schema with boolean default set to true
41-
const formSchema = z.object({
42-
orderby: z.boolean().default(true),
43-
search: z.string().optional()
55+
// The boolean field should default to true
56+
expect(form.data.orderby).toBe(true);
57+
expect(form.data.search).toBe('test');
4458
});
4559

46-
// Create URL with other params but not orderby
47-
const url = new URL('http://test.com?search=test');
60+
// FormData tests - different behavior expected
61+
describe('FormData validation', () => {
62+
it('should return false for boolean field with .default(true) when validating empty FormData', async () => {
63+
// Schema with boolean default set to true
64+
const formSchema = z3.object({
65+
orderby: z3.boolean().default(true)
66+
});
67+
68+
// Create empty FormData
69+
const formData = new FormData();
70+
71+
// Validate FormData with schema
72+
const form = await superValidate(formData, zod3(formSchema));
73+
74+
// For FormData, missing boolean should return false (HTML form behavior)
75+
expect(form.data.orderby).toBe(false);
76+
});
77+
78+
it('should respect explicit false value in FormData', async () => {
79+
// Schema with boolean default set to true
80+
const formSchema = z3.object({
81+
orderby: z3.boolean().default(true)
82+
});
83+
84+
// Create FormData with orderby=false
85+
const formData = new FormData();
86+
formData.set('orderby', 'false');
4887

49-
// Validate URL with schema
50-
const form = await superValidate(url, zod(formSchema));
88+
// Validate FormData with schema
89+
const form = await superValidate(formData, zod3(formSchema));
90+
91+
// The explicit false value should be preserved
92+
expect(form.data.orderby).toBe(false);
93+
});
94+
95+
it('should return true for explicit true value in FormData', async () => {
96+
// Schema with boolean default set to true
97+
const formSchema = z3.object({
98+
orderby: z3.boolean().default(true)
99+
});
100+
101+
// Create FormData with orderby=true
102+
const formData = new FormData();
103+
formData.set('orderby', 'true');
104+
105+
// Validate FormData with schema
106+
const form = await superValidate(formData, zod3(formSchema));
107+
108+
// The explicit true value should be preserved
109+
expect(form.data.orderby).toBe(true);
110+
});
51111

52-
// The boolean field should default to true
53-
expect(form.data.orderby).toBe(true);
54-
expect(form.data.search).toBe('test');
112+
it('should return false when FormData has other fields but not orderby', async () => {
113+
// Schema with boolean default set to true
114+
const formSchema = z3.object({
115+
orderby: z3.boolean().default(true),
116+
search: z3.string().optional()
117+
});
118+
119+
// Create FormData with other fields but not orderby
120+
const formData = new FormData();
121+
formData.set('search', 'test');
122+
123+
const adapter = zod3(formSchema);
124+
// console.log('===ZOD3===');
125+
// console.dir(adapter.jsonSchema, { depth: 10 }); //debug
126+
127+
// Validate FormData with schema
128+
const form = await superValidate(formData, adapter);
129+
130+
// For FormData, missing boolean should return false (HTML form behavior)
131+
expect(form.data.orderby).toBe(false);
132+
expect(form.data.search).toBe('test');
133+
});
134+
});
55135
});
56136

57-
// FormData tests - different behavior expected
58-
describe('FormData validation', () => {
59-
it('should return false for boolean field with .default(true) when validating empty FormData', async () => {
137+
describe('Zod v4', () => {
138+
it('should return true for boolean field with .default(true) when validating empty URL', async () => {
60139
// Schema with boolean default set to true
61-
const formSchema = z.object({
62-
orderby: z.boolean().default(true)
140+
const formSchema = z4.object({
141+
orderby: z4.boolean().default(true)
63142
});
64143

65-
// Create empty FormData
66-
const formData = new FormData();
144+
// Create URL with no searchParams
145+
const url = new URL('http://test.com');
67146

68-
// Validate FormData with schema
69-
const form = await superValidate(formData, zod(formSchema));
147+
// Validate URL with schema
148+
const form = await superValidate(url, zod4(formSchema));
70149

71-
// For FormData, missing boolean should return false (HTML form behavior)
72-
expect(form.data.orderby).toBe(false);
150+
// The boolean field should default to true, not false
151+
expect(form.data.orderby).toBe(true);
73152
});
74153

75-
it('should respect explicit false value in FormData', async () => {
154+
it('should respect explicit false value in URL searchParams', async () => {
76155
// Schema with boolean default set to true
77-
const formSchema = z.object({
78-
orderby: z.boolean().default(true)
156+
const formSchema = z4.object({
157+
orderby: z4.boolean().default(true)
79158
});
80159

81-
// Create FormData with orderby=false
82-
const formData = new FormData();
83-
formData.set('orderby', 'false');
160+
// Create URL with orderby=false
161+
const url = new URL('http://test.com?orderby=false');
84162

85-
// Validate FormData with schema
86-
const form = await superValidate(formData, zod(formSchema));
163+
// Validate URL with schema
164+
const form = await superValidate(url, zod4(formSchema));
87165

88166
// The explicit false value should be preserved
89167
expect(form.data.orderby).toBe(false);
90168
});
91169

92-
it('should return true for explicit true value in FormData', async () => {
170+
it('should use true default when URL has other params but not orderby', async () => {
93171
// Schema with boolean default set to true
94-
const formSchema = z.object({
95-
orderby: z.boolean().default(true)
172+
const formSchema = z4.object({
173+
orderby: z4.boolean().default(true),
174+
search: z4.string().optional()
96175
});
97176

98-
// Create FormData with orderby=true
99-
const formData = new FormData();
100-
formData.set('orderby', 'true');
177+
// Create URL with other params but not orderby
178+
const url = new URL('http://test.com?search=test');
101179

102-
// Validate FormData with schema
103-
const form = await superValidate(formData, zod(formSchema));
180+
// Validate URL with schema
181+
const form = await superValidate(url, zod4(formSchema));
104182

105-
// The explicit true value should be preserved
183+
// The boolean field should default to true
106184
expect(form.data.orderby).toBe(true);
185+
expect(form.data.search).toBe('test');
107186
});
108187

109-
it('should return false when FormData has other fields but not orderby', async () => {
110-
// Schema with boolean default set to true
111-
const formSchema = z.object({
112-
orderby: z.boolean().default(true),
113-
search: z.string().optional()
188+
// FormData tests - different behavior expected
189+
// This is a breaking change between Zod 3 and 4:
190+
// See https://github.com/colinhacks/zod/issues/4883
191+
describe('FormData validation', () => {
192+
it('should return true for boolean field with .default(true) when validating empty FormData', async () => {
193+
// Schema with boolean default set to true
194+
const formSchema = z4.object({
195+
orderby: z4.boolean().default(true)
196+
});
197+
198+
// Create empty FormData
199+
const formData = new FormData();
200+
201+
// Validate FormData with schema
202+
const form = await superValidate(formData, zod4(formSchema));
203+
204+
// For FormData, missing boolean should return false (HTML form behavior)
205+
expect(form.data.orderby).toBe(true);
114206
});
115207

116-
// Create FormData with other fields but not orderby
117-
const formData = new FormData();
118-
formData.set('search', 'test');
208+
it('should respect explicit false value in FormData', async () => {
209+
// Schema with boolean default set to true
210+
const formSchema = z4.object({
211+
orderby: z4.boolean().default(true)
212+
});
119213

120-
// Validate FormData with schema
121-
const form = await superValidate(formData, zod(formSchema));
214+
// Create FormData with orderby=false
215+
const formData = new FormData();
216+
formData.set('orderby', 'false');
122217

123-
// For FormData, missing boolean should return false (HTML form behavior)
124-
expect(form.data.orderby).toBe(false);
125-
expect(form.data.search).toBe('test');
218+
// Validate FormData with schema
219+
const form = await superValidate(formData, zod4(formSchema));
220+
221+
// The explicit false value should be preserved
222+
expect(form.data.orderby).toBe(false);
223+
});
224+
225+
it('should return true for explicit true value in FormData', async () => {
226+
// Schema with boolean default set to true
227+
const formSchema = z4.object({
228+
orderby: z4.boolean().default(true)
229+
});
230+
231+
// Create FormData with orderby=true
232+
const formData = new FormData();
233+
formData.set('orderby', 'true');
234+
235+
// Validate FormData with schema
236+
const form = await superValidate(formData, zod4(formSchema));
237+
238+
// The explicit true value should be preserved
239+
expect(form.data.orderby).toBe(true);
240+
});
241+
242+
// This is a breaking change between Zod 3 and 4:
243+
// See https://github.com/colinhacks/zod/issues/4883
244+
it('should return true when FormData has other fields but not orderby', async () => {
245+
// Schema with boolean default set to true
246+
const formSchema = z4.object({
247+
orderby: z4.boolean().default(true),
248+
search: z4.string().optional()
249+
});
250+
251+
// Create FormData with other fields but not orderby
252+
const formData = new FormData();
253+
formData.set('search', 'test');
254+
255+
const adapter = zod4(formSchema);
256+
// console.log('===ZOD4===');
257+
// console.dir(adapter.jsonSchema, { depth: 10 }); //debug
258+
259+
// Validate FormData with schema
260+
const form = await superValidate(formData, adapter);
261+
262+
// For FormData, missing boolean should return false (HTML form behavior)
263+
expect(form.data.orderby).toBe(true);
264+
expect(form.data.search).toBe('test');
265+
});
126266
});
127267
});
128268
});

0 commit comments

Comments
 (0)