Skip to content

Commit

Permalink
fix: add support for names option
Browse files Browse the repository at this point in the history
  • Loading branch information
YuyaYoshioka committed Sep 1, 2024
1 parent 5fc1e63 commit 1a3dc4b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/__tests__/toNestErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,35 @@ test('transforms flat object to nested object and shouldUseNativeValidation: tru
).toHaveBeenCalledWith(flatObject.name.message);
});

test('transforms flat object to nested object with names option', () => {
const result = toNestErrors(
{
username: {
type: 'custom',
message: 'error',
},
},
{
names: ['username', 'username.first'],
fields: {
username: {
name: 'username',
ref: { name: 'username' },
},
},
shouldUseNativeValidation: false,
},
);

expect(result).toEqual({
username: {
type: 'custom',
message: 'error',
ref: { name: 'username' },
},
});
});

test('transforms flat object to nested object with root error for field array', () => {
const result = toNestErrors(
{
Expand Down
2 changes: 1 addition & 1 deletion src/toNestErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ export const toNestErrors = <TFieldValues extends FieldValues>(
const isNameInFieldArray = (
names: InternalFieldName[],
name: InternalFieldName,
) => names.some((n) => n.startsWith(name + '.'));
) => names.some((n) => n.match(`^${name}\\.\\d+`));

0 comments on commit 1a3dc4b

Please sign in to comment.