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

feat: Add selectiveUnion for improved Superstruct error messaging #2696

Merged
merged 8 commits into from
Sep 10, 2024

Conversation

FrederikBolding
Copy link
Member

@FrederikBolding FrederikBolding commented Sep 4, 2024

Adds selectiveUnion and rewrites a bunch of structs to use this pattern. This helps alleviate a problem where Superstruct errors would be impossible to read. The idea of selectiveUnion is to be able to guide Superstruct validation more with simple checks that can be used to choose between validation paths. This combined with typedUnion makes it possible to have drastically improved error messages for structs that use unions. This PR rewrites some of the UI structs to use these new patterns, but is not necessarily exhaustive. It also adds support for refinements and coercion to typedUnion.

Generally, this PR turns errors like this:

Invalid params: At path: ui -- Expected the value to satisfy a union of `union | union`, but received: "foo".'

into errors like this:

Invalid params: At path: ui -- Expected type to be one of: "Address", "Bold", "Box", "Button", "Copyable", "Divider", "Dropdown", "RadioGroup", "FileInput", "Form", "Heading", "Input", "Image", "Italic", "Link", "Row", "Spinner", "Text", "Tooltip", "Checkbox", "Card", "Icon", "Selector", "Section", "Container", but received: undefined.

This PR does not solve this problem for structs that use the children() utility function with more than one struct as the input (e.g. Button). These components will fail with something like:

Invalid params: At path: ui.props.children.1.props.children.props.children -- Expected the value to satisfy a union of `union | object | object`, but received: [object Object].

There may also still be other UI components that fail with similar errors, however, this is a huge step forward in readability and therefore dev-ex.

Progresses #2692

@FrederikBolding FrederikBolding changed the title feat: Add selectiveUnion for improved Superstruct error messaging feat: Add selectiveUnion for improved Superstruct error messaging Sep 4, 2024
@FrederikBolding FrederikBolding marked this pull request as ready for review September 4, 2024 14:35
@FrederikBolding FrederikBolding requested a review from a team as a code owner September 4, 2024 14:35
Copy link

codecov bot commented Sep 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.35%. Comparing base (8275454) to head (6295362).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2696      +/-   ##
==========================================
+ Coverage   94.31%   94.35%   +0.04%     
==========================================
  Files         481      481              
  Lines       10057    10129      +72     
  Branches     1521     1540      +19     
==========================================
+ Hits         9485     9557      +72     
  Misses        572      572              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Mrtenz
Copy link
Member

Mrtenz commented Sep 10, 2024

@metamaskbot update-pr

@Mrtenz Mrtenz merged commit d21f57b into main Sep 10, 2024
156 checks passed
@Mrtenz Mrtenz deleted the fb/selective-union branch September 10, 2024 13:21
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.

3 participants