Skip to content

Commit

Permalink
Add Test for Union Recursive Type (colinhacks#1148)
Browse files Browse the repository at this point in the history
* union recursive

* union recursive
  • Loading branch information
zevstravitz authored and MrAwesome committed May 20, 2022
1 parent 41cfc06 commit 1ac5ff4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
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

0 comments on commit 1ac5ff4

Please sign in to comment.