-
Notifications
You must be signed in to change notification settings - Fork 14
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
Deserialising/validating/decoding JSON strings to unionize types #41
Comments
The rule I try to live by with my libraries is that they should do one very narrowly focused thing and (hopefully) do it well. This is mostly out of necessity, because when scope explodes so does the maintenance burden, but I also prefer to use libraries like that, because they compose well together and can be readily swapped out for new solutions as needed. So anyway, I think this is a great case for a composite library which leverages either |
Agreed!
How do you think this would look? |
@pelotom @OliverJAsh that would be great! |
I'm not sure what it would look like for I don't have the bandwidth to investigate this more fully, just spitballin' here 😄 |
I filed an issue with io-ts to see if there's a way we can incorporate the benefits of unionize into io-ts, so we have the best of both worlds: |
For anyone who is interested, this might be helpful: gcanti/io-ts#187 |
Do you have any suggestions how to validate values as unionize types? Here is a real world example of where this is needed.
I have a union type to represent a page modal.
The modal is specified to the application through the URL as a query param, e.g.
http://foo.com/?modal=VALUE
, whereVALUE
is an object after it has been JSON stringified and URI encoded, e.g.In my application I want to be able to match against the modal using the match helpers provide by unionize. However, it is not safe to do so, because the
modal
query param could be a string containing anything, e.g.modal=foo
.For this reason I need to validate the value before matching against it.
In the past I've enjoyed using io-ts for the purpose of validating. I am aware you also have a similar library called runtypes.
If this is a common use case, I wonder if there's anything we could work into the library, or build on top of it, to make it easier.
Here is a working example that uses io-ts and tries to share as much as possible, but duplication is inevitable and it's a lot of boilerplate.
The text was updated successfully, but these errors were encountered: