Repository navigation
fix: list an extraErrors message once when it matches a validator error - #5443
Conversation
Size limit reportSizes are minified + brotli, measured by size-limit at A package listed twice is measured both as installed and, on the second row, as its own code with its dependencies excluded. Peer dependencies are always excluded.
|
heath-freenome
left a comment
There was a problem hiding this comment.
Thanks for picking up #5442! The direction looks right, but the duplicate check compares property strings instead of paths, so the double listing still happens for several common spellings (details inline).
Two more points that fall outside the diff:
extraErrorsstill duplicates (packages/core/src/components/Form.tsx:533).mergeErrorsmergesextraErrorswithoutpreventDuplicates. So anextraErrorsmessage identical to a validator error is still listed twice in theErrorListand on the field. For example,extraErrors={{ addr: { street: { __errors: ['must NOT have fewer than 3 characters'] } } }}with a 1-character street. The fix currently covers only widget-raised (customErrors) errors. That may be intentional, but it's worth deciding explicitly.- Existing bug in
validationDataMerge(), not from this PR: whenoldErrorSchemais empty butoldErrorsisn't, the merge branch is skipped andoldErrorsis thrown away. This happens when atransformErrorsclearsmessage, sotoErrorSchema()leaves those errors out. The comment atForm.tsx:601describes the same situation. A widget-raised error then makesmergeErrorsreturn onlytoErrorList(customErrors), dropping the validator's errors from theErrorListand fromonError. It's fine as a follow-up, but since this PR already touches the function it may be easy to fix here.
|
Thanks for the thorough review. The path-keyed dedupe (toPath, so foo/.foo, ''/., .arr[0]/.arr.0 match) and the CHANGELOG-v7.md entry are in. Point 1: mergeErrors now passes preventDuplicates for extraErrors too, with a test. Point 2: validationDataMerge now keeps oldErrors when oldErrorSchema is empty, with a test. I also merged v7 and changed one Form.behaviors expectation that counted the duplicated extraErrors message twice. |
|
@drakeo338 Can you rebase onto the latest v7 and resolve conflicts |
heath-freenome
left a comment
There was a problem hiding this comment.
Thanks, every point from the last round is addressed, and the path-keyed dedupe handles foo/.foo, ''/. and .arr[0]/.arr.0 as intended. The inline comments are mostly side effects of keeping the old errors, plus a few nits.
Two things that aren't on lines in the diff:
- Rebase: the branch still conflicts with v7 (
CHANGELOG-v7.mdandpackages/core/test/Form.errors.test.tsx), as mentioned above. - API docs:
packages/docs/docs/api-reference/utility-functions.md:2259still sayspreventDuplicatesonly passes the flag tomergeObjects(). It should also say that additionalerrorsalready in the list (same path and message) are skipped, and that existing errors are kept when theirerrorSchemais empty, matching the JSDoc you updated.
Not for this PR: keys escaped as ~0/~1 still aren't deduplicated, but the cause is upstream. The validators build property with instancePath.replace(/\//g, '.'), so a key a/b is reported as .a~1b, and that error never reaches the a/b field at all. That's a separate validator issue.
|
Merged the latest v7 into the branch with the conflicts resolved, and updated the |
There was a problem hiding this comment.
Thanks @drakeo338. All six points from the last round are addressed, and the code looks right to me. What's left is changelog wording and one small nit, in the inline comments. The branch is one commit behind v7 again (#5429), but it merges cleanly.
One thing for a separate issue, not this PR: a widget-raised error that matches the validator's error never clears after submit. With this PR's StreetWidget (minLength 3), type a, submit, then type bc. Both the field and the ErrorList still show "must NOT have fewer than 3 characters" for the valid abc. The empty raise only unsets the validator's copy, and the copy in customErrors stays. v7 behaves the same, so this PR didn't cause it, but it's the same flow as #5442 and nothing tests that the error clears. Could you open an issue for it, with these steps?
|
@drakeo338 This could use a rebase w/ conflict merge |
… error validationDataMerge() with preventDuplicates dropped the duplicate message from the ErrorSchema but still concatenated the flat errors list, so the top ErrorList showed the message twice after submit.
The errors filter compared raw property strings, so spellings of one location that differ (foo vs .foo, '' vs ., .arr[0] vs .arr.0) kept the double listing the errorSchema merge already removes. Key both sides by the toPath() split that toErrorSchema() uses, with a Set built once from the existing errors. Move the changelog entry to CHANGELOG-v7.md.
…mpty errorSchema mergeErrors() now merges extraErrors with preventDuplicates, so an extraErrors message equal to a validator error at the same path is listed once in the ErrorList and on the field. validationDataMerge() no longer drops the existing errors when their errorSchema is empty (a transformErrors that clears message).
…ata fields Export propertyToPath() from @rjsf/utils for the `property ? toPath(property) : []` split that validationDataMerge(), toErrorSchema() and Form each spelled out. validationDataMerge() defaults a missing errors or errorSchema instead of throwing, and builds its duplicate Set only when there are errors to add. Note in CHANGELOG-v7.md that keeping the existing errors changes what the validators' validateFormData() returns and Form passes to onError. Spell out the listed errors per case in Form.behaviors.test.tsx, run the extraErrors dedupe test with extraErrorsAreWarnings unset and true, and update the test that recorded the duplicate listing as current behaviour.
Add a validateFormData() test to each of ajv8, ata and cfworker where a transformErrors clears every message and customValidate adds an error: the cleared errors now come back with the custom one. Note this under the three validator headings in CHANGELOG-v7.md. validationDataMerge() merges unconditionally now that errors and errorSchema default to empty, and the API docs say what preventDuplicates skips and that existing errors are kept when their errorSchema is empty.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
6e80396 to
14cfb7e
Compare
|
Rebased onto v7 and resolved the conflicts in the changelog, validationDataMerge and the Form tests, and the extraErrors dedupe now sits in formState's mergeErrors; tests, lint and format pass. |
heath-freenome
left a comment
There was a problem hiding this comment.
Thanks @drakeo338, the rebase is clean and most of the last round is in. I've resolved the threads that are done. Still open: the propertyToPath() changelog bullet, the toErrorSchema.ts comment, using propertyToPath() in core, and point 2 of the core changelog thread (the dedupe reaching onError/onChange).
The widget-raised case is already fixed on v7. #5470 added a filter to formState.ts's mergeErrors() that lists a raised error once when the validator reports the same message at the same path. Run against v7's source, this PR's widget-raised tests already pass, and only the extraErrors tests fail. So what this PR actually changes is the extraErrors dedupe plus the path-keyed list filter in validationDataMerge(). Could you retitle the PR to match, and reword the changelog (see inline)?
The inline comments cover what the rebase onto #5469 and #5470 left behind: overlaps with what v7 now does, and two edge cases in the path matching.
…orSchema comment formState's private errorPath() and focusOnError() in formModel spelled out the split propertyToPath() makes, so they could drift from toErrorSchema() and validationDataMerge(). The comment above toErrorSchema's propertyToPath() call still named toPath and repeated the helper's JSDoc. Co-Authored-By: Claude Opus 5.5 <[email protected]>
After the rebase onto rjsf-team#5469 the validators' src is untouched, and rjsf-team#5469's 'validateFormData() with a transformErrors that clears every message' blocks already cover the cleared errors coming back. Co-Authored-By: Claude Opus 5.5 <[email protected]>
With preventDuplicates, the appended errors are now toErrorList() of the additional ErrorSchema without the messages the existing errorSchema already holds under the same keys, which are exactly the ones mergeObjects() leaves out. The list and the merged errorSchema can no longer disagree: a property named 'a.b' next to a nested a -> b, and a root error next to an invalid-schema error held under $schema, are both listed where the merge shows them. Any spelling of the existing error's property still matches, since toErrorSchema() has already split it into keys. The missing-field defaults are gone, so a validationData without errors throws as rjsf-team#5469's changelog says. Drop the test that repeated v7's 'Keeps the existing errors when their errorSchema is empty', reword the utils changelog bullet to what this changes, and add one for propertyToPath(). Co-Authored-By: Claude Opus 5.5 <[email protected]>
validationDataMerge(..., true) already leaves out of the list what it leaves out of the errorSchema, so rjsf-team#5470's keyOf/listedKeys filter over the raised errors repeated it with a second definition of the same error. Both merges now go through validationDataMerge() alone. The core changelog bullet says the extraErrors dedupe reaches the errors passed to onError and onChange too, and the tests check both. The ErrorList comment no longer compares against the old behaviour. Co-Authored-By: Claude Opus 5.5 <[email protected]>
heath-freenome
left a comment
There was a problem hiding this comment.
Thanks @drakeo338, everything from the last round is in, and I've resolved all the open threads. Matching on the errorSchema keys is a nice way to keep the list and the fields in step.
What's left is small: one cleanup in validationDataMerge(), a test that repeats an existing one, a regression test worth adding, and one more edge case (fine to accept).
heath-freenome
left a comment
There was a problem hiding this comment.
Thanks for working through all the rounds of feedback. Approving.
validationDataMerge()'s dedupe takes the package to 24.01 kB, just over the 24 kB budget. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reasons for making this change
Fixes #5442.
On v7, #5470 already lists a widget-raised error once when the validator reports the same message at the same path. What was still duplicated is an
extraErrorsmessage equal to a validator error:FormmergedextraErrorswithoutpreventDuplicates, so it showed twice on the field, in theErrorList, and in theerrorspassed toonErrorandonChange.This PR:
extraErrorswithpreventDuplicatesinformState'smergeErrors();validationDataMerge()withpreventDuplicatesleave out of the appendederrorsexactly whatmergeObjects()leaves out of the mergederrorSchema(a message the existingerrorSchemaalready holds under the same keys), so the list and the fields cannot disagree.mergeErrors()now relies on that for both merges instead of keeping its own filter;propertyToPath()to@rjsf/utilsand uses it intoErrorSchema(),formStateandformModel.Checklist
pnpm exec nx run-many --target=build --exclude=@rjsf/docs && pnpm run test:updateto update snapshots, if needed.Tested with the
@rjsf/utils,@rjsf/coreand three validator test suites,pnpm run cs-checkandpnpm run lint. The snapshot command was not run because the change touches no snapshots.