Effect CommunityEC
Effect Communityβ€’2y agoβ€’
57 replies
Steff

Adding Custom Logic in Schema.Class Constructor

Is it possible to add custom logic within the constructor of Schema.Class (or Schema.struct) based on other properties? Something like the following:
const PersonInsured = Schema.struct({
  age: pipe(Schema.number, Schema.positive(), Schema.lessThan(200)),
  gender: pipe(Schema.literal("male", "female")),
  price: Schema.optional(pipe(Schema.number, Schema.positive()), {
    default: () => 15,
  }),
}).pipe(
  Schema.filter((o) => {
    let price = o.price;

    if (o.age >= 50) {
      price *= 2;
    }
    if (o.gender === "male") {
      price *= 2;
    }
    return price;
  })
);

ps. its ok to have also basePrice and calculatedPrice props
Was this page helpful?