|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { superValidate } from '$lib/superValidate.js'; |
| 3 | +import { zod } from '$lib/adapters/zod.js'; |
| 4 | +import { z } from 'zod/v3'; |
| 5 | + |
| 6 | +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) |
| 11 | + }); |
| 12 | + |
| 13 | + // Create URL with no searchParams |
| 14 | + const url = new URL('http://test.com'); |
| 15 | + |
| 16 | + // Validate URL with schema |
| 17 | + const form = await superValidate(url, zod(formSchema)); |
| 18 | + |
| 19 | + // The boolean field should default to true, not false |
| 20 | + expect(form.data.orderby).toBe(true); |
| 21 | + }); |
| 22 | + |
| 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) |
| 27 | + }); |
| 28 | + |
| 29 | + // Create URL with orderby=false |
| 30 | + const url = new URL('http://test.com?orderby=false'); |
| 31 | + |
| 32 | + // Validate URL with schema |
| 33 | + const form = await superValidate(url, zod(formSchema)); |
| 34 | + |
| 35 | + // The explicit false value should be preserved |
| 36 | + expect(form.data.orderby).toBe(false); |
| 37 | + }); |
| 38 | + |
| 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() |
| 44 | + }); |
| 45 | + |
| 46 | + // Create URL with other params but not orderby |
| 47 | + const url = new URL('http://test.com?search=test'); |
| 48 | + |
| 49 | + // Validate URL with schema |
| 50 | + const form = await superValidate(url, zod(formSchema)); |
| 51 | + |
| 52 | + // The boolean field should default to true |
| 53 | + expect(form.data.orderby).toBe(true); |
| 54 | + expect(form.data.search).toBe('test'); |
| 55 | + }); |
| 56 | + |
| 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 () => { |
| 60 | + // Schema with boolean default set to true |
| 61 | + const formSchema = z.object({ |
| 62 | + orderby: z.boolean().default(true) |
| 63 | + }); |
| 64 | + |
| 65 | + // Create empty FormData |
| 66 | + const formData = new FormData(); |
| 67 | + |
| 68 | + // Validate FormData with schema |
| 69 | + const form = await superValidate(formData, zod(formSchema)); |
| 70 | + |
| 71 | + // For FormData, missing boolean should return false (HTML form behavior) |
| 72 | + expect(form.data.orderby).toBe(false); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should respect explicit false value in FormData', async () => { |
| 76 | + // Schema with boolean default set to true |
| 77 | + const formSchema = z.object({ |
| 78 | + orderby: z.boolean().default(true) |
| 79 | + }); |
| 80 | + |
| 81 | + // Create FormData with orderby=false |
| 82 | + const formData = new FormData(); |
| 83 | + formData.set('orderby', 'false'); |
| 84 | + |
| 85 | + // Validate FormData with schema |
| 86 | + const form = await superValidate(formData, zod(formSchema)); |
| 87 | + |
| 88 | + // The explicit false value should be preserved |
| 89 | + expect(form.data.orderby).toBe(false); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should return true for explicit true value in FormData', async () => { |
| 93 | + // Schema with boolean default set to true |
| 94 | + const formSchema = z.object({ |
| 95 | + orderby: z.boolean().default(true) |
| 96 | + }); |
| 97 | + |
| 98 | + // Create FormData with orderby=true |
| 99 | + const formData = new FormData(); |
| 100 | + formData.set('orderby', 'true'); |
| 101 | + |
| 102 | + // Validate FormData with schema |
| 103 | + const form = await superValidate(formData, zod(formSchema)); |
| 104 | + |
| 105 | + // The explicit true value should be preserved |
| 106 | + expect(form.data.orderby).toBe(true); |
| 107 | + }); |
| 108 | + |
| 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() |
| 114 | + }); |
| 115 | + |
| 116 | + // Create FormData with other fields but not orderby |
| 117 | + const formData = new FormData(); |
| 118 | + formData.set('search', 'test'); |
| 119 | + |
| 120 | + // Validate FormData with schema |
| 121 | + const form = await superValidate(formData, zod(formSchema)); |
| 122 | + |
| 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'); |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments