Skip to content
Merged
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
Contributor 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.

Copy link

Choose a reason for hiding this comment

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

@YuyaYoshioka this actual change broke validation for nested fields like below:

{
    user: { // user should have name or email
         name: '',
         email: '',
         passport: '' // passport should not be empty
    }
}

Yup validation for example supports this behavior, and @hookform/resolvers also supported this behavior until version 4.0.0.

Can we bring the previous solution back?