How to Decode Nested Structures in Effect Typescript Schema?

Hi,

I have an encoded schema looking like that =>
{
  boxes: [{ box: { name: 'Box', isOpen: true } }]
}


I would like to decode it to something like that =>
{
  boxes: [{ name: 'Box', isOpen: true }]
}


I tried using transformation but that’s not handy to have to make the decode/operation for the already transformed field like BooleanFromString in that example =>

const StringBoolean = Schema.Literal('on', 'off')
const BooleanFromString = Schema.transform(StringBoolean, Schema.Boolean, {
  strict: true,
  decode: (value) => value === 'on',
  encode: (value) => (value ? 'on' : 'off'),
})
export class Box extends Schema.Class<Box>('Box')({
  isOpen: BooleanFromString,
}) {}

const BoxFromWrapper = Schema.transform(Schema.Struct({ box: Box }), Box, {
  strict: true,
  decode: ({ box }) => ({
    isOpen: box.isOpen ? ('on' as const) : ('off' as const),
  }),
  encode: (box) => ({ box: { isOpen: box.isOpen === 'on' } }),
})
export class Container extends Schema.Class<Container>('Container')({
  boxes: Schema.Array(BoxFromWrapper),
}) {}


Is there a way to do the same with a more handy (and less verbose way)?

I found https://effect.website/docs/schema/advanced-usage/#property-signatures but it seems that I cannot do something like that =>

boxes: Schema.Array(Schema.propertySignature(Box).pipe(Schema.fromKey(“box”)))
Effect Documentation
Learn advanced techniques for defining and extending data schemas, including recursive and mutually recursive types, optional fields, branded types, and schema transformations.
Was this page helpful?