-
Notifications
You must be signed in to change notification settings - Fork 331
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
nullable and optional combinators in stable module #617
Comments
@etiennebatise feel free to provide a PR. That would make it a lot easier to review the code and give you feedback. |
@mlegenhausen will do 👍 |
As of version 2.2.19, there is a
Where
But that is incomplete because it loses the input and output information for the codec being wrapped with
which, I believe, is closer to what you get when you do |
So right now there is not code that can do
Right? I just want to be sure about that |
🚀 Feature request
Current Behavior
To define optional or nullable fields using the stable api (residing in the index module), we have to define unions :
An annoying consequence of that is described in this issue. tldr: when reporting, we get an error for each member of the union More on why this is annoying, at least to me, later.
Desired Behavior
It'd be nice to have some combinators to make those type definitions much more readable.
And it'd be nice to get around the issue of distinct errors for the same field.
A
nullable
combinator is already available in theDecoder
module.Suggested Solution
I was not able to come up for a solution by myself so I just took the author's code from the issue mentioned above. Note that I had to add a smelly
as undefined
to make this typecheck on my local setup.Similar thing for
nullable
. I monkey patched the above with no shame:Who does this impact? Who is this for?
This is for all users.
Describe alternatives you've considered
We could also provide those snippets as part of the docs or some other lib (eg:
io-ts-types
)Additional context
My initial goal was to create a JSON path reporter for my types. Unfortunately, when crawling the error contexts, I could not understand how to efficiently distinguish union context errors from array items errors.
For instance, given the following type
My custom error reporter would give me the following:
There is obviously no array of arrays in the original type but the only hint I get in the error context is if the key is a number (with
isNan(parseInt)
). In that case, I wrap the key between brackets. Also, this won't even work for dictionaries with number fields.Maybe I am missing something completely obvious and written in bold in the docs and all this is useless.
Your environment
The text was updated successfully, but these errors were encountered: