I'm experimenting with the code example found at https://github.com/joanllenas/ts.data.json to decode and validate a JSON payload in Typescript. While it works fine, my goal is to apply this concept to nested data structures, such as having a User object that contains an Address.
I attempted to define the type for Address using JsonDecoder.object, but I encountered an issue. My IDE (IntelliJ) suggests that I need a DecoderObject instead of a Decoder. Can anyone provide guidance on how to achieve this?
type Address = {
street: string;
town: string;
postcode: string;
};
type User = {
firstname: string;
lastname: string;
address: Address;
};
const addressDecoder = JsonDecoder.object<User>(
{
street: JsonDecoder.string,
town: JsonDecoder.string,
postcode: JsonDecoder.string
},
'User'
);
const userDecoder = JsonDecoder.object<User>(
{
firstname: JsonDecoder.string,
lastname: JsonDecoder.string,
address: JsonDecoder.object(addressDecoder, "AddressDecoder")
},
'User'
);