zod 3.26.0-canary.20250703T013930 → 3.26.0-canary.20250703T214020
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +20 -20
- package/src/index.ts +4 -0
- package/src/v3/ZodError.ts +330 -0
- package/src/v3/benchmarks/datetime.ts +58 -0
- package/src/v3/benchmarks/discriminatedUnion.ts +80 -0
- package/src/v3/benchmarks/index.ts +59 -0
- package/src/v3/benchmarks/ipv4.ts +57 -0
- package/src/v3/benchmarks/object.ts +69 -0
- package/src/v3/benchmarks/primitives.ts +162 -0
- package/src/v3/benchmarks/realworld.ts +63 -0
- package/src/v3/benchmarks/string.ts +55 -0
- package/src/v3/benchmarks/union.ts +80 -0
- package/src/v3/errors.ts +13 -0
- package/src/v3/external.ts +6 -0
- package/src/v3/helpers/enumUtil.ts +17 -0
- package/src/v3/helpers/errorUtil.ts +8 -0
- package/src/v3/helpers/parseUtil.ts +176 -0
- package/src/v3/helpers/partialUtil.ts +34 -0
- package/src/v3/helpers/typeAliases.ts +2 -0
- package/src/v3/helpers/util.ts +224 -0
- package/src/v3/index.ts +4 -0
- package/src/v3/locales/en.ts +124 -0
- package/src/v3/standard-schema.ts +113 -0
- package/src/v3/tests/Mocker.ts +54 -0
- package/src/v3/tests/all-errors.test.ts +157 -0
- package/src/v3/tests/anyunknown.test.ts +28 -0
- package/src/v3/tests/array.test.ts +71 -0
- package/src/v3/tests/async-parsing.test.ts +388 -0
- package/src/v3/tests/async-refinements.test.ts +46 -0
- package/src/v3/tests/base.test.ts +29 -0
- package/src/v3/tests/bigint.test.ts +55 -0
- package/src/v3/tests/branded.test.ts +53 -0
- package/src/v3/tests/catch.test.ts +220 -0
- package/src/v3/tests/coerce.test.ts +133 -0
- package/src/v3/tests/complex.test.ts +56 -0
- package/src/v3/tests/custom.test.ts +31 -0
- package/src/v3/tests/date.test.ts +32 -0
- package/src/v3/tests/deepmasking.test.ts +186 -0
- package/src/v3/tests/default.test.ts +112 -0
- package/src/v3/tests/description.test.ts +33 -0
- package/src/v3/tests/discriminated-unions.test.ts +315 -0
- package/src/v3/tests/enum.test.ts +80 -0
- package/src/v3/tests/error.test.ts +551 -0
- package/src/v3/tests/firstparty.test.ts +87 -0
- package/src/v3/tests/firstpartyschematypes.test.ts +21 -0
- package/src/v3/tests/function.test.ts +257 -0
- package/src/v3/tests/generics.test.ts +48 -0
- package/src/v3/tests/instanceof.test.ts +37 -0
- package/src/v3/tests/intersection.test.ts +110 -0
- package/src/v3/tests/language-server.source.ts +76 -0
- package/src/v3/tests/language-server.test.ts +207 -0
- package/src/v3/tests/literal.test.ts +36 -0
- package/src/v3/tests/map.test.ts +110 -0
- package/src/v3/tests/masking.test.ts +4 -0
- package/src/v3/tests/mocker.test.ts +19 -0
- package/src/v3/tests/nan.test.ts +21 -0
- package/src/v3/tests/nativeEnum.test.ts +87 -0
- package/src/v3/tests/nullable.test.ts +42 -0
- package/src/v3/tests/number.test.ts +176 -0
- package/src/v3/tests/object-augmentation.test.ts +29 -0
- package/src/v3/tests/object-in-es5-env.test.ts +29 -0
- package/src/v3/tests/object.test.ts +434 -0
- package/src/v3/tests/optional.test.ts +42 -0
- package/src/v3/tests/parseUtil.test.ts +23 -0
- package/src/v3/tests/parser.test.ts +41 -0
- package/src/v3/tests/partials.test.ts +243 -0
- package/src/v3/tests/pickomit.test.ts +111 -0
- package/src/v3/tests/pipeline.test.ts +29 -0
- package/src/v3/tests/preprocess.test.ts +186 -0
- package/src/v3/tests/primitive.test.ts +440 -0
- package/src/v3/tests/promise.test.ts +90 -0
- package/src/v3/tests/readonly.test.ts +194 -0
- package/src/v3/tests/record.test.ts +171 -0
- package/src/v3/tests/recursive.test.ts +197 -0
- package/src/v3/tests/refine.test.ts +313 -0
- package/src/v3/tests/safeparse.test.ts +27 -0
- package/src/v3/tests/set.test.ts +142 -0
- package/src/v3/tests/standard-schema.test.ts +83 -0
- package/src/v3/tests/string.test.ts +916 -0
- package/src/v3/tests/transformer.test.ts +233 -0
- package/src/v3/tests/tuple.test.ts +90 -0
- package/src/v3/tests/unions.test.ts +57 -0
- package/src/v3/tests/validations.test.ts +133 -0
- package/src/v3/tests/void.test.ts +15 -0
- package/src/v3/types.ts +5136 -0
- package/src/v4/classic/checks.ts +30 -0
- package/src/v4/classic/coerce.ts +27 -0
- package/src/v4/classic/compat.ts +66 -0
- package/src/v4/classic/errors.ts +75 -0
- package/src/v4/classic/external.ts +50 -0
- package/src/v4/classic/index.ts +5 -0
- package/src/v4/classic/iso.ts +90 -0
- package/src/v4/classic/parse.ts +33 -0
- package/src/v4/classic/schemas.ts +2055 -0
- package/src/v4/classic/tests/anyunknown.test.ts +26 -0
- package/src/v4/classic/tests/array.test.ts +264 -0
- package/src/v4/classic/tests/assignability.test.ts +210 -0
- package/src/v4/classic/tests/async-parsing.test.ts +381 -0
- package/src/v4/classic/tests/async-refinements.test.ts +68 -0
- package/src/v4/classic/tests/base.test.ts +7 -0
- package/src/v4/classic/tests/bigint.test.ts +54 -0
- package/src/v4/classic/tests/brand.test.ts +65 -0
- package/src/v4/classic/tests/catch.test.ts +252 -0
- package/src/v4/classic/tests/coalesce.test.ts +20 -0
- package/src/v4/classic/tests/coerce.test.ts +160 -0
- package/src/v4/classic/tests/continuability.test.ts +352 -0
- package/src/v4/classic/tests/custom.test.ts +40 -0
- package/src/v4/classic/tests/date.test.ts +31 -0
- package/src/v4/classic/tests/datetime.test.ts +296 -0
- package/src/v4/classic/tests/default.test.ts +313 -0
- package/src/v4/classic/tests/description.test.ts +32 -0
- package/src/v4/classic/tests/discriminated-unions.test.ts +592 -0
- package/src/v4/classic/tests/enum.test.ts +285 -0
- package/src/v4/classic/tests/error-utils.test.ts +527 -0
- package/src/v4/classic/tests/error.test.ts +711 -0
- package/src/v4/classic/tests/file.test.ts +91 -0
- package/src/v4/classic/tests/firstparty.test.ts +175 -0
- package/src/v4/classic/tests/function.test.ts +268 -0
- package/src/v4/classic/tests/generics.test.ts +72 -0
- package/src/v4/classic/tests/index.test.ts +829 -0
- package/src/v4/classic/tests/instanceof.test.ts +34 -0
- package/src/v4/classic/tests/intersection.test.ts +171 -0
- package/src/v4/classic/tests/json.test.ts +108 -0
- package/src/v4/classic/tests/lazy.test.ts +227 -0
- package/src/v4/classic/tests/literal.test.ts +92 -0
- package/src/v4/classic/tests/map.test.ts +196 -0
- package/src/v4/classic/tests/nan.test.ts +21 -0
- package/src/v4/classic/tests/nested-refine.test.ts +168 -0
- package/src/v4/classic/tests/nonoptional.test.ts +86 -0
- package/src/v4/classic/tests/nullable.test.ts +22 -0
- package/src/v4/classic/tests/number.test.ts +247 -0
- package/src/v4/classic/tests/object.test.ts +553 -0
- package/src/v4/classic/tests/optional.test.ts +103 -0
- package/src/v4/classic/tests/partial.test.ts +147 -0
- package/src/v4/classic/tests/pickomit.test.ts +127 -0
- package/src/v4/classic/tests/pipe.test.ts +81 -0
- package/src/v4/classic/tests/prefault.test.ts +37 -0
- package/src/v4/classic/tests/preprocess.test.ts +298 -0
- package/src/v4/classic/tests/primitive.test.ts +175 -0
- package/src/v4/classic/tests/promise.test.ts +81 -0
- package/src/v4/classic/tests/prototypes.test.ts +23 -0
- package/src/v4/classic/tests/readonly.test.ts +252 -0
- package/src/v4/classic/tests/record.test.ts +332 -0
- package/src/v4/classic/tests/recursive-types.test.ts +325 -0
- package/src/v4/classic/tests/refine.test.ts +423 -0
- package/src/v4/classic/tests/registries.test.ts +195 -0
- package/src/v4/classic/tests/set.test.ts +179 -0
- package/src/v4/classic/tests/standard-schema.test.ts +57 -0
- package/src/v4/classic/tests/string-formats.test.ts +109 -0
- package/src/v4/classic/tests/string.test.ts +881 -0
- package/src/v4/classic/tests/stringbool.test.ts +66 -0
- package/src/v4/classic/tests/template-literal.test.ts +758 -0
- package/src/v4/classic/tests/to-json-schema.test.ts +2182 -0
- package/src/v4/classic/tests/transform.test.ts +250 -0
- package/src/v4/classic/tests/tuple.test.ts +163 -0
- package/src/v4/classic/tests/union.test.ts +94 -0
- package/src/v4/classic/tests/validations.test.ts +283 -0
- package/src/v4/classic/tests/void.test.ts +12 -0
- package/src/v4/core/api.ts +1592 -0
- package/src/v4/core/checks.ts +1285 -0
- package/src/v4/core/config.ts +15 -0
- package/src/v4/core/core.ts +134 -0
- package/src/v4/core/doc.ts +44 -0
- package/src/v4/core/errors.ts +420 -0
- package/src/v4/core/function.ts +176 -0
- package/src/v4/core/index.ts +15 -0
- package/src/v4/core/json-schema.ts +143 -0
- package/src/v4/core/parse.ts +94 -0
- package/src/v4/core/regexes.ts +135 -0
- package/src/v4/core/registries.ts +86 -0
- package/src/v4/core/schemas.ts +3781 -0
- package/src/v4/core/standard-schema.ts +64 -0
- package/src/v4/core/tests/index.test.ts +46 -0
- package/src/v4/core/tests/locales/be.test.ts +124 -0
- package/src/v4/core/tests/locales/en.test.ts +22 -0
- package/src/v4/core/tests/locales/ru.test.ts +128 -0
- package/src/v4/core/tests/locales/tr.test.ts +69 -0
- package/src/v4/core/to-json-schema.ts +944 -0
- package/src/v4/core/util.ts +775 -0
- package/src/v4/core/versions.ts +5 -0
- package/src/v4/core/zsf.ts +323 -0
- package/src/v4/index.ts +4 -0
- package/src/v4/locales/ar.ts +125 -0
- package/src/v4/locales/az.ts +121 -0
- package/src/v4/locales/be.ts +184 -0
- package/src/v4/locales/ca.ts +127 -0
- package/src/v4/locales/cs.ts +142 -0
- package/src/v4/locales/de.ts +124 -0
- package/src/v4/locales/en.ts +127 -0
- package/src/v4/locales/eo.ts +125 -0
- package/src/v4/locales/es.ts +125 -0
- package/src/v4/locales/fa.ts +134 -0
- package/src/v4/locales/fi.ts +131 -0
- package/src/v4/locales/fr-CA.ts +126 -0
- package/src/v4/locales/fr.ts +124 -0
- package/src/v4/locales/he.ts +125 -0
- package/src/v4/locales/hu.ts +126 -0
- package/src/v4/locales/id.ts +125 -0
- package/src/v4/locales/index.ts +39 -0
- package/src/v4/locales/it.ts +125 -0
- package/src/v4/locales/ja.ts +122 -0
- package/src/v4/locales/kh.ts +126 -0
- package/src/v4/locales/ko.ts +131 -0
- package/src/v4/locales/mk.ts +127 -0
- package/src/v4/locales/ms.ts +124 -0
- package/src/v4/locales/nl.ts +126 -0
- package/src/v4/locales/no.ts +124 -0
- package/src/v4/locales/ota.ts +125 -0
- package/src/v4/locales/pl.ts +126 -0
- package/src/v4/locales/ps.ts +133 -0
- package/src/v4/locales/pt.ts +123 -0
- package/src/v4/locales/ru.ts +184 -0
- package/src/v4/locales/sl.ts +126 -0
- package/src/v4/locales/sv.ts +127 -0
- package/src/v4/locales/ta.ts +125 -0
- package/src/v4/locales/th.ts +126 -0
- package/src/v4/locales/tr.ts +121 -0
- package/src/v4/locales/ua.ts +126 -0
- package/src/v4/locales/ur.ts +126 -0
- package/src/v4/locales/vi.ts +125 -0
- package/src/v4/locales/zh-CN.ts +123 -0
- package/src/v4/locales/zh-TW.ts +125 -0
- package/src/v4/mini/checks.ts +32 -0
- package/src/v4/mini/coerce.ts +22 -0
- package/src/v4/mini/external.ts +40 -0
- package/src/v4/mini/index.ts +3 -0
- package/src/v4/mini/iso.ts +62 -0
- package/src/v4/mini/parse.ts +1 -0
- package/src/v4/mini/schemas.ts +1579 -0
- package/src/v4/mini/tests/assignability.test.ts +129 -0
- package/src/v4/mini/tests/brand.test.ts +51 -0
- package/src/v4/mini/tests/checks.test.ts +144 -0
- package/src/v4/mini/tests/computed.test.ts +36 -0
- package/src/v4/mini/tests/error.test.ts +22 -0
- package/src/v4/mini/tests/functions.test.ts +43 -0
- package/src/v4/mini/tests/index.test.ts +871 -0
- package/src/v4/mini/tests/number.test.ts +95 -0
- package/src/v4/mini/tests/object.test.ts +185 -0
- package/src/v4/mini/tests/prototypes.test.ts +43 -0
- package/src/v4/mini/tests/recursive-types.test.ts +275 -0
- package/src/v4/mini/tests/string.test.ts +293 -0
- package/src/v4-mini/index.ts +1 -0
- package/v4/classic/compat.cjs +1 -7
- package/v4/classic/compat.d.cts +0 -2
- package/v4/classic/compat.d.ts +0 -2
- package/v4/classic/compat.js +0 -6
- package/v4/classic/external.cjs +2 -1
- package/v4/classic/external.d.cts +1 -1
- package/v4/classic/external.d.ts +1 -1
- package/v4/classic/external.js +1 -1
- package/v4/classic/schemas.d.cts +4 -4
- package/v4/classic/schemas.d.ts +4 -4
- package/v4/core/api.cjs +11 -10
- package/v4/core/api.js +11 -10
- package/v4/core/checks.cjs +6 -4
- package/v4/core/checks.js +6 -4
- package/v4/core/core.cjs +5 -1
- package/v4/core/core.d.cts +2 -0
- package/v4/core/core.d.ts +2 -0
- package/v4/core/core.js +4 -0
- package/v4/core/schemas.cjs +12 -13
- package/v4/core/schemas.js +12 -13
- package/v4/core/util.cjs +3 -0
- package/v4/core/util.d.cts +1 -1
- package/v4/core/util.d.ts +1 -1
- package/v4/core/util.js +3 -0
- package/v4/locales/eo.cjs +144 -0
- package/v4/locales/eo.d.cts +5 -0
- package/v4/locales/eo.d.ts +5 -0
- package/v4/locales/eo.js +116 -0
- package/v4/locales/index.cjs +3 -1
- package/v4/locales/index.d.cts +1 -0
- package/v4/locales/index.d.ts +1 -0
- package/v4/locales/index.js +1 -0
- package/v4/mini/external.cjs +2 -1
- package/v4/mini/external.d.cts +1 -1
- package/v4/mini/external.d.ts +1 -1
- package/v4/mini/external.js +1 -1
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { test } from "vitest";
|
|
2
|
+
// import path from "path";
|
|
3
|
+
// import { Node, Project, SyntaxKind } from "ts-morph";
|
|
4
|
+
|
|
5
|
+
// import { filePath } from "./language-server.source";
|
|
6
|
+
|
|
7
|
+
// The following tool is helpful for understanding the TypeScript AST associated with these tests:
|
|
8
|
+
// https://ts-ast-viewer.com/ (just copy the contents of language-server.source into the viewer)
|
|
9
|
+
|
|
10
|
+
test("", () => {});
|
|
11
|
+
// describe("Executing Go To Definition (and therefore Find Usages and Rename Refactoring) using an IDE works on inferred object properties", () => {
|
|
12
|
+
// // Compile file developmentEnvironment.source
|
|
13
|
+
// const project = new Project({
|
|
14
|
+
// tsConfigFilePath: path.join(__dirname, "..", "..", "tsconfig.json"),
|
|
15
|
+
// skipAddingFilesFromTsConfig: true,
|
|
16
|
+
// });
|
|
17
|
+
// const sourceFile = project.addSourceFileAtPath(filePath);
|
|
18
|
+
|
|
19
|
+
// test("works for object properties inferred from z.object()", () => {
|
|
20
|
+
// // Find usage of Test.f1 property
|
|
21
|
+
// const instanceVariable =
|
|
22
|
+
// sourceFile.getVariableDeclarationOrThrow("instanceOfTest");
|
|
23
|
+
// const propertyBeingAssigned = getPropertyBeingAssigned(
|
|
24
|
+
// instanceVariable,
|
|
25
|
+
// "f1"
|
|
26
|
+
// );
|
|
27
|
+
|
|
28
|
+
// // Find definition of Test.f1 property
|
|
29
|
+
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
|
|
30
|
+
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
|
|
31
|
+
// SyntaxKind.VariableDeclaration
|
|
32
|
+
// );
|
|
33
|
+
|
|
34
|
+
// // Assert that find definition returned the Zod definition of Test
|
|
35
|
+
// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
|
|
36
|
+
// expect(parentOfProperty?.getName()).toEqual("Test");
|
|
37
|
+
// });
|
|
38
|
+
|
|
39
|
+
// // test("works for first object properties inferred from z.object().merge()", () => {
|
|
40
|
+
// // // Find usage of TestMerge.f1 property
|
|
41
|
+
// // const instanceVariable = sourceFile.getVariableDeclarationOrThrow(
|
|
42
|
+
// // "instanceOfTestMerge"
|
|
43
|
+
// // );
|
|
44
|
+
// // const propertyBeingAssigned = getPropertyBeingAssigned(
|
|
45
|
+
// // instanceVariable,
|
|
46
|
+
// // "f1"
|
|
47
|
+
// // );
|
|
48
|
+
|
|
49
|
+
// // // Find definition of TestMerge.f1 property
|
|
50
|
+
// // const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
|
|
51
|
+
// // const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
|
|
52
|
+
// // SyntaxKind.VariableDeclaration
|
|
53
|
+
// // );
|
|
54
|
+
|
|
55
|
+
// // // Assert that find definition returned the Zod definition of Test
|
|
56
|
+
// // expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
|
|
57
|
+
// // expect(parentOfProperty?.getName()).toEqual("Test");
|
|
58
|
+
// // });
|
|
59
|
+
|
|
60
|
+
// // test("works for second object properties inferred from z.object().merge()", () => {
|
|
61
|
+
// // // Find usage of TestMerge.f2 property
|
|
62
|
+
// // const instanceVariable = sourceFile.getVariableDeclarationOrThrow(
|
|
63
|
+
// // "instanceOfTestMerge"
|
|
64
|
+
// // );
|
|
65
|
+
// // const propertyBeingAssigned = getPropertyBeingAssigned(
|
|
66
|
+
// // instanceVariable,
|
|
67
|
+
// // "f2"
|
|
68
|
+
// // );
|
|
69
|
+
|
|
70
|
+
// // // Find definition of TestMerge.f2 property
|
|
71
|
+
// // const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
|
|
72
|
+
// // const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
|
|
73
|
+
// // SyntaxKind.VariableDeclaration
|
|
74
|
+
// // );
|
|
75
|
+
|
|
76
|
+
// // // Assert that find definition returned the Zod definition of TestMerge
|
|
77
|
+
// // expect(definitionOfProperty?.getText()).toEqual(
|
|
78
|
+
// // "f2: z.string().optional()"
|
|
79
|
+
// // );
|
|
80
|
+
// // expect(parentOfProperty?.getName()).toEqual("TestMerge");
|
|
81
|
+
// // });
|
|
82
|
+
|
|
83
|
+
// test("works for first object properties inferred from z.union()", () => {
|
|
84
|
+
// // Find usage of TestUnion.f1 property
|
|
85
|
+
// const instanceVariable = sourceFile.getVariableDeclarationOrThrow(
|
|
86
|
+
// "instanceOfTestUnion"
|
|
87
|
+
// );
|
|
88
|
+
// const propertyBeingAssigned = getPropertyBeingAssigned(
|
|
89
|
+
// instanceVariable,
|
|
90
|
+
// "f1"
|
|
91
|
+
// );
|
|
92
|
+
|
|
93
|
+
// // Find definition of TestUnion.f1 property
|
|
94
|
+
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
|
|
95
|
+
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
|
|
96
|
+
// SyntaxKind.VariableDeclaration
|
|
97
|
+
// );
|
|
98
|
+
|
|
99
|
+
// // Assert that find definition returned the Zod definition of Test
|
|
100
|
+
// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
|
|
101
|
+
// expect(parentOfProperty?.getName()).toEqual("Test");
|
|
102
|
+
// });
|
|
103
|
+
|
|
104
|
+
// test("works for second object properties inferred from z.union()", () => {
|
|
105
|
+
// // Find usage of TestUnion.f2 property
|
|
106
|
+
// const instanceVariable = sourceFile.getVariableDeclarationOrThrow(
|
|
107
|
+
// "instanceOfTestUnion"
|
|
108
|
+
// );
|
|
109
|
+
// const propertyBeingAssigned = getPropertyBeingAssigned(
|
|
110
|
+
// instanceVariable,
|
|
111
|
+
// "f2"
|
|
112
|
+
// );
|
|
113
|
+
|
|
114
|
+
// // Find definition of TestUnion.f2 property
|
|
115
|
+
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
|
|
116
|
+
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
|
|
117
|
+
// SyntaxKind.VariableDeclaration
|
|
118
|
+
// );
|
|
119
|
+
|
|
120
|
+
// // Assert that find definition returned the Zod definition of TestUnion
|
|
121
|
+
// expect(definitionOfProperty?.getText()).toEqual(
|
|
122
|
+
// "f2: z.string().optional()"
|
|
123
|
+
// );
|
|
124
|
+
// expect(parentOfProperty?.getName()).toEqual("TestUnion");
|
|
125
|
+
// });
|
|
126
|
+
|
|
127
|
+
// test("works for object properties inferred from z.object().partial()", () => {
|
|
128
|
+
// // Find usage of TestPartial.f1 property
|
|
129
|
+
// const instanceVariable = sourceFile.getVariableDeclarationOrThrow(
|
|
130
|
+
// "instanceOfTestPartial"
|
|
131
|
+
// );
|
|
132
|
+
// const propertyBeingAssigned = getPropertyBeingAssigned(
|
|
133
|
+
// instanceVariable,
|
|
134
|
+
// "f1"
|
|
135
|
+
// );
|
|
136
|
+
|
|
137
|
+
// // Find definition of TestPartial.f1 property
|
|
138
|
+
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
|
|
139
|
+
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
|
|
140
|
+
// SyntaxKind.VariableDeclaration
|
|
141
|
+
// );
|
|
142
|
+
|
|
143
|
+
// // Assert that find definition returned the Zod definition of Test
|
|
144
|
+
// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
|
|
145
|
+
// expect(parentOfProperty?.getName()).toEqual("Test");
|
|
146
|
+
// });
|
|
147
|
+
|
|
148
|
+
// test("works for object properties inferred from z.object().pick()", () => {
|
|
149
|
+
// // Find usage of TestPick.f1 property
|
|
150
|
+
// const instanceVariable =
|
|
151
|
+
// sourceFile.getVariableDeclarationOrThrow("instanceOfTestPick");
|
|
152
|
+
// const propertyBeingAssigned = getPropertyBeingAssigned(
|
|
153
|
+
// instanceVariable,
|
|
154
|
+
// "f1"
|
|
155
|
+
// );
|
|
156
|
+
|
|
157
|
+
// // Find definition of TestPick.f1 property
|
|
158
|
+
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
|
|
159
|
+
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
|
|
160
|
+
// SyntaxKind.VariableDeclaration
|
|
161
|
+
// );
|
|
162
|
+
|
|
163
|
+
// // Assert that find definition returned the Zod definition of Test
|
|
164
|
+
// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
|
|
165
|
+
// expect(parentOfProperty?.getName()).toEqual("Test");
|
|
166
|
+
// });
|
|
167
|
+
|
|
168
|
+
// test("works for object properties inferred from z.object().omit()", () => {
|
|
169
|
+
// // Find usage of TestOmit.f1 property
|
|
170
|
+
// const instanceVariable =
|
|
171
|
+
// sourceFile.getVariableDeclarationOrThrow("instanceOfTestOmit");
|
|
172
|
+
// const propertyBeingAssigned = getPropertyBeingAssigned(
|
|
173
|
+
// instanceVariable,
|
|
174
|
+
// "f1"
|
|
175
|
+
// );
|
|
176
|
+
|
|
177
|
+
// // Find definition of TestOmit.f1 property
|
|
178
|
+
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
|
|
179
|
+
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
|
|
180
|
+
// SyntaxKind.VariableDeclaration
|
|
181
|
+
// );
|
|
182
|
+
|
|
183
|
+
// // Assert that find definition returned the Zod definition of Test
|
|
184
|
+
// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
|
|
185
|
+
// expect(parentOfProperty?.getName()).toEqual("Test");
|
|
186
|
+
// });
|
|
187
|
+
// });
|
|
188
|
+
|
|
189
|
+
// const getPropertyBeingAssigned = (node: Node, name: string) => {
|
|
190
|
+
// const propertyAssignment = node.forEachDescendant((descendent) =>
|
|
191
|
+
// Node.isPropertyAssignment(descendent) && descendent.getName() == name
|
|
192
|
+
// ? descendent
|
|
193
|
+
// : undefined
|
|
194
|
+
// );
|
|
195
|
+
|
|
196
|
+
// if (propertyAssignment == null)
|
|
197
|
+
// fail(`Could not find property assignment with name ${name}`);
|
|
198
|
+
|
|
199
|
+
// const propertyLiteral = propertyAssignment.getFirstDescendantByKind(
|
|
200
|
+
// SyntaxKind.Identifier
|
|
201
|
+
// );
|
|
202
|
+
|
|
203
|
+
// if (propertyLiteral == null)
|
|
204
|
+
// fail(`Could not find property literal with name ${name}`);
|
|
205
|
+
|
|
206
|
+
// return propertyLiteral;
|
|
207
|
+
// };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// @ts-ignore TS6133
|
|
2
|
+
import { expect, test } from "vitest";
|
|
3
|
+
|
|
4
|
+
import * as z from "zod/v3";
|
|
5
|
+
|
|
6
|
+
const literalTuna = z.literal("tuna");
|
|
7
|
+
const literalFortyTwo = z.literal(42);
|
|
8
|
+
const literalTrue = z.literal(true);
|
|
9
|
+
|
|
10
|
+
const terrificSymbol = Symbol("terrific");
|
|
11
|
+
const literalTerrificSymbol = z.literal(terrificSymbol);
|
|
12
|
+
|
|
13
|
+
test("passing validations", () => {
|
|
14
|
+
literalTuna.parse("tuna");
|
|
15
|
+
literalFortyTwo.parse(42);
|
|
16
|
+
literalTrue.parse(true);
|
|
17
|
+
literalTerrificSymbol.parse(terrificSymbol);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("failing validations", () => {
|
|
21
|
+
expect(() => literalTuna.parse("shark")).toThrow();
|
|
22
|
+
expect(() => literalFortyTwo.parse(43)).toThrow();
|
|
23
|
+
expect(() => literalTrue.parse(false)).toThrow();
|
|
24
|
+
expect(() => literalTerrificSymbol.parse(Symbol("terrific"))).toThrow();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("invalid_literal should have `received` field with data", () => {
|
|
28
|
+
const data = "shark";
|
|
29
|
+
const result = literalTuna.safeParse(data);
|
|
30
|
+
if (!result.success) {
|
|
31
|
+
const issue = result.error.issues[0];
|
|
32
|
+
if (issue.code === "invalid_literal") {
|
|
33
|
+
expect(issue.received).toBe(data);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// @ts-ignore TS6133
|
|
2
|
+
import { expect, test } from "vitest";
|
|
3
|
+
|
|
4
|
+
import * as z from "zod/v3";
|
|
5
|
+
import { ZodIssueCode } from "zod/v3";
|
|
6
|
+
import { util } from "../helpers/util.js";
|
|
7
|
+
|
|
8
|
+
const stringMap = z.map(z.string(), z.string());
|
|
9
|
+
type stringMap = z.infer<typeof stringMap>;
|
|
10
|
+
|
|
11
|
+
test("type inference", () => {
|
|
12
|
+
util.assertEqual<stringMap, Map<string, string>>(true);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("valid parse", () => {
|
|
16
|
+
const result = stringMap.safeParse(
|
|
17
|
+
new Map([
|
|
18
|
+
["first", "foo"],
|
|
19
|
+
["second", "bar"],
|
|
20
|
+
])
|
|
21
|
+
);
|
|
22
|
+
expect(result.success).toEqual(true);
|
|
23
|
+
if (result.success) {
|
|
24
|
+
expect(result.data.has("first")).toEqual(true);
|
|
25
|
+
expect(result.data.has("second")).toEqual(true);
|
|
26
|
+
expect(result.data.get("first")).toEqual("foo");
|
|
27
|
+
expect(result.data.get("second")).toEqual("bar");
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("valid parse async", async () => {
|
|
32
|
+
const result = await stringMap.spa(
|
|
33
|
+
new Map([
|
|
34
|
+
["first", "foo"],
|
|
35
|
+
["second", "bar"],
|
|
36
|
+
])
|
|
37
|
+
);
|
|
38
|
+
expect(result.success).toEqual(true);
|
|
39
|
+
if (result.success) {
|
|
40
|
+
expect(result.data.has("first")).toEqual(true);
|
|
41
|
+
expect(result.data.has("second")).toEqual(true);
|
|
42
|
+
expect(result.data.get("first")).toEqual("foo");
|
|
43
|
+
expect(result.data.get("second")).toEqual("bar");
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("throws when a Set is given", () => {
|
|
48
|
+
const result = stringMap.safeParse(new Set([]));
|
|
49
|
+
expect(result.success).toEqual(false);
|
|
50
|
+
if (result.success === false) {
|
|
51
|
+
expect(result.error.issues.length).toEqual(1);
|
|
52
|
+
expect(result.error.issues[0].code).toEqual(ZodIssueCode.invalid_type);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("throws when the given map has invalid key and invalid input", () => {
|
|
57
|
+
const result = stringMap.safeParse(new Map([[42, Symbol()]]));
|
|
58
|
+
expect(result.success).toEqual(false);
|
|
59
|
+
if (result.success === false) {
|
|
60
|
+
expect(result.error.issues.length).toEqual(2);
|
|
61
|
+
expect(result.error.issues[0].code).toEqual(ZodIssueCode.invalid_type);
|
|
62
|
+
expect(result.error.issues[0].path).toEqual([0, "key"]);
|
|
63
|
+
expect(result.error.issues[1].code).toEqual(ZodIssueCode.invalid_type);
|
|
64
|
+
expect(result.error.issues[1].path).toEqual([0, "value"]);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("throws when the given map has multiple invalid entries", () => {
|
|
69
|
+
// const result = stringMap.safeParse(new Map([[42, Symbol()]]));
|
|
70
|
+
|
|
71
|
+
const result = stringMap.safeParse(
|
|
72
|
+
new Map([
|
|
73
|
+
[1, "foo"],
|
|
74
|
+
["bar", 2],
|
|
75
|
+
] as [any, any][]) as Map<any, any>
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
// const result = stringMap.safeParse(new Map([[42, Symbol()]]));
|
|
79
|
+
expect(result.success).toEqual(false);
|
|
80
|
+
if (result.success === false) {
|
|
81
|
+
expect(result.error.issues.length).toEqual(2);
|
|
82
|
+
expect(result.error.issues[0].code).toEqual(ZodIssueCode.invalid_type);
|
|
83
|
+
expect(result.error.issues[0].path).toEqual([0, "key"]);
|
|
84
|
+
expect(result.error.issues[1].code).toEqual(ZodIssueCode.invalid_type);
|
|
85
|
+
expect(result.error.issues[1].path).toEqual([1, "value"]);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("dirty", async () => {
|
|
90
|
+
const map = z.map(
|
|
91
|
+
z.string().refine((val) => val === val.toUpperCase(), {
|
|
92
|
+
message: "Keys must be uppercase",
|
|
93
|
+
}),
|
|
94
|
+
z.string()
|
|
95
|
+
);
|
|
96
|
+
const result = await map.spa(
|
|
97
|
+
new Map([
|
|
98
|
+
["first", "foo"],
|
|
99
|
+
["second", "bar"],
|
|
100
|
+
])
|
|
101
|
+
);
|
|
102
|
+
expect(result.success).toEqual(false);
|
|
103
|
+
if (!result.success) {
|
|
104
|
+
expect(result.error.issues.length).toEqual(2);
|
|
105
|
+
expect(result.error.issues[0].code).toEqual(z.ZodIssueCode.custom);
|
|
106
|
+
expect(result.error.issues[0].message).toEqual("Keys must be uppercase");
|
|
107
|
+
expect(result.error.issues[1].code).toEqual(z.ZodIssueCode.custom);
|
|
108
|
+
expect(result.error.issues[1].message).toEqual("Keys must be uppercase");
|
|
109
|
+
}
|
|
110
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// @ts-ignore TS6133
|
|
2
|
+
import { test } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { Mocker } from "./Mocker.js";
|
|
5
|
+
|
|
6
|
+
test("mocker", () => {
|
|
7
|
+
const mocker = new Mocker();
|
|
8
|
+
mocker.string;
|
|
9
|
+
mocker.number;
|
|
10
|
+
mocker.boolean;
|
|
11
|
+
mocker.null;
|
|
12
|
+
mocker.undefined;
|
|
13
|
+
mocker.stringOptional;
|
|
14
|
+
mocker.stringNullable;
|
|
15
|
+
mocker.numberOptional;
|
|
16
|
+
mocker.numberNullable;
|
|
17
|
+
mocker.booleanOptional;
|
|
18
|
+
mocker.booleanNullable;
|
|
19
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// @ts-ignore TS6133
|
|
2
|
+
import { expect, test } from "vitest";
|
|
3
|
+
|
|
4
|
+
import * as z from "zod/v3";
|
|
5
|
+
|
|
6
|
+
const schema = z.nan();
|
|
7
|
+
|
|
8
|
+
test("passing validations", () => {
|
|
9
|
+
schema.parse(Number.NaN);
|
|
10
|
+
schema.parse(Number("Not a number"));
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("failing validations", () => {
|
|
14
|
+
expect(() => schema.parse(5)).toThrow();
|
|
15
|
+
expect(() => schema.parse("John")).toThrow();
|
|
16
|
+
expect(() => schema.parse(true)).toThrow();
|
|
17
|
+
expect(() => schema.parse(null)).toThrow();
|
|
18
|
+
expect(() => schema.parse(undefined)).toThrow();
|
|
19
|
+
expect(() => schema.parse({})).toThrow();
|
|
20
|
+
expect(() => schema.parse([])).toThrow();
|
|
21
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// @ts-ignore TS6133
|
|
2
|
+
import { expect, test } from "vitest";
|
|
3
|
+
|
|
4
|
+
import * as z from "zod/v3";
|
|
5
|
+
import { util } from "../helpers/util.js";
|
|
6
|
+
|
|
7
|
+
test("nativeEnum test with consts", () => {
|
|
8
|
+
const Fruits: { Apple: "apple"; Banana: "banana" } = {
|
|
9
|
+
Apple: "apple",
|
|
10
|
+
Banana: "banana",
|
|
11
|
+
};
|
|
12
|
+
const fruitEnum = z.nativeEnum(Fruits);
|
|
13
|
+
type fruitEnum = z.infer<typeof fruitEnum>;
|
|
14
|
+
fruitEnum.parse("apple");
|
|
15
|
+
fruitEnum.parse("banana");
|
|
16
|
+
fruitEnum.parse(Fruits.Apple);
|
|
17
|
+
fruitEnum.parse(Fruits.Banana);
|
|
18
|
+
util.assertEqual<fruitEnum, "apple" | "banana">(true);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("nativeEnum test with real enum", () => {
|
|
22
|
+
enum Fruits {
|
|
23
|
+
Apple = "apple",
|
|
24
|
+
Banana = "banana",
|
|
25
|
+
}
|
|
26
|
+
// @ts-ignore
|
|
27
|
+
const fruitEnum = z.nativeEnum(Fruits);
|
|
28
|
+
type fruitEnum = z.infer<typeof fruitEnum>;
|
|
29
|
+
fruitEnum.parse("apple");
|
|
30
|
+
fruitEnum.parse("banana");
|
|
31
|
+
fruitEnum.parse(Fruits.Apple);
|
|
32
|
+
fruitEnum.parse(Fruits.Banana);
|
|
33
|
+
util.assertIs<fruitEnum extends Fruits ? true : false>(true);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("nativeEnum test with const with numeric keys", () => {
|
|
37
|
+
const FruitValues = {
|
|
38
|
+
Apple: 10,
|
|
39
|
+
Banana: 20,
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
} as const;
|
|
42
|
+
const fruitEnum = z.nativeEnum(FruitValues);
|
|
43
|
+
type fruitEnum = z.infer<typeof fruitEnum>;
|
|
44
|
+
fruitEnum.parse(10);
|
|
45
|
+
fruitEnum.parse(20);
|
|
46
|
+
fruitEnum.parse(FruitValues.Apple);
|
|
47
|
+
fruitEnum.parse(FruitValues.Banana);
|
|
48
|
+
util.assertEqual<fruitEnum, 10 | 20>(true);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("from enum", () => {
|
|
52
|
+
enum Fruits {
|
|
53
|
+
Cantaloupe = 0,
|
|
54
|
+
Apple = "apple",
|
|
55
|
+
Banana = "banana",
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const FruitEnum = z.nativeEnum(Fruits as any);
|
|
59
|
+
type _FruitEnum = z.infer<typeof FruitEnum>;
|
|
60
|
+
FruitEnum.parse(Fruits.Cantaloupe);
|
|
61
|
+
FruitEnum.parse(Fruits.Apple);
|
|
62
|
+
FruitEnum.parse("apple");
|
|
63
|
+
FruitEnum.parse(0);
|
|
64
|
+
expect(() => FruitEnum.parse(1)).toThrow();
|
|
65
|
+
expect(() => FruitEnum.parse("Apple")).toThrow();
|
|
66
|
+
expect(() => FruitEnum.parse("Cantaloupe")).toThrow();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("from const", () => {
|
|
70
|
+
const Greek = {
|
|
71
|
+
Alpha: "a",
|
|
72
|
+
Beta: "b",
|
|
73
|
+
Gamma: 3,
|
|
74
|
+
// @ts-ignore
|
|
75
|
+
} as const;
|
|
76
|
+
|
|
77
|
+
const GreekEnum = z.nativeEnum(Greek);
|
|
78
|
+
type _GreekEnum = z.infer<typeof GreekEnum>;
|
|
79
|
+
GreekEnum.parse("a");
|
|
80
|
+
GreekEnum.parse("b");
|
|
81
|
+
GreekEnum.parse(3);
|
|
82
|
+
expect(() => GreekEnum.parse("v")).toThrow();
|
|
83
|
+
expect(() => GreekEnum.parse("Alpha")).toThrow();
|
|
84
|
+
expect(() => GreekEnum.parse(2)).toThrow();
|
|
85
|
+
|
|
86
|
+
expect(GreekEnum.enum.Alpha).toEqual("a");
|
|
87
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// @ts-ignore TS6133
|
|
2
|
+
import { expect, test } from "vitest";
|
|
3
|
+
|
|
4
|
+
import * as z from "zod/v3";
|
|
5
|
+
|
|
6
|
+
function checkErrors(a: z.ZodTypeAny, bad: any) {
|
|
7
|
+
let expected: any;
|
|
8
|
+
try {
|
|
9
|
+
a.parse(bad);
|
|
10
|
+
} catch (error) {
|
|
11
|
+
expected = (error as z.ZodError).formErrors;
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
a.nullable().parse(bad);
|
|
15
|
+
} catch (error) {
|
|
16
|
+
expect((error as z.ZodError).formErrors).toEqual(expected);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
test("Should have error messages appropriate for the underlying type", () => {
|
|
21
|
+
checkErrors(z.string().min(2), 1);
|
|
22
|
+
z.string().min(2).nullable().parse(null);
|
|
23
|
+
checkErrors(z.number().gte(2), 1);
|
|
24
|
+
z.number().gte(2).nullable().parse(null);
|
|
25
|
+
checkErrors(z.boolean(), "");
|
|
26
|
+
z.boolean().nullable().parse(null);
|
|
27
|
+
checkErrors(z.null(), null);
|
|
28
|
+
z.null().nullable().parse(null);
|
|
29
|
+
checkErrors(z.null(), {});
|
|
30
|
+
z.null().nullable().parse(null);
|
|
31
|
+
checkErrors(z.object({}), 1);
|
|
32
|
+
z.object({}).nullable().parse(null);
|
|
33
|
+
checkErrors(z.tuple([]), 1);
|
|
34
|
+
z.tuple([]).nullable().parse(null);
|
|
35
|
+
checkErrors(z.unknown(), 1);
|
|
36
|
+
z.unknown().nullable().parse(null);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("unwrap", () => {
|
|
40
|
+
const unwrapped = z.string().nullable().unwrap();
|
|
41
|
+
expect(unwrapped).toBeInstanceOf(z.ZodString);
|
|
42
|
+
});
|