Open OliverJAsh opened 6 years ago
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 io-ts
or runtypes
. Since it looks like io-ts
has a tagged union primitive whereas runtypes
doesn't, maybe that's a better starting point. An io-unionize
library could theoretically take any t.taggedUnion
and produce from it a unionize
d instance. What do you think?
Agreed!
take any t.taggedUnion and produce from it a unionized instance
How do you think this would look?
@pelotom @OliverJAsh that would be great!
How do you think this would look?
I'm not sure what it would look like for io-ts
, but in runtypes
all types are backed by a Reflect
instance with fields that allow writing runtime algorithms against them. I would imagine a tagged union runtime type would have a tagName: string
field and a values: Record<string, Runtype>
field which maps tag values to variant types... these are the essential ingredients needed to pass to unionize
.
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: https://github.com/gcanti/io-ts/issues/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.