Effect CommunityEC
Effect Communityβ€’3y agoβ€’
111 replies
attila

Declaring Fallback Values for Unknown Literals

What is the best way to declare fallback values for unknown literals? I'm tackling the problem of ensuring forward-compatibility of schemas and often I face literal values that may be extended in the future. For example: const planType = Schema.literal("lite", "professional", "enterprise"). Let's say someone introduces a new plan type enterprise-plus. I would not want the parsing of this schema to fail. The way I'm currently solving it is the following:

const planType = Schema.literal("lite", "professional", "enterprise", "unknown");
const parentObject = Schema.struct({
  planType: Schema.optional(planType).withDefault(() => "unknown")
});


The workaround is suboptimal for the following reasons:
- Parsing succeeds even if planType is missing or undefined, some schema users may want it to fail in such a case
- Only works if there is a parent object in which the schema can be placed, Schema.literal does not have a withDefault method on it.

I'd like to hear from you. How do you tackle forward-compatibility with schema? Am I just not aware of some API?
Was this page helpful?