Fix FieldArray validation masking field errors#199
Fix FieldArray validation masking field errors#199erikras-richard-agent wants to merge 1 commit into
Conversation
📝 WalkthroughWalkthroughThe PR fixes a regression where array-level validation returning undefined entries incorrectly blocked field-level validation errors. It introduces error normalization helpers to properly detect and suppress non-existent array errors during validation. ChangesArray Error Normalization
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/useFieldArray.ts (1)
60-91: 🧹 Nitpick | 🔵 Trivial | 💤 Low valueOptional: deduplicate the sync/async branches.
The
!error || Array.isArray(error)→normalizeArrayError/ else wrap-with-ARRAY_ERRORlogic is repeated identically for the sync and Promise paths. Extracting it into a small helper keeps the two branches in lockstep and makes the validator easier to follow.♻️ Proposed refactor
+const wrapValidatorResult = (error: any): any => { + if (!error || Array.isArray(error)) { + return normalizeArrayError(error) + } + const arrayError: any[] = [] + // gross, but we have to set a string key on the array + ;(arrayError as any)[ARRAY_ERROR] = error + return arrayError +} + const useFieldArray = ( ... const validate: FieldValidator | undefined = useConstant(() => !validateProp ? undefined : (value: any, allValues: any, meta: any) => { const rawError = validateProp(value, allValues, meta) - - // If the validator returned a Promise, await it before processing if (rawError && typeof rawError.then === 'function') { - return rawError.then((error: any) => { - if (!error || Array.isArray(error)) { - return normalizeArrayError(error) - } else { - const arrayError: any[] = [] - // gross, but we have to set a string key on the array - ; (arrayError as any)[ARRAY_ERROR] = error - return arrayError - } - }) + return rawError.then(wrapValidatorResult) } - - // Synchronous validator - process immediately - const error = rawError - if (!error || Array.isArray(error)) { - return normalizeArrayError(error) - } else { - const arrayError: any[] = [] - // gross, but we have to set a string key on the array - ; (arrayError as any)[ARRAY_ERROR] = error - return arrayError - } + return wrapValidatorResult(rawError) } )🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/useFieldArray.ts` around lines 60 - 91, The validator repeats the same post-processing logic for sync and async results; inside the useConstant callback for validate (which wraps validateProp), extract that repeated block into a small helper (e.g., processError) that takes the raw error, returns normalizeArrayError(error) when !error || Array.isArray(error), otherwise builds an array, sets (array as any)[ARRAY_ERROR]=error and returns it; then replace the duplicated code by returning rawError.then(processError) for the Promise branch and returning processError(rawError) for the synchronous branch so both paths share the same logic.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@src/useFieldArray.ts`:
- Around line 60-91: The validator repeats the same post-processing logic for
sync and async results; inside the useConstant callback for validate (which
wraps validateProp), extract that repeated block into a small helper (e.g.,
processError) that takes the raw error, returns normalizeArrayError(error) when
!error || Array.isArray(error), otherwise builds an array, sets (array as
any)[ARRAY_ERROR]=error and returns it; then replace the duplicated code by
returning rawError.then(processError) for the Promise branch and returning
processError(rawError) for the synchronous branch so both paths share the same
logic.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 4017dcad-ee6b-4bdf-adb9-92e730dde690
📒 Files selected for processing (2)
src/FieldArray.test.tsxsrc/useFieldArray.ts
Summary
Fixes #163
Test
Summary by CodeRabbit