Effect CommunityEC
Effect Community17mo ago
4 replies
Juan Sepulveda

Problem with Schema.Union and Schema.extend

Hello everyone, I’m new to using the effect library. I’m working with schemas, but I’m not getting the expected results. I’m not sure what I’m doing wrong,

Here’s the issue: numberField does not include the options property, even though I’ve defined it in the schema.

Here’s my code:
https://effect.website/play#f801699cdfca
const FieldBase = Schema.Struct({
  id: Schema.String,
  name: Schema.String,
  description: Schema.optional(Schema.String)
})

const TextField = Schema.extend(
  FieldBase,
  Schema.Struct({
    type: Schema.Literal("text")
  })
)

const NumberField = Schema.extend(
  FieldBase,
  Schema.Struct({
    type: Schema.Literal("number"),
    options: Schema.Struct({
      precision: Schema.Number
    })
  })
)

const FieldSchema = Schema.Union(
  TextField,
  NumberField
)

export type FieldSchema = typeof FieldSchema.Type

const CreateFieldSchema = FieldSchema.pipe(Schema.omit("id"))

const numberField = Schema.decodeUnknownSync(CreateFieldSchema)({
  name: "Number Field",
  type: "number",
  options: { precision: 1 }
}) 
/* Output: The `options` property is omitted.
   { name: 'Number Field', type: 'number', description: undefined }
*/

const textField = Schema.decodeUnknownSync(CreateFieldSchema)({
  name: "Text Field",
  type: "text"
})
/* Output: This works as expected.
   { name: 'Text Field', type: 'text', description: undefined }
*/

Any help will be appreciated, thanks!
Was this page helpful?