Skip to content

Commit 46e6cee

Browse files
committed
Added tests for issue 623
1 parent 7ae9b1d commit 46e6cee

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed

src/tests/issue-623.test.ts

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { z as z4 } from 'zod';
3+
import { zod } from '$lib/adapters/zod4.js';
4+
import { superValidate } from '$lib/superValidate.js';
5+
6+
describe('Issue 623 - Array and date properties modify each other', () => {
7+
it('should preserve date values when array validation fails (Zod v4)', async () => {
8+
const data = {
9+
date: new Date('2023-10-01T00:00:00Z'),
10+
items: [
11+
{ id: 1, product: 'Product A' },
12+
{ id: 2, product: 'Product B' },
13+
{ id: 'bad' as any, product: 'Product C' } // This will fail validation
14+
]
15+
};
16+
17+
const schema = z4.object({
18+
date: z4.date(),
19+
items: z4.array(
20+
z4.object({
21+
id: z4.number(),
22+
product: z4.string()
23+
})
24+
)
25+
});
26+
27+
const form = await superValidate(data, zod(schema), {
28+
errors: false
29+
});
30+
31+
// Form should be invalid due to bad array item
32+
expect(form.valid).toBe(false);
33+
34+
// Date should still be preserved, not become empty string
35+
expect(form.data.date).toBeInstanceOf(Date);
36+
expect(form.data.date).toEqual(new Date('2023-10-01T00:00:00Z'));
37+
expect(form.data.date).not.toBe('');
38+
});
39+
40+
it('should preserve date values when array validation fails (nested dates)', async () => {
41+
const data = {
42+
dates: {
43+
startDate: new Date('2023-10-01T00:00:00Z'),
44+
endDate: new Date('2023-12-31T00:00:00Z')
45+
},
46+
items: [
47+
{ id: 1, name: 'Item 1' },
48+
{ id: 'invalid' as any, name: 'Item 2' } // This will fail validation
49+
]
50+
};
51+
52+
const schema = z4.object({
53+
dates: z4.object({
54+
startDate: z4.date(),
55+
endDate: z4.date()
56+
}),
57+
items: z4.array(
58+
z4.object({
59+
id: z4.number(),
60+
name: z4.string()
61+
})
62+
)
63+
});
64+
65+
const form = await superValidate(data, zod(schema), {
66+
errors: false
67+
});
68+
69+
// Form should be invalid due to bad array item
70+
expect(form.valid).toBe(false);
71+
72+
// Dates should still be preserved
73+
expect(form.data.dates.startDate).toBeInstanceOf(Date);
74+
expect(form.data.dates.endDate).toBeInstanceOf(Date);
75+
expect(form.data.dates.startDate).toEqual(new Date('2023-10-01T00:00:00Z'));
76+
expect(form.data.dates.endDate).toEqual(new Date('2023-12-31T00:00:00Z'));
77+
});
78+
79+
it('should handle valid data with both dates and arrays correctly', async () => {
80+
const data = {
81+
date: new Date('2023-10-01T00:00:00Z'),
82+
items: [
83+
{ id: 1, product: 'Product A' },
84+
{ id: 2, product: 'Product B' }
85+
]
86+
};
87+
88+
const schema = z4.object({
89+
date: z4.date(),
90+
items: z4.array(
91+
z4.object({
92+
id: z4.number(),
93+
product: z4.string()
94+
})
95+
)
96+
});
97+
98+
const form = await superValidate(data, zod(schema), {
99+
errors: false
100+
});
101+
102+
// Form should be valid
103+
expect(form.valid).toBe(true);
104+
105+
// Date should be preserved
106+
expect(form.data.date).toBeInstanceOf(Date);
107+
expect(form.data.date).toEqual(new Date('2023-10-01T00:00:00Z'));
108+
109+
// Array should be preserved
110+
expect(form.data.items).toEqual([
111+
{ id: 1, product: 'Product A' },
112+
{ id: 2, product: 'Product B' }
113+
]);
114+
});
115+
116+
it('should preserve multiple dates when array validation fails', async () => {
117+
const data = {
118+
createdAt: new Date('2023-01-01T00:00:00Z'),
119+
updatedAt: new Date('2023-10-01T00:00:00Z'),
120+
items: [{ id: 'bad' as any }]
121+
};
122+
123+
const schema = z4.object({
124+
createdAt: z4.date(),
125+
updatedAt: z4.date(),
126+
items: z4.array(
127+
z4.object({
128+
id: z4.number()
129+
})
130+
)
131+
});
132+
133+
const form = await superValidate(data, zod(schema), {
134+
errors: false
135+
});
136+
137+
expect(form.valid).toBe(false);
138+
expect(form.data.createdAt).toBeInstanceOf(Date);
139+
expect(form.data.updatedAt).toBeInstanceOf(Date);
140+
expect(form.data.createdAt).toEqual(new Date('2023-01-01T00:00:00Z'));
141+
expect(form.data.updatedAt).toEqual(new Date('2023-10-01T00:00:00Z'));
142+
});
143+
144+
it('should preserve date values when array validation fails (without errors option)', async () => {
145+
const data = {
146+
date: new Date('2023-10-01T00:00:00Z'),
147+
items: [
148+
{ id: 1, product: 'Product A' },
149+
{ id: 2, product: 'Product B' },
150+
{ id: 'bad' as any, product: 'Product C' } // This will fail validation
151+
]
152+
};
153+
154+
const schema = z4.object({
155+
date: z4.date(),
156+
items: z4.array(
157+
z4.object({
158+
id: z4.number(),
159+
product: z4.string()
160+
})
161+
)
162+
});
163+
164+
// Note: NO errors: false option here!
165+
const form = await superValidate(data, zod(schema));
166+
167+
// Form should be invalid due to bad array item
168+
expect(form.valid).toBe(false);
169+
170+
// Date should still be preserved, not become empty string
171+
expect(form.data.date).toBeInstanceOf(Date);
172+
expect(form.data.date).toEqual(new Date('2023-10-01T00:00:00Z'));
173+
expect(form.data.date).not.toBe('');
174+
});
175+
176+
it('should preserve date in the exact scenario from issue #623', async () => {
177+
// This is the EXACT test case from the GitHub issue
178+
const data = {
179+
date: new Date('2023-10-01T00:00:00Z'),
180+
items: [
181+
{ id: 1, product: 'Product A' },
182+
{ id: 2, product: 'Product B' },
183+
{ id: 'bad' as any, product: '' }
184+
]
185+
};
186+
187+
const schema = z4.object({
188+
date: z4.date(),
189+
items: z4.array(
190+
z4.object({
191+
id: z4.number(),
192+
product: z4.string()
193+
})
194+
)
195+
});
196+
197+
const form = await superValidate(data, zod(schema), {
198+
errors: false
199+
});
200+
201+
// Form should be invalid due to bad array item
202+
expect(form.valid).toBe(false);
203+
// The issue reports that date becomes empty string ""
204+
expect(form.data.date).not.toBe('');
205+
expect(form.data.date).toBeInstanceOf(Date);
206+
expect(form.data.date).toEqual(new Date('2023-10-01T00:00:00Z'));
207+
});
208+
});

0 commit comments

Comments
 (0)