You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After reading the documentation, I understood that we can use Decoder.struct to convert a Form to, I could say, a FormValidated as below:
import*asDfrom"io-ts/Decoder"typeForm={foo: stringbar: number}typeFormValidated={foo: NonEmptyStringbar: 0|1}constformDecoder=D.struct({foo: /* Decoder to parse it */,bar: /* Decoder to parse it */})
Desired Behavior
My question is if there is a way to do the same thing for a type with a different structure. It's common here in the project I work on that we have a type that the backend expects and its structure isn't the same as the form. For example, can I convert Form to MyType in this example:
I see that I can use Decoder.struct to parse the form to an intermediary type (this would be the FormValidated) and then use Decoder.map to construct MyType, but is there a way to do that directly?
Suggested Solution
I implemented a function to achieve that, the way I use it is:
import*asDfrom"io-ts/Decoder"import*asDHfrom"./decoder-helper"typeForm={foo: stringbar: number}typeMyType={foobar: NonEmptyStringbarbaz: 0|1}constformDecoder=DH.top<Form,MyType>({foobar: DH.lift("foo",/* Decoder to parse it */),barbaz: DH.lift("bar",/* Decoder to parse it */)})
This way I can convert Form directly to MyType without needing to convert it to an intermediary type.
My function is a bit complex, that's why I'm asking if there is already a way to do that.
Who does this impact? Who is this for?
This would benefit everyone that is using this library as a form decoder or even to decode JSON that has a different structure than the target type
❓ Question
My current understanding
After reading the documentation, I understood that we can use
Decoder.struct
to convert aForm
to, I could say, aFormValidated
as below:Desired Behavior
My question is if there is a way to do the same thing for a type with a different structure. It's common here in the project I work on that we have a type that the backend expects and its structure isn't the same as the form. For example, can I convert
Form
toMyType
in this example:I see that I can use
Decoder.struct
to parse the form to an intermediary type (this would be theFormValidated
) and then useDecoder.map
to constructMyType
, but is there a way to do that directly?Suggested Solution
I implemented a function to achieve that, the way I use it is:
This way I can convert
Form
directly toMyType
without needing to convert it to an intermediary type.My function is a bit complex, that's why I'm asking if there is already a way to do that.
Who does this impact? Who is this for?
This would benefit everyone that is using this library as a form decoder or even to decode JSON that has a different structure than the target type
Describe alternatives you've considered
Decoder.struct
+Decoder.map
Additional context
I was inspired by this elm package
Your environment
The text was updated successfully, but these errors were encountered: