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

Add Test for Union Recursive Type #1148

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion deno/lib/__tests__/recursive.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @ts-ignore TS6133
import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts";
const test = Deno.test;

import { z } from "../index.ts";

interface Category {
Expand Down Expand Up @@ -50,6 +49,36 @@ test("recursion with z.lazy", () => {
test("schema getter", () => {
z.lazy(() => z.string()).schema.parse("asdf");
});

type LinkedList = null | { value: number; next: LinkedList };

const linkedListExample = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: {
value: 4,
next: null,
},
},
},
};

test("recursion involving union type", () => {
const LinkedListSchema: z.ZodType<LinkedList> = z.lazy(() =>
z.union([
z.null(),
z.object({
value: z.number(),
next: LinkedListSchema,
}),
])
);
LinkedListSchema.parse(linkedListExample);
});

// interface A {
// val: number;
// b: B;
Expand Down
33 changes: 31 additions & 2 deletions src/__tests__/recursive.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @ts-ignore TS6133
import { expect, test } from "@jest/globals";

import { test } from "@jest/globals";
import { z } from "..";

interface Category {
Expand Down Expand Up @@ -49,6 +48,36 @@ test("recursion with z.lazy", () => {
test("schema getter", () => {
z.lazy(() => z.string()).schema.parse("asdf");
});

type LinkedList = null | { value: number; next: LinkedList };

const linkedListExample = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: {
value: 4,
next: null,
},
},
},
};

test("recursion involving union type", () => {
const LinkedListSchema: z.ZodType<LinkedList> = z.lazy(() =>
z.union([
z.null(),
z.object({
value: z.number(),
next: LinkedListSchema,
}),
])
);
LinkedListSchema.parse(linkedListExample);
});

// interface A {
// val: number;
// b: B;
Expand Down