Effect CommunityEC
Effect Community16mo ago
11 replies
talysson.oliveira

Setting Default Value for Optional Field based on Required Fields in TypeScript Schema Class

hey there! I have this schema, where name is optional and the other two fields are required

export class Round extends Schema.Class<Round>('Round')({
  name: Schema.optional(SchemaSchema.NonEmptyString),
  referenceSemester: Schema.Union(Schema.Literal(1), Schema.Literal(2)),
  referenceYear: Schema.Number.pipe(Schema.positive()),
}) {}

I would like to make the default value of name be ${referenceYear}/${referenceSemester} if name is not present when I call Round.make, but I could not find a way to do it using Schema.optionalWith. I'm not sure if that should be done with encode/decode or if there's something specific for this scenario. Can anyone help me on that?
Was this page helpful?