Error visitor for custom types #1199
laurence-myers
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello! 👋🙂
In my app, I have implemented a "discriminated union" custom type, and registered it in TypeRegistry. I discovered that TypeBox's error reporting for custom types ("Kind") is limited, and only says "Expected NameOfCustomType". I could specify a custom error message, using
SetErrorFunction()
, but the info available at that point is very limited.With a bit of tinkering, I found that TypeBox could potentially support a custom "visitor" as part of
Errors()
! The visitor would be a generator function, yielding one or more errorValueError
s.I was wondering if I could add this to TypeBox, and if so, how you would like it to look?
For a proper implementation, I was thinking of changing TypeRegistry to either accept a function, or an object, roughly like this:
For my app, I've just used patch-package to add a quick hack to TypeBox.
(Click to see the patch)
Usage (cut down from my actual implementation, but gives you an idea):
Beta Was this translation helpful? Give feedback.
All reactions