Skip to content

Commit 853a375

Browse files
committed
setError fix for form-level errors.
1 parent 7e77aa3 commit 853a375

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

CHANGELOG.md

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

1010
### Fixed
1111

12+
- Form-level errors couldn't be overwritten.
1213
- Array-level errors couldn't be added with `setError`.
1314
- Native string enums weren't working when posting the actual string value.
1415

src/errors.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ describe('Errors', async () => {
3636
setError(output, 'enumber', 'This should be ok');
3737
setError(output, 'enumber', 'Still ok');
3838
setError(output, 'arr._errors', 'Array-level error');
39-
setError(output, '', 'Form-level error');
39+
setError(output, '', 'Form-level error that should not be displayed.');
40+
setError(output, '', 'Form-level error', { overwrite: true });
4041

4142
assert(!output.valid);
4243
expect(output.errors).toStrictEqual(err);

src/lib/superValidate.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ export function setError<T extends ZodValidation<AnyZodObject>>(
7474

7575
if (path === null || path === '') {
7676
if (!form.errors._errors) form.errors._errors = [];
77-
form.errors._errors = form.errors._errors.concat(errArr);
77+
form.errors._errors = options.overwrite
78+
? errArr
79+
: form.errors._errors.concat(errArr);
7880
} else {
7981
const realPath = splitPath(path);
8082

0 commit comments

Comments
 (0)