Error with Transforming Nested Object in Struct Schema
is there a way to write a transform inside a Struct?
I am trying to parse this json object
and at the same time converting the nested object
when using this Schema to parse the object I get this error
is it trying here to apply the transformation two times?
I am trying to parse this json object
const test = {
id: "agent-4613",
name: "Bloody Darryl",
description:
"Before he was leader of",
rarity: {
id: "rarity_legendary",
name: "Superior",
color: "#d32ce6",
},
team: { id: "terrorists", name: "Terrorist" },
market_hash_name: "Bloody Darryl",
};const test = {
id: "agent-4613",
name: "Bloody Darryl",
description:
"Before he was leader of",
rarity: {
id: "rarity_legendary",
name: "Superior",
color: "#d32ce6",
},
team: { id: "terrorists", name: "Terrorist" },
market_hash_name: "Bloody Darryl",
};and at the same time converting the nested object
teamteam to a string using const _Basic = S.transform(
S.Struct({ name: S.String }),
S.String,
{
decode: (obj) => obj.name,
encode: (name) => ({ name }),
},
)
function transformRarity<T extends S.Struct.Fields>(struct: S.Struct<T>) {
return S.transform(
S.Struct({
...struct.fields,
rarity: S.String,
color: S.String,
}),
{
strict: true,
decode: (data) => ({
...data,
rarity: data.rarity.name,
color: data.rarity.color,
}),
encode: (data) => data,
},
)(struct);
}
export const Agent = S.Struct({
id: S.String,
name: S.String,
description: StringWithDefault,
rarity: _Rarity,
market_hash_name: StringWithDefault,
collections: S.Array(S.partialWith(Collection, { exact: true })),
team: _Basic,
}).pipe(transformRarity)const _Basic = S.transform(
S.Struct({ name: S.String }),
S.String,
{
decode: (obj) => obj.name,
encode: (name) => ({ name }),
},
)
function transformRarity<T extends S.Struct.Fields>(struct: S.Struct<T>) {
return S.transform(
S.Struct({
...struct.fields,
rarity: S.String,
color: S.String,
}),
{
strict: true,
decode: (data) => ({
...data,
rarity: data.rarity.name,
color: data.rarity.color,
}),
encode: (data) => data,
},
)(struct);
}
export const Agent = S.Struct({
id: S.String,
name: S.String,
description: StringWithDefault,
rarity: _Rarity,
market_hash_name: StringWithDefault,
collections: S.Array(S.partialWith(Collection, { exact: true })),
team: _Basic,
}).pipe(transformRarity)when using this Schema to parse the object I get this error
error: Uncaught (in promise) ParseError: ((Struct (Encoded side) <-> Struct (Type side)) <-> (Struct (Encoded side) <-> Struct (Type side)))
└─ Type side transformation failure
└─ (Struct (Encoded side) <-> Struct (Type side))
└─ Encoded side transformation failure
└─ Struct (Encoded side)
└─ ["team"]
└─ ({ readonly name: string } <-> string)
└─ Encoded side transformation failure
└─ Expected { readonly name: string }, actual "Terrorist"error: Uncaught (in promise) ParseError: ((Struct (Encoded side) <-> Struct (Type side)) <-> (Struct (Encoded side) <-> Struct (Type side)))
└─ Type side transformation failure
└─ (Struct (Encoded side) <-> Struct (Type side))
└─ Encoded side transformation failure
└─ Struct (Encoded side)
└─ ["team"]
└─ ({ readonly name: string } <-> string)
└─ Encoded side transformation failure
└─ Expected { readonly name: string }, actual "Terrorist" └─ Expected { readonly name: string }, actual "Terrorist" └─ Expected { readonly name: string }, actual "Terrorist"is it trying here to apply the transformation two times?
