Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

From Discord: Assigning Error Messages to Specific Inputs in Effect/Schema with Hook Resolvers #2965

Closed
effect-bot opened this issue Jun 9, 2024 · 1 comment

Comments

@effect-bot
Copy link

Summary

Summary

The user is working with effect/schema and react-hook-form to validate a form schema. They want to assign custom error messages to specific fields within a nested structure. The user is encountering an issue where the error message from a higher-level filter is being overridden by a message from a lower-level validation rule.

Key Points

  1. Initial Schema Definition:

    • The user has a nested schema for an address, which includes fields like streetNumber, streetName, postalCode, etc.
    • They are using S.filter to validate the postalCode based on the country field.
  2. Issue:

    • The error message from the filter on the postalCode field is being overridden by the message from the ValidString validation.
    • The user wants the custom error message from the filter to take precedence.
  3. Possible Solutions:

    • One approach is to return a ParseIssue from the filter to indicate validation failure.
    • However, the message from ValidString still takes precedence.
  4. Comparison with Zod:

    • In Zod, you can specify the path in the error message, which allows for more granular control.
    • Zod also has a global error mapping feature that can handle such cases more elegantly.
  5. Current Implementation:

    • The user provided an example of how they are trying to implement the filter and custom error message in effect/schema.
    • The issue persists, and the user is considering moving the validation logic entirely into the filter.

Key Takeaways

  1. Message Precedence:

    • The message from a lower-level validation (like minLength in ValidString) takes precedence over a higher-level filter's custom message.
  2. Custom Error Handling:

    • Returning a ParseIssue from the filter can help in associating the error with a specific field, but it doesn't solve the precedence issue.
  3. API Design:

    • Zod's approach to error handling, which includes specifying paths and global error mapping, provides more flexibility and control.
  4. Need for Improvement:

    • The current API in effect/schema could be improved to allow more control over error message precedence, similar to Zod's approach.

Conclusion

The user is looking for a way to ensure that custom error messages from higher-level filters take precedence over lower-level validation messages. They are considering replicating Zod's API for better error handling and message control.

Discord thread

https://discord.com/channels/795981131316985866/1249377412026732576

@gcanti
Copy link
Contributor

gcanti commented Jun 17, 2024

#2906

@gcanti gcanti closed this as completed Jun 17, 2024
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

No branches or pull requests

2 participants