Skip to content

Value.Clean inconsistent behavior with Type.Codec and Type.Union #1479

@joshuaavalon

Description

@joshuaavalon

I was writing code using Type.Codec for serializing json request and response.
However, I found out under some cases, Value.Clean is not removing the value when it is using with Type.Codec and Type.Union.

In the example code, I expect schema3 and schema4 should return the same values.

Example Code

import { Type } from "typebox";
import { Compile } from "typebox/compile";

const schema1 = Type.Codec(Type.String()).Decode(s => Number.parseInt(s)).Encode(s => s.toString());
const schema2 = Type.Options(Type.Object({ a: Type.Ref("schema1") }), { $id: "schema2" });
const schema3 = Type.Object({ data: Type.Union([Type.Ref("schema2"), Type.Null()]) });
const schema4 = Type.Object({ data: Type.Ref("schema2") });

function main(): void {
  const a1 = Compile({ schema1, schema2 }, schema3);
  console.log(a1.Clean({ data: { a: 1, b: 2 } })); // { data: { a: 1, b: 2 } }

  const a2 = Compile({ schema1, schema2 }, schema4);
  console.log(a2.Clean({ data: { a: 1, b: 2 } })); // { data: { a: 1 } }
}

main();

Version

typebox: 1.0.61

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions