Skip to content

Commit

Permalink
fix: Schema.extend with nested union (#2396)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukovanej authored Mar 24, 2024
1 parent b6ee13b commit d17a427
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/warm-crews-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

Fix extend with nested union.
6 changes: 5 additions & 1 deletion packages/schema/src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2335,7 +2335,11 @@ const intersectUnionMembers = (
AST.Union.make(
xs.flatMap((x) => {
return ys.map((y) => {
if (AST.isTypeLiteral(x)) {
if (AST.isUnion(x)) {
return intersectUnionMembers(x.types, [y], path)
} else if (AST.isUnion(y)) {
return intersectUnionMembers([x], y.types, path)
} else if (AST.isTypeLiteral(x)) {
if (AST.isTypeLiteral(y)) {
return intersectTypeLiterals(x, y, path)
} else if (
Expand Down
20 changes: 20 additions & 0 deletions packages/schema/test/Schema/extend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ describe("Schema > extend", () => {
expect(is({ a: "b" })).toBe(false)
})

it(`union extend on nested union`, () => {
const schema = S.union(
S.union(
S.struct({ a: S.literal("a") }),
S.struct({ a: S.literal("b") })
),
S.struct({ b: S.literal("b") })
).pipe(
S.extend(S.struct({ c: S.boolean }))
)
const is = S.is(schema)

expect(is({ a: "a", c: false })).toBe(true)
expect(is({ b: "b", c: false })).toBe(true)
expect(is({ a: "b", c: false })).toBe(true)

expect(is({ a: "a" })).toBe(false)
expect(is({ a: "b" })).toBe(false)
})

it(`union extend union`, () => {
const schema = S.union(
S.struct({ a: S.literal("a") }),
Expand Down

0 comments on commit d17a427

Please sign in to comment.