const YesNo = Schema.Literal("Yes", "No");
const BooleanFromYesNo = Schema.transform(YesNo, Schema.Boolean, {
encode: (boolean) => (boolean ? "Yes" : "No"),
decode: (literal) => literal === "Yes",
});
const StructA = Schema.Struct({
enabled: YesNo, // <- Notice `YesNo`
anotherKey: Schema.String,
});
const StructB = Schema.Struct({
enabled: Schema.Boolean, // <- Notice `Schema.Boolean`
someOtherKey: Schema.String,
});
const StructAFromStructB = Schema.transform(StructA, StructB, {
encode: (structB) => {
// What if BooleanFromYesNo could fail?
// - How can we propogate that failure here?
// - How can we communicate that the `enabled` property has the issue?
const enabled = Schema.encodeSync(BooleanFromYesNo)(structB.enabled);
return { enabled, anotherKey: structB.someOtherKey };
},
decode: (structA) => {
const enabled = Schema.decodeSync(BooleanFromYesNo)(structA.enabled);
return { enabled, someOtherKey: structA.anotherKey };
},
});
const YesNo = Schema.Literal("Yes", "No");
const BooleanFromYesNo = Schema.transform(YesNo, Schema.Boolean, {
encode: (boolean) => (boolean ? "Yes" : "No"),
decode: (literal) => literal === "Yes",
});
const StructA = Schema.Struct({
enabled: YesNo, // <- Notice `YesNo`
anotherKey: Schema.String,
});
const StructB = Schema.Struct({
enabled: Schema.Boolean, // <- Notice `Schema.Boolean`
someOtherKey: Schema.String,
});
const StructAFromStructB = Schema.transform(StructA, StructB, {
encode: (structB) => {
// What if BooleanFromYesNo could fail?
// - How can we propogate that failure here?
// - How can we communicate that the `enabled` property has the issue?
const enabled = Schema.encodeSync(BooleanFromYesNo)(structB.enabled);
return { enabled, anotherKey: structB.someOtherKey };
},
decode: (structA) => {
const enabled = Schema.decodeSync(BooleanFromYesNo)(structA.enabled);
return { enabled, someOtherKey: structA.anotherKey };
},
});