fix: updates field validation error messages to use labels if applicable (#10601)

### What?

Previously, field error messages displayed in toast notifications used
the field path to reference fields that failed validation. This
path-based approach was necessary to distinguish between fields that
might share the same name when nested inside arrays, groups, rows, or
collapsible fields.

However, the human readability of these paths was lacking, especially
for unnamed fields like rows and collapsible fields. For example:

- A text field inside a row could display as: `_index-0.text`
- A text field nested within multiple arrays could display as:
`items.0.subArray.0.text`

These outputs are technically correct but not user-friendly.

### Why?

While the previous format was helpful for pinpointing the specific field
that caused the validation error, it could be more user-friendly and
clearer to read. The goal is to maintain the same level of accuracy
while improving the readability for both developers and content editors.

### How?

To improve readability, the following changes were made:

1. Use Field Labels Instead of Field Paths:
- The ValidationError component now uses the label prop from the field
config (if available) instead of the field’s name.
       - If a label is provided, it will be used in the error message.
       - If no label exists, it will fall back to the field’s name.

2. Remove _index from Paths for Unnamed Fields (In the validationError
component only):
- For unnamed fields like rows and collapsibles, the _index prefix is
now stripped from the output to make it cleaner.
       - Instead of `_index-0.text`, it now outputs just `Text`.

3. Reformat the Error Path for Readability:
- The error message format has been improved to be more human-readable,
showing the field hierarchy in a structured way with array indices
converted to 1-based numbers.

#### Example transformation:

##### Before:
The following fields are invalid: `items.0.subArray.0.text`

##### After:
The following fields are invalid: `Items 1 > SubArray 1 > Text`
This commit is contained in:
Patrik
2025-01-17 09:42:46 -05:00
committed by GitHub
parent 38a06e7bd3
commit ad553e967b
11 changed files with 180 additions and 16 deletions

View File

@@ -1159,7 +1159,7 @@ describe('collections-graphql', () => {
})
.then((res) => res.json())
expect(Array.isArray(errors)).toBe(true)
expect(errors[0].message).toEqual('The following field is invalid: min')
expect(errors[0].message).toEqual('The following field is invalid: Min')
expect(typeof errors[0].locations).toBeDefined()
})
@@ -1207,7 +1207,7 @@ describe('collections-graphql', () => {
expect(errors[1].extensions.data.errors[0].path).toEqual('email')
expect(Array.isArray(errors[2].locations)).toEqual(true)
expect(errors[2].message).toEqual('The following field is invalid: email')
expect(errors[2].message).toEqual('The following field is invalid: Email')
expect(errors[2].path[0]).toEqual('test4')
expect(errors[2].extensions.name).toEqual('ValidationError')
expect(errors[2].extensions.data.errors[0].message).toEqual(