Effect CommunityEC
Effect Community2y ago
1 reply
attila

Handling Unknown Members in Union Schemas

Is there an "easy" way to express unknown members of a union in a schema for forward-compatibility purposes? For example, I would like to be able to write the following (or similar):
const myForwardCompatibleSchema = Schema.Union(
  Schema.Struct({ type: Schema.Literal("type1"), one: Schema.Number }),
  Schema.Struct({ type: Schema.Literal("type2"), two: Schema.Number }),
  Schema.Struct({ type: Schema.Literal("unknown") }),
).orElse(() => ({ type: "unknown" }))

Or is the current consensus that it's best to let decoding fail and then report such cases to Sentry or similar? On the other hand, if the rest of the data can be parsed without issues and it's just that whoever sent the data is on a newer version of the schema and the server could still process such request, why should it fail because of the schema version mismatch? It may still be processed successfully without that detail.
Was this page helpful?