Skip to content

Commit 5e3ec3c

Browse files
committed
Zod 4 adapter now respects global customError configuration when no explicit error map is provided.
1 parent e07206c commit 5e3ec3c

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
- Zod 4 adapter: Allow top-level `.transform()` and `.refine()` in schemas. [#646](https://github.com/ciscoheat/sveltekit-superforms/issues/646).
13+
- Zod 4 adapter: Now respects global `customError` configuration when no explicit error map is provided. The adapter prioritizes `customError` over `localeError`. [#618](https://github.com/ciscoheat/sveltekit-superforms/issues/618).
1314

1415
## [2.28.0] - 2025-10-19
1516

src/lib/adapters/zod4.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ async function validate<T extends ZodValidationSchema>(
130130
error: $ZodErrorMap | undefined
131131
): Promise<ValidationResult<Infer<T, 'zod4'>>> {
132132
// Use Zod's global config error map if none provided to preserve custom messages.
133-
if (error === undefined) error = config().localeError;
133+
// Prioritize customError over localeError when both are set.
134+
if (error === undefined) {
135+
const zConfig = config();
136+
error = zConfig.customError ?? zConfig.localeError;
137+
}
134138
const result = await safeParseAsync(schema, data, { error });
135139
if (result.success) {
136140
return {

src/tests/issue-639.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,25 @@ describe('Issue #639 - Zod v4 generic error messages in dev mode', () => {
7979
expect(error.toLowerCase()).not.toBe('invalid input');
8080
}
8181
});
82+
83+
test('Global custom error respected', async () => {
84+
z.config({
85+
customError: () => 'Global error'
86+
});
87+
// Schema with explicit custom error messages
88+
const customSchema = z.object({
89+
username: z.string().min(3)
90+
});
91+
92+
const formData = new FormData();
93+
formData.set('username', 'ab'); // Too short
94+
95+
const form = await superValidate(formData, zod(customSchema));
96+
expect(form.valid).toBe(false);
97+
98+
const usernameErrors = form.errors.username ?? [];
99+
100+
// Verify custom messages are present
101+
expect(usernameErrors).toContain('Global error');
102+
});
82103
});

0 commit comments

Comments
 (0)