Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: isNameInFieldArray with names option #713

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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+`));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n should contain number in field array.