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
65 changes: 65 additions & 0 deletions src/__tests__/toNestErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,71 @@ test('does not throw SyntaxError for field names containing regex metacharacters
).not.toThrow();
});

test('preserves nested field array errors when the error path uses bracket notation (#875)', () => {
const result = toNestErrors(
{
'scenarios[0].rows': { type: 'root-error', message: 'rows required' },
'scenarios[0].rows[0].values': {
type: 'required',
message: 'value required',
},
},
{
// Mirrors react-hook-form's real `_fields`, which is a genuine nested
// tree (unlike the flat dot-keyed mocks used elsewhere in this file),
// so that `get()` resolves bracket-notation paths the same way it
// would in production.
fields: {
scenarios: {
0: {
rows: Object.assign(
{
name: 'scenarios.0.rows',
ref: { name: 'scenarios.0.rows' },
},
{
0: {
values: {
name: 'scenarios.0.rows.0.values',
ref: { name: 'scenarios.0.rows.0.values' },
},
},
},
),
},
},
} as any as Record<InternalFieldName, Field['_f']>,
names: [
'scenarios.0.rows',
'scenarios.0.rows.0.values',
'scenarios.0.rows.1.values',
],
shouldUseNativeValidation: false,
},
);

expect(result).toEqual({
scenarios: [
{
rows: {
'0': {
values: {
type: 'required',
message: 'value required',
ref: { name: 'scenarios.0.rows.0.values' },
},
},
root: {
type: 'root-error',
message: 'rows required',
ref: { name: 'scenarios.0.rows' },
},
},
},
],
});
});

test('should correctly validate object with special characters', () => {
const result = toNestErrors(
{ '[array-2]': { type: 'string', message: 'string is required' } },
Expand Down
2 changes: 1 addition & 1 deletion src/toNestErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const isNameInFieldArray = (

// Removes brackets to match react-hook-form's `set` method behavior.
function escapeBrackets(input: string): string {
return input.replace(/[\[\]]/g, '');
return input.replace(/\[(\d+)]/g, '.$1').replace(/[[\]]/g, '');
}

// Removes brackets then escapes regex metacharacters so a field name can be
Expand Down
Loading