|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { superValidate } from '$lib/superValidate.js'; |
| 3 | +import { zod } from '$lib/adapters/zod4.js'; |
| 4 | +import { z } from 'zod/v4'; |
| 5 | + |
| 6 | +describe('Issue 650 - Zod 4 Default Date values in nested objects', () => { |
| 7 | + it('should handle default dates in nested objects correctly', async () => { |
| 8 | + const schema = z.object({ |
| 9 | + name: z.string().min(2), |
| 10 | + email: z.string().email(), |
| 11 | + dateRange: z |
| 12 | + .object({ |
| 13 | + start: z.date(), |
| 14 | + end: z.date() |
| 15 | + }) |
| 16 | + .default({ |
| 17 | + start: new Date('2025-10-29T10:47:03.068Z'), |
| 18 | + end: new Date('2025-10-29T10:47:03.068Z') |
| 19 | + }), |
| 20 | + dateRangeIndividual: z.object({ |
| 21 | + start: z.date().default(new Date('2025-10-29T10:52:25.537Z')), |
| 22 | + end: z.date().default(new Date('2025-10-29T10:52:25.537Z')) |
| 23 | + }), |
| 24 | + date: z.date().default(new Date('2025-10-29T10:47:03.068Z')) |
| 25 | + }); |
| 26 | + |
| 27 | + const form = await superValidate(zod(schema)); |
| 28 | + const data = form.data as any; // eslint-disable-line @typescript-eslint/no-explicit-any |
| 29 | + |
| 30 | + // All dates should be Date objects, not strings |
| 31 | + expect(data.dateRange.start).toBeInstanceOf(Date); |
| 32 | + expect(data.dateRange.end).toBeInstanceOf(Date); |
| 33 | + expect(data.dateRangeIndividual.start).toBeInstanceOf(Date); |
| 34 | + expect(data.dateRangeIndividual.end).toBeInstanceOf(Date); |
| 35 | + expect(data.date).toBeInstanceOf(Date); |
| 36 | + |
| 37 | + // Verify the actual date values match what was set |
| 38 | + expect(data.dateRange.start.toISOString()).toBe('2025-10-29T10:47:03.068Z'); |
| 39 | + expect(data.dateRange.end.toISOString()).toBe('2025-10-29T10:47:03.068Z'); |
| 40 | + expect(data.dateRangeIndividual.start.toISOString()).toBe('2025-10-29T10:52:25.537Z'); |
| 41 | + expect(data.dateRangeIndividual.end.toISOString()).toBe('2025-10-29T10:52:25.537Z'); |
| 42 | + expect(data.date.toISOString()).toBe('2025-10-29T10:47:03.068Z'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should handle nested objects with default dates at multiple levels', async () => { |
| 46 | + const schema = z.object({ |
| 47 | + level1: z |
| 48 | + .object({ |
| 49 | + level2: z |
| 50 | + .object({ |
| 51 | + date: z.date() |
| 52 | + }) |
| 53 | + .default({ |
| 54 | + date: new Date('2025-01-01T00:00:00.000Z') |
| 55 | + }) |
| 56 | + }) |
| 57 | + .default({ |
| 58 | + level2: { |
| 59 | + date: new Date('2025-01-01T00:00:00.000Z') |
| 60 | + } |
| 61 | + }) |
| 62 | + }); |
| 63 | + |
| 64 | + const form = await superValidate(zod(schema)); |
| 65 | + const data = form.data as any; // eslint-disable-line @typescript-eslint/no-explicit-any |
| 66 | + |
| 67 | + expect(data.level1.level2.date).toBeInstanceOf(Date); |
| 68 | + expect(data.level1.level2.date.toISOString()).toBe('2025-01-01T00:00:00.000Z'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should handle mixed default values in nested objects', async () => { |
| 72 | + const schema = z.object({ |
| 73 | + settings: z |
| 74 | + .object({ |
| 75 | + theme: z.string(), |
| 76 | + createdAt: z.date(), |
| 77 | + count: z.number() |
| 78 | + }) |
| 79 | + .default({ |
| 80 | + theme: 'dark', |
| 81 | + createdAt: new Date('2025-10-29T00:00:00.000Z'), |
| 82 | + count: 42 |
| 83 | + }) |
| 84 | + }); |
| 85 | + |
| 86 | + const form = await superValidate(zod(schema)); |
| 87 | + const data = form.data as any; // eslint-disable-line @typescript-eslint/no-explicit-any |
| 88 | + |
| 89 | + expect(data.settings.theme).toBe('dark'); |
| 90 | + expect(data.settings.createdAt).toBeInstanceOf(Date); |
| 91 | + expect(data.settings.createdAt.toISOString()).toBe('2025-10-29T00:00:00.000Z'); |
| 92 | + expect(data.settings.count).toBe(42); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should preserve individual field defaults when object has no default', async () => { |
| 96 | + const schema = z.object({ |
| 97 | + timestamps: z.object({ |
| 98 | + created: z.date().default(new Date('2025-01-01T00:00:00.000Z')), |
| 99 | + updated: z.date().default(new Date('2025-01-02T00:00:00.000Z')) |
| 100 | + }) |
| 101 | + }); |
| 102 | + |
| 103 | + const form = await superValidate(zod(schema)); |
| 104 | + const data = form.data as any; // eslint-disable-line @typescript-eslint/no-explicit-any |
| 105 | + |
| 106 | + expect(data.timestamps.created).toBeInstanceOf(Date); |
| 107 | + expect(data.timestamps.updated).toBeInstanceOf(Date); |
| 108 | + expect(data.timestamps.created.toISOString()).toBe('2025-01-01T00:00:00.000Z'); |
| 109 | + expect(data.timestamps.updated.toISOString()).toBe('2025-01-02T00:00:00.000Z'); |
| 110 | + }); |
| 111 | +}); |
0 commit comments