|
1 | 1 | import { describe, it, expect } from 'vitest'; |
2 | 2 | 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'; |
5 | 7 |
|
6 | 8 | 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); |
11 | 24 | }); |
12 | 25 |
|
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 | + }); |
15 | 31 |
|
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'); |
18 | 34 |
|
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)); |
22 | 37 |
|
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); |
27 | 40 | }); |
28 | 41 |
|
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 | + }); |
31 | 48 |
|
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'); |
34 | 51 |
|
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)); |
38 | 54 |
|
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'); |
44 | 58 | }); |
45 | 59 |
|
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'); |
48 | 87 |
|
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 | + }); |
51 | 111 |
|
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 | + }); |
55 | 135 | }); |
56 | 136 |
|
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 () => { |
60 | 139 | // 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) |
63 | 142 | }); |
64 | 143 |
|
65 | | - // Create empty FormData |
66 | | - const formData = new FormData(); |
| 144 | + // Create URL with no searchParams |
| 145 | + const url = new URL('http://test.com'); |
67 | 146 |
|
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)); |
70 | 149 |
|
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); |
73 | 152 | }); |
74 | 153 |
|
75 | | - it('should respect explicit false value in FormData', async () => { |
| 154 | + it('should respect explicit false value in URL searchParams', async () => { |
76 | 155 | // 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) |
79 | 158 | }); |
80 | 159 |
|
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'); |
84 | 162 |
|
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)); |
87 | 165 |
|
88 | 166 | // The explicit false value should be preserved |
89 | 167 | expect(form.data.orderby).toBe(false); |
90 | 168 | }); |
91 | 169 |
|
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 () => { |
93 | 171 | // 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() |
96 | 175 | }); |
97 | 176 |
|
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'); |
101 | 179 |
|
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)); |
104 | 182 |
|
105 | | - // The explicit true value should be preserved |
| 183 | + // The boolean field should default to true |
106 | 184 | expect(form.data.orderby).toBe(true); |
| 185 | + expect(form.data.search).toBe('test'); |
107 | 186 | }); |
108 | 187 |
|
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); |
114 | 206 | }); |
115 | 207 |
|
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 | + }); |
119 | 213 |
|
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'); |
122 | 217 |
|
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 | + }); |
126 | 266 | }); |
127 | 267 | }); |
128 | 268 | }); |
0 commit comments