Effect CommunityEC
Effect Community3y ago
36 replies
attila

Using `@effect/schema` for Refinements with Nested Schemas

I've started looking into refinements. As I understand, the way you do it in @effect/schema is that you use Schema.is where you pass in a schema. Simple example:
const flaggedAsBoolean = Schema.struct({ flag: Schema.boolean });
const flaggedAsTrue = Schema.struct({ flag: Schema.literal(true) });
const parse = Schema.parseSync(flaggedAsBoolean);
const isFlagTrue = Schema.is(flaggedAsTrue);

const flagged = parse({ flag: true });
if (isFlagTrue(flagged)) {
  const a: { flag: true } = flagged;
}

Now, what if flag is nested many many levels deep? Do I need to "duplicate" the schema and introduce the Schema.literal(true) change at the appropriate level? Does schema provide a simpler way to achieve it through the concept of optics?
Was this page helpful?