Effect CommunityEC
Effect Community2y ago
2 replies
David Novák

Use default value when decoding fails

Let's say we have a struct with field that accepts multiple literals. I want to decode it in a way, that if the decoding of the field fails, the
unknown
is set as a value.

I was used to doing it in a zod like this:
const zodObject = z.object({
  someLiterals: z.enum(['valueA', 'valueB', 'unknown']).catch('unknown'),
  // ... imagine there are more fields here
})

const schemaObject = Schema.Struct({
  someLiterals: Schema.Literal('valueA', 'valueB', 'unknown'), // How to catch errors, if there is an unknown value?
  // ...imagine there are more fields here
})


So object like this {someLiterals: 'valueC'} gets encoded into {someLiterals: 'unknown'}.

Is it possible to do with some transformer in schema?
Was this page helpful?