Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value.Clean causes dereference error on Recursive type with nullable optional union #845

Closed
truumahn opened this issue Apr 23, 2024 · 2 comments

Comments

@truumahn
Copy link

Here's a repro with vitest:

import { Type, type StaticDecode, type TSchema } from "@sinclair/typebox";
import { test } from "vitest";
import { Value } from "@sinclair/typebox/value";

export function Parse<T extends TSchema, R = StaticDecode<T>>(
  schema: T,
  value: unknown
): R {
  const cloned = Value.Clone(value);
  const cleaned = Value.Clean(schema, cloned);
  return Value.Decode(schema, cleaned);
}

const RecursiveSchema = Type.Recursive((This) => {
  return Type.Object({
    id: Type.Number(),
    parent: Type.Optional(Type.Union([This, Type.Null()])),
  });
});

const inputs = [
  {
    id: 1,
    parent: {
      id: 2,
      parent: {
        id: 3,
      },
    },
  },
];

test("schema parses", () => {
  for (const input of inputs) {
    Parse(RecursiveSchema, input);
  }
});

If I remove the union from parent to be like this: Type.Optional(This), then there's no trouble.

@sinclairzx81
Copy link
Owner

@truumahn Hi, thanks for reporting. This is definitely a bug!

I have pushed a version on 0.32.23 where this issue should be resolved. Relevant PR is here #847

If you just want to test things your side, can close off this issue
Thanks again!
S

@truumahn
Copy link
Author

Thank you, works perfectly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants