Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@moh3n9595
Copy link
Contributor

This PR enhances antd-zod by introducing a customizable error aggregation mechanism. Instead of only returning a single error per field, developers now have control over how multiple error messages are combined using a new option: aggregateErrorMessages.

Changes

  • New Option: aggregateErrorMessages

    • Accepts a function with the signature (prev: string, next: string) => string that defines how to combine error messages for the same field.
    • This allows users to join multiple errors (e.g., with a comma) or simply return the first error.
  • Refactored Modules

    • createSchemaFieldRule.ts
      • Updated to accept the aggregateErrorMessages option.
      • Passes the option to validateFields during rule creation.
    • validateFields.ts
      • Updated to forward the aggregateErrorMessages option to formatErrors.
    • formatErrors.ts
      • Refactored the error formatting logic to aggregate multiple errors using the provided aggregateErrorMessages function.
      • Ensures that when multiple errors exist for the same field, they are combined according to the user’s strategy.
  • Test Updates

    • Modified existing tests for createSchemaFieldRule, validateFields, and formatErrors to supply the new option.
    • Added new tests in formatErrors.test.ts to verify that different aggregation strategies work as expected:
      • One test returns only the first error.
      • Another joins errors with a comma.

Motivation

This change addresses a common need where developers want more control over error message presentation in forms. Previously, antd-zod only returned one error per field, which limited flexibility. With the new aggregation option, developers can tailor the error display—whether that means showing all errors concatenated or just the primary error—thereby improving the user experience in form validations.

Impact

  • Backward Compatible: Developers can maintain existing behavior by passing an aggregation function that returns the first error (e.g., (prev, next) => prev).
  • Enhanced Flexibility: Users now have full control over error message aggregation without altering core validation functionality.

Looking forward to your feedback!

@MrBr
Copy link
Owner

MrBr commented Mar 15, 2025

@moh3n9595 thanks for opening the PR.

Can you add a Storybook example (can be a new file) for this particular case that you're solving. I'd like to play around with a practical use case.

This PR is very detailed, documented and clean. Admirable, but still, I'd suggest reaching out before making changes just for the purpose of aligning the vision before starting to code. This is primarily to save your own time. Measure twice, cut once.

@moh3n9595
Copy link
Contributor Author

@MrBr, I added the storybook. Could you recheck this, please?

@MrBr
Copy link
Owner

MrBr commented Mar 16, 2025

It looks like antd form supports multiple errors out of the box. It can be achieved by using array of errors instead of string.

In formatErrors function change the core logic to be like this:

        if (formattedErrors[path]) {
          formattedErrors[path].push(issue.message);
        } else {
          formattedErrors[path] = [issue.message];
        }

This is the result in the UI
image

Please update the necessary types, tests and the rename aggregated story file to multiple errors file.

@MrBr
Copy link
Owner

MrBr commented Mar 22, 2025

@moh3n9595 please let me know if you're interested in implementing this change. Your contribution would be great!

@moh3n9595
Copy link
Contributor Author

Hi, I've opened a new PR #19. The new PR refactors error handling so that multiple errors are returned as an array (instead of a single string) and includes updated tests and Storybook examples. Please check out the new PR for these additional improvements. Thanks!

@moh3n9595 moh3n9595 closed this Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants