Skip to content

Commit

Permalink
Clean: Ensure References Passed for Union Check
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Apr 24, 2024
1 parent 3719b3e commit 6ba7c92
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/value/clean/clean.ts
Expand Up @@ -141,7 +141,7 @@ function FromTuple(schema: TTuple, references: TSchema[], value: unknown): any {
}
function FromUnion(schema: TUnion, references: TSchema[], value: unknown): any {
for (const inner of schema.anyOf) {
if (IsCheckable(inner) && Check(inner, value)) {
if (IsCheckable(inner) && Check(inner, references, value)) {
return Visit(inner, references, value)
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/runtime/value/clean/union.ts
Expand Up @@ -131,4 +131,25 @@ describe('value/clean/Union', () => {
const R = Value.Clean(T, { u: null, y: 1 })
Assert.IsEqual(R, { u: null, y: 1 })
})
// ----------------------------------------------------------------
// Union Recursive
//
// https://github.com/sinclairzx81/typebox/issues/845
// ----------------------------------------------------------------
it('Should clean recursive with union', () => {
const T = Type.Recursive((This) => Type.Object({
id: Type.Number(),
parent: Type.Union([This, Type.Null()]),
}))
const R = Value.Clean(T, {
id: 1,
unknown: 1,
parent: {
id: 2,
unknown: 1,
parent: null
},
})
Assert.IsEqual(R, { id: 1, parent: { id: 2, parent: null } })
})
})

0 comments on commit 6ba7c92

Please sign in to comment.