Effect CommunityEC
Effect Community3y ago
115 replies
Kristian Notari

Discriminating between parse errors when using `Schema.union` with two struct schemas

I found out when using a Schema.union with two struct schema, I get two parse errors back. Is there a way to discriminate over what's the "correct" error to show? Cause if I want either A or B, and you give me "weird B", I'd like to only report the validation errors from B, not an union of validation errors from A or B.

Example:
const mySchema = S.union(
   S.struct({ _tag: 'A', value: S.string }),
   S.struct({ _tag: 'B', value: S.number }),
)
S.parse(mySchema)({ _tag: 'B', "not a number" })
/* 2 errors found
 ─ union member
   └─ ["_tag"]
      └─ Expected "A", actual "B"
└─ union member
   └─ ["value"]
      └─ Expected "number", actual "not a number"
*/


Desired (something like this):
/* 1 error found
["value"]
└─ Expected "number", actual "not a number" for "B"
*/
Was this page helpful?