The ParseIssue model in the @effect/schema/ParseResult module has undergone a comprehensive redesign and simplification that enhances its expressiveness without compromising functionality. This section explores the motivation and details of this refactoring.
Enhanced Schema.filter API
The Schema.filter API has been improved to support more complex filtering that can involve multiple properties of a struct. This is especially useful for validations that compare two fields, such as ensuring that a password field matches a confirm_password field, a common requirement in form validations.
Previous Limitations:
Previously, while it was possible to implement a filter that compared two fields, there was no straightforward way to attach validation messages to a specific field. This posed challenges, especially in form validations where precise error reporting is crucial.
Example of Previous Implementation:
In this scenario, while the filter functionally works, the lack of a specific error path (path: []) means errors are not as descriptive or helpful as they could be.
Specifying Error Paths
With the new improvements, it’s now possible to specify an error path along with the message, which enhances error specificity and is particularly beneficial for integration with tools like react-hook-form.
Updated Implementation Example:
This modification allows the error to be directly associated with the confirm_password field, improving clarity for the end-user.
Multiple Error Reporting
The refactored API also supports reporting multiple issues at once, which is useful in forms where several validation checks might fail simultaneously.
Example of Multiple Issues Reporting:
The new ParseIssue Model
The ParseIssue type has undergone a significant restructuring to improve its expressiveness and simplicity. This new model categorizes issues into leaf and composite types, enhancing clarity and making error handling more systematic.
Structure of ParseIssue Type:
Key Changes in the Model:
New Members:
Composite: A new class that aggregates multiple ParseIssue instances.
Missing: Identifies when a required element or value is absent.
Unexpected: Flags unexpected elements or values in the input.
Pointer: Points to the part of the data structure where an issue occurs.
Removed Members:
Previous categories like Declaration, TupleType, TypeLiteral, Union, Member, Key, and Index have been consolidated under the Composite type for a more streamlined approach.
Definition of Composite:
Refined Error Messaging System
We’ve updated our internal function getErrorMessage to enhance how error messages are formatted throughout our application. This function constructs an error message that includes the reason for the error, additional details, the path to where the error occurred, and the schema’s AST representation if available.
Example
Enhancing Tuples with Element Annotations
Annotations are used to add metadata to tuple elements, which can describe the purpose or requirements of each element more clearly. This can be particularly useful when generating documentation or JSON schemas from your schemas.
Missing messages
You can provide custom messages for missing fields or elements using the new missingMessage annotation.
Example (missing field)
Example (missing element)
Streamlining Annotations
The individual APIs that were previously used to add annotations to schemas have been removed. This change was made because these individual annotation APIs did not provide significant value and were burdensome to maintain. Instead, you can now use the annotations method directly or the Schema.annotations API for a pipe-able approach.
Before
Now
Standardize Error Handling for *Either, *Sync and asserts APIs
Now the *Sync and asserts APIs throw a ParseError while before they was throwing a simple Error with a cause containing a ParseIssue