Skip to content

Commit fd1b2a7

Browse files
authored
fix(computed-types): nested schema (#425)
1 parent 52a6e07 commit fd1b2a7

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

computed-types/src/__tests__/Form.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { computedTypesResolver } from '..';
88
const schema = Schema({
99
username: string.min(2).error('username field is required'),
1010
password: string.min(2).error('password field is required'),
11+
address: Schema({
12+
zipCode: string.min(5).max(5).error('zipCode field is required'),
13+
}),
1114
});
1215

1316
type FormData = Type<typeof schema> & { unusedProperty: string };
@@ -33,6 +36,11 @@ function TestComponent({ onSubmit }: Props) {
3336
<input {...register('password')} />
3437
{errors.password && <span role="alert">{errors.password.message}</span>}
3538

39+
<input {...register('address.zipCode')} />
40+
{errors.address?.zipCode && (
41+
<span role="alert">{errors.address.zipCode.message}</span>
42+
)}
43+
3644
<button type="submit">submit</button>
3745
</form>
3846
);
@@ -50,5 +58,6 @@ test("form's validation with computed-types and TypeScript's integration", async
5058

5159
expect(screen.getByText(/username field is required/i)).toBeInTheDocument();
5260
expect(screen.getByText(/password field is required/i)).toBeInTheDocument();
61+
expect(screen.getByText(/zipCode field is required/i)).toBeInTheDocument();
5362
expect(handleSubmit).not.toHaveBeenCalled();
5463
});

computed-types/src/__tests__/__fixtures__/data.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ export const schema = Schema({
2626
name: string.min(4).max(4),
2727
})
2828
.optional(),
29+
address: Schema({
30+
city: string.min(3, 'Is required'),
31+
zipCode: string
32+
.min(5, 'Must be 5 characters long')
33+
.max(5, 'Must be 5 characters long'),
34+
}),
2935
});
3036

3137
export const validData: Type<typeof schema> = {
@@ -43,13 +49,21 @@ export const validData: Type<typeof schema> = {
4349
name: 'name',
4450
},
4551
],
52+
address: {
53+
city: 'Awesome city',
54+
zipCode: '12345',
55+
},
4656
};
4757

4858
export const invalidData = {
4959
password: '___',
5060
email: '',
5161
birthYear: 'birthYear',
5262
like: [{ id: 'z' }],
63+
address: {
64+
city: '',
65+
zipCode: '123',
66+
},
5367
};
5468

5569
export const fields: Record<InternalFieldName, Field['_f']> = {

computed-types/src/__tests__/__snapshots__/computed-types.ts.snap

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
exports[`computedTypesResolver should return a single error from computedTypesResolver when validation fails 1`] = `
44
Object {
55
"errors": Object {
6+
"address": Object {
7+
"city": Object {
8+
"message": "Is required",
9+
"ref": undefined,
10+
"type": "ValidationError",
11+
},
12+
"zipCode": Object {
13+
"message": "Must be 5 characters long",
14+
"ref": undefined,
15+
"type": "ValidationError",
16+
},
17+
},
618
"birthYear": Object {
719
"message": "Expect value to be \\"number\\"",
820
"ref": undefined,
@@ -21,9 +33,16 @@ Object {
2133
"type": "ValidationError",
2234
},
2335
"like": Object {
24-
"message": "Expect value to be \\"string\\"",
25-
"ref": undefined,
26-
"type": "ValidationError",
36+
"id": Object {
37+
"message": "Expect value to be \\"number\\"",
38+
"ref": undefined,
39+
"type": "ValidationError",
40+
},
41+
"name": Object {
42+
"message": "Expect value to be \\"string\\"",
43+
"ref": undefined,
44+
"type": "ValidationError",
45+
},
2746
},
2847
"password": Object {
2948
"message": "One uppercase character",

computed-types/src/computed-types.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,14 @@ import { toNestError, validateFieldsNatively } from '@hookform/resolvers';
33
import type { ValidationError } from 'computed-types';
44
import type { Resolver } from './types';
55

6-
const parseErrorSchema = (
7-
computedTypesError: ValidationError,
8-
parsedErrors: FieldErrors = {},
9-
path = '',
10-
) => {
6+
const parseErrorSchema = (computedTypesError: ValidationError) => {
7+
const parsedErrors: FieldErrors = {};
118
return (computedTypesError.errors || []).reduce((acc, error) => {
12-
const _currentPath = String(error.path[0]);
13-
const _path = path ? `${path}.${_currentPath}` : _currentPath;
14-
15-
acc[_path] = {
9+
acc[error.path.join('.')] = {
1610
type: error.error.name,
1711
message: error.error.message,
1812
};
1913

20-
parseErrorSchema(error.error, acc, _path);
21-
2214
return acc;
2315
}, parsedErrors);
2416
};

0 commit comments

Comments
 (0)