Skip to content

Commit 1671c35

Browse files
committed
Type inference for validation errors didn't include _errors for objects, only for arrays.
1 parent a5ed133 commit 1671c35

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Headlines: Added, Changed, Deprecated, Removed, Fixed, Security
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Fixed
11+
12+
- Type inference for validation errors didn't include `_errors` for objects, only for arrays.
13+
814
## [2.15.2] - 2024-06-26
915

1016
### Changed

src/lib/superStruct.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type SuperStructArray<T extends Record<string, unknown>, Data, ArrayData
44
// eslint-disable-next-line @typescript-eslint/no-explicit-any
55
[Property in AllKeys<T>]?: [T] extends [any]
66
? NonNullable<T[Property]> extends Record<string, unknown>
7-
? SuperStructArray<MergeUnion<NonNullable<T[Property]>>, Data, ArrayData>
7+
? ArrayData & SuperStructArray<MergeUnion<NonNullable<T[Property]>>, Data, ArrayData>
88
: NonNullable<T[Property]> extends (infer A)[]
99
? ArrayData &
1010
Record<

src/tests/errors.test.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,29 @@ describe('Mapping defaults to invalid data', () => {
113113

114114
describe('The ValidationErrors type', () => {
115115
it('should work as expected', () => {
116-
const data = { name: '' };
117-
const name = 'name' as keyof typeof data;
116+
const schema = z.object({
117+
name: z.string().min(1),
118+
birthDate: z
119+
.object({
120+
year: z.number(),
121+
month: z.number(),
122+
day: z.number()
123+
})
124+
.refine(() => {
125+
// Custom validation logic for the date
126+
return false; // Assuming the validation fails
127+
}, 'Invalid Date')
128+
});
129+
130+
const data: z.infer<typeof schema> = { name: '', birthDate: { year: 0, month: 0, day: 0 } };
118131
const errors: ValidationErrors<typeof data> = {};
119-
const test: string[] = errors[name] ?? [];
120-
expect(test).toEqual([]);
132+
133+
const birthErrors: string[] | undefined = errors.birthDate?._errors;
134+
const name: string[] = errors['name'] ?? [];
135+
const year: string[] | undefined = errors.birthDate?.year;
136+
137+
expect(name).toEqual([]);
138+
expect(year).toBeUndefined();
139+
expect(birthErrors).toBeUndefined();
121140
});
122141
});

0 commit comments

Comments
 (0)