/
opt
/
canhelp
/
node_modules
/
zod
/
src
/
v4
/
classic
/
tests
/
/opt/canhelp/node_modules/zod/src/v4/classic/tests
mkdir
upload
Name
Size
Mode
Actions
anyunknown.test.ts
639
0644
edit
dl
rm
array.test.ts
7284
0644
edit
dl
rm
assignability.test.ts
6790
0644
edit
dl
rm
async-parsing.test.ts
12311
0644
edit
dl
rm
async-refinements.test.ts
1867
0644
edit
dl
rm
base.test.ts
179
0644
edit
dl
rm
bigint.test.ts
1956
0644
edit
dl
rm
brand.test.ts
2206
0644
edit
dl
rm
catch.test.ts
7224
0644
edit
dl
rm
coalesce.test.ts
743
0644
edit
dl
rm
coerce.test.ts
7736
0644
edit
dl
rm
continuability.test.ts
7478
0644
edit
dl
rm
custom.test.ts
1215
0644
edit
dl
rm
date.test.ts
962
0644
edit
dl
rm
datetime.test.ts
11511
0644
edit
dl
rm
default.test.ts
7717
0644
edit
dl
rm
description.test.ts
1294
0644
edit
dl
rm
discriminated-unions.test.ts
16810
0644
edit
dl
rm
enum.test.ts
8488
0644
edit
dl
rm
error-utils.test.ts
12572
0644
edit
dl
rm
error.test.ts
19502
0644
edit
dl
rm
file.test.ts
2460
0644
edit
dl
rm
firstparty.test.ts
2977
0644
edit
dl
rm
function.test.ts
6299
0644
edit
dl
rm
generics.test.ts
2162
0644
edit
dl
rm
index.test.ts
27562
0644
edit
dl
rm
instanceof.test.ts
1052
0644
edit
dl
rm
intersection.test.ts
4763
0644
edit
dl
rm
json.test.ts
3309
0644
edit
dl
rm
lazy.test.ts
4756
0644
edit
dl
rm
literal.test.ts
2478
0644
edit
dl
rm
map.test.ts
4817
0644
edit
dl
rm
nan.test.ts
645
0644
edit
dl
rm
nested-refine.test.ts
3623
0644
edit
dl
rm
nonoptional.test.ts
2405
0644
edit
dl
rm
nullable.test.ts
591
0644
edit
dl
rm
number.test.ts
7827
0644
edit
dl
rm
object.test.ts
15999
0644
edit
dl
rm
optional.test.ts
4130
0644
edit
dl
rm
partial.test.ts
4835
0644
edit
dl
rm
pickomit.test.ts
4422
0644
edit
dl
rm
pipe.test.ts
1848
0644
edit
dl
rm
prefault.test.ts
1007
0644
edit
dl
rm
preprocess.test.ts
6959
0644
edit
dl
rm
primitive.test.ts
7649
0644
edit
dl
rm
promise.test.ts
2315
0644
edit
dl
rm
prototypes.test.ts
498
0644
edit
dl
rm
readonly.test.ts
11777
0644
edit
dl
rm
record.test.ts
8118
0644
edit
dl
rm
recursive-types.test.ts
7335
0644
edit
dl
rm
refine.test.ts
14774
0644
edit
dl
rm
registries.test.ts
5999
0644
edit
dl
rm
set.test.ts
5572
0644
edit
dl
rm
standard-schema.test.ts
1369
0644
edit
dl
rm
string-formats.test.ts
3068
0644
edit
dl
rm
string.test.ts
64124
0644
edit
dl
rm
stringbool.test.ts
2333
0644
edit
dl
rm
template-literal.test.ts
34836
0644
edit
dl
rm
to-json-schema.test.ts
59276
0644
edit
dl
rm
transform.test.ts
6000
0644
edit
dl
rm
tuple.test.ts
4702
0644
edit
dl
rm
union.test.ts
2497
0644
edit
dl
rm
validations.test.ts
6736
0644
edit
dl
rm
void.test.ts
306
0644
edit
dl
rm
Edit:
/opt/canhelp/node_modules/zod/src/v4/classic/tests/recursive-types.test.ts
(7335B)
import { expect, expectTypeOf, test } from "vitest"; import { z } from "zod/v4"; test("recursion with z.lazy", () => { const data = { name: "I", subcategories: [ { name: "A", subcategories: [ { name: "1", subcategories: [ { name: "a", subcategories: [], }, ], }, ], }, ], }; const Category = z.object({ name: z.string(), get subcategories() { return z.array(Category).optional().nullable(); }, }); type Category = z.infer<typeof Category>; interface _Category { name: string; subcategories?: _Category[] | undefined | null; } expectTypeOf<Category>().toEqualTypeOf<_Category>(); Category.parse(data); }); test("recursion involving union type", () => { const data = { value: 1, next: { value: 2, next: { value: 3, next: { value: 4, next: null, }, }, }, }; const LL = z.object({ value: z.number(), get next() { return LL.nullable(); }, }); type LL = z.infer<typeof LL>; type _LL = { value: number; next: _LL | null; }; expectTypeOf<LL>().toEqualTypeOf<_LL>(); LL.parse(data); }); test("mutual recursion - native", () => { const Alazy = z.object({ val: z.number(), get b() { return Blazy; }, }); const Blazy = z.object({ val: z.number(), get a() { return Alazy.optional(); }, }); const testData = { val: 1, b: { val: 5, a: { val: 3, b: { val: 4, a: { val: 2, b: { val: 1, }, }, }, }, }, }; type Alazy = z.infer<typeof Alazy>; type Blazy = z.infer<typeof Blazy>; interface _Alazy { val: number; b: _Blazy; } interface _Blazy { val: number; a?: _Alazy | undefined; } expectTypeOf<Alazy>().toEqualTypeOf<_Alazy>(); expectTypeOf<Blazy>().toEqualTypeOf<_Blazy>(); Alazy.parse(testData); Blazy.parse(testData.b); expect(() => Alazy.parse({ val: "asdf" })).toThrow(); }); test("pick and omit with getter", () => { const Category = z.strictObject({ name: z.string(), get subcategories() { return z.array(Category); }, }); type Category = z.infer<typeof Category>; interface _Category { name: string; subcategories: _Category[]; } expectTypeOf<Category>().toEqualTypeOf<_Category>(); const PickedCategory = Category.pick({ name: true }); const OmittedCategory = Category.omit({ subcategories: true }); const picked = { name: "test" }; const omitted = { name: "test" }; PickedCategory.parse(picked); OmittedCategory.parse(omitted); expect(() => PickedCategory.parse({ name: "test", subcategories: [] })).toThrow(); expect(() => OmittedCategory.parse({ name: "test", subcategories: [] })).toThrow(); }); test("deferred self-recursion", () => { const Feature = z.object({ title: z.string(), get features(): z.ZodOptional<z.ZodArray<typeof Feature>> { return z.optional(z.array(Feature)); //.optional(); }, }); // type Feature = z.infer<typeof Feature>; const Output = z.object({ id: z.int(), //.nonnegative(), name: z.string(), get features(): z.ZodArray<typeof Feature> { return Feature.array(); }, }); type Output = z.output<typeof Output>; type _Feature = { title: string; features?: _Feature[] | undefined; }; type _Output = { id: number; name: string; features: _Feature[]; }; // expectTypeOf<Feature>().toEqualTypeOf<_Feature>(); expectTypeOf<Output>().toEqualTypeOf<_Output>(); }); test("deferred mutual recursion", () => { const Slot = z.object({ slotCode: z.string(), get blocks() { return z.array(Block); }, }); type Slot = z.infer<typeof Slot>; const Block = z.object({ blockCode: z.string(), get slots() { return z.array(Slot).optional(); }, }); type Block = z.infer<typeof Block>; const Page = z.object({ slots: z.array(Slot), }); type Page = z.infer<typeof Page>; type _Slot = { slotCode: string; blocks: _Block[]; }; type _Block = { blockCode: string; slots?: _Slot[] | undefined; }; type _Page = { slots: _Slot[]; }; expectTypeOf<Slot>().toEqualTypeOf<_Slot>(); expectTypeOf<Block>().toEqualTypeOf<_Block>(); expectTypeOf<Page>().toEqualTypeOf<_Page>(); }); test("mutual recursion with meta", () => { const A = z .object({ name: z.string(), get b() { return B; }, }) .readonly() .meta({ id: "A" }) .optional(); const B = z .object({ name: z.string(), get a() { return A; }, }) .readonly() .meta({ id: "B" }); type A = z.infer<typeof A>; type B = z.infer<typeof B>; type _A = | Readonly<{ name: string; b: _B; }> | undefined; // | undefined; type _B = Readonly<{ name: string; a?: _A; }>; expectTypeOf<A>().toEqualTypeOf<_A>(); expectTypeOf<B>().toEqualTypeOf<_B>(); }); test("recursion compatibility", () => { // array const A = z.object({ get array() { return A.array(); }, get optional() { return A.optional(); }, get nullable() { return A.nullable(); }, get nonoptional() { return A.nonoptional(); }, get readonly() { return A.readonly(); }, get describe() { return A.describe("A recursive type"); }, get meta() { return A.meta({ description: "A recursive type" }); }, get pipe() { return A.pipe(z.any()); }, get strict() { return A.strict(); }, get tuple() { return z.tuple([A, A]); }, get object() { return z .object({ subcategories: A, }) .strict() .loose(); }, get union() { return z.union([A, A]); }, get intersection() { return z.intersection(A, A); }, get record() { return z.record(z.string(), A); }, get map() { return z.map(z.string(), A); }, get set() { return z.set(A); }, get lazy() { return z.lazy(() => A); }, get promise() { return z.promise(A); }, }); }); // biome-ignore lint: sadf export type RecursiveA = z.ZodUnion< [ z.ZodObject<{ a: z.ZodDefault<RecursiveA>; b: z.ZodPrefault<RecursiveA>; c: z.ZodNonOptional<RecursiveA>; d: z.ZodOptional<RecursiveA>; e: z.ZodNullable<RecursiveA>; g: z.ZodReadonly<RecursiveA>; h: z.ZodPipe<RecursiveA, z.ZodString>; i: z.ZodArray<RecursiveA>; j: z.ZodSet<RecursiveA>; k: z.ZodMap<RecursiveA, RecursiveA>; l: z.ZodRecord<z.ZodString, RecursiveA>; m: z.ZodUnion<[RecursiveA, RecursiveA]>; n: z.ZodIntersection<RecursiveA, RecursiveA>; o: z.ZodLazy<RecursiveA>; p: z.ZodPromise<RecursiveA>; q: z.ZodCatch<RecursiveA>; r: z.ZodSuccess<RecursiveA>; s: z.ZodTransform<RecursiveA, string>; t: z.ZodTuple<[RecursiveA, RecursiveA]>; u: z.ZodObject<{ a: RecursiveA; }>; }>, ] >;
Save
cmd:
run