Skip to content

Commit 985c48d

Browse files
authored
fix: add support for names option (#713)
1 parent 077486a commit 985c48d

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/__tests__/toNestErrors.ts

+29
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,35 @@ test('transforms flat object to nested object and shouldUseNativeValidation: tru
3939
).toHaveBeenCalledWith(flatObject.name.message);
4040
});
4141

42+
test('transforms flat object to nested object with names option', () => {
43+
const result = toNestErrors(
44+
{
45+
username: {
46+
type: 'custom',
47+
message: 'error',
48+
},
49+
},
50+
{
51+
names: ['username', 'username.first'],
52+
fields: {
53+
username: {
54+
name: 'username',
55+
ref: { name: 'username' },
56+
},
57+
},
58+
shouldUseNativeValidation: false,
59+
},
60+
);
61+
62+
expect(result).toEqual({
63+
username: {
64+
type: 'custom',
65+
message: 'error',
66+
ref: { name: 'username' },
67+
},
68+
});
69+
});
70+
4271
test('transforms flat object to nested object with root error for field array', () => {
4372
const result = toNestErrors(
4473
{

src/toNestErrors.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ export const toNestErrors = <TFieldValues extends FieldValues>(
3838
const isNameInFieldArray = (
3939
names: InternalFieldName[],
4040
name: InternalFieldName,
41-
) => names.some((n) => n.startsWith(name + '.'));
41+
) => names.some((n) => n.match(`^${name}\\.\\d+`));

0 commit comments

Comments
 (0)