zod 3.26.0-canary.20250703T013930 → 3.26.0-canary.20250703T025502
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/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 +38 -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/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,175 @@
|
|
|
1
|
+
import { expect, expectTypeOf, test } from "vitest";
|
|
2
|
+
import * as z from "zod/v4";
|
|
3
|
+
|
|
4
|
+
const literalStringSchema = z.literal("asdf");
|
|
5
|
+
const literalNumberSchema = z.literal(12);
|
|
6
|
+
const literalBooleanSchema = z.literal(true);
|
|
7
|
+
const literalBigIntSchema = z.literal(BigInt(42));
|
|
8
|
+
|
|
9
|
+
const stringSchema = z.string();
|
|
10
|
+
const numberSchema = z.number();
|
|
11
|
+
const bigintSchema = z.bigint();
|
|
12
|
+
const booleanSchema = z.boolean();
|
|
13
|
+
const dateSchema = z.date();
|
|
14
|
+
const symbolSchema = z.symbol();
|
|
15
|
+
const nullSchema = z.null();
|
|
16
|
+
const undefinedSchema = z.undefined();
|
|
17
|
+
const stringSchemaOptional = z.string().optional();
|
|
18
|
+
const stringSchemaNullable = z.string().nullable();
|
|
19
|
+
const numberSchemaOptional = z.number().optional();
|
|
20
|
+
const numberSchemaNullable = z.number().nullable();
|
|
21
|
+
const bigintSchemaOptional = z.bigint().optional();
|
|
22
|
+
const bigintSchemaNullable = z.bigint().nullable();
|
|
23
|
+
const booleanSchemaOptional = z.boolean().optional();
|
|
24
|
+
const booleanSchemaNullable = z.boolean().nullable();
|
|
25
|
+
const dateSchemaOptional = z.date().optional();
|
|
26
|
+
const dateSchemaNullable = z.date().nullable();
|
|
27
|
+
const symbolSchemaOptional = z.symbol().optional();
|
|
28
|
+
const symbolSchemaNullable = z.symbol().nullable();
|
|
29
|
+
|
|
30
|
+
test("literal string schema", () => {
|
|
31
|
+
expect(literalStringSchema.parse("asdf")).toBe("asdf");
|
|
32
|
+
expect(() => literalStringSchema.parse("not_asdf")).toThrow();
|
|
33
|
+
expect(() => literalStringSchema.parse(123)).toThrow();
|
|
34
|
+
expect(() => literalStringSchema.parse(true)).toThrow();
|
|
35
|
+
expect(() => literalStringSchema.parse({})).toThrow();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("literal number schema", () => {
|
|
39
|
+
expect(literalNumberSchema.parse(12)).toBe(12);
|
|
40
|
+
expect(() => literalNumberSchema.parse(13)).toThrow();
|
|
41
|
+
expect(() => literalNumberSchema.parse("foo")).toThrow();
|
|
42
|
+
expect(() => literalNumberSchema.parse(true)).toThrow();
|
|
43
|
+
expect(() => literalNumberSchema.parse({})).toThrow();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("literal boolean schema", () => {
|
|
47
|
+
expect(literalBooleanSchema.parse(true)).toBe(true);
|
|
48
|
+
expect(() => literalBooleanSchema.parse(false)).toThrow();
|
|
49
|
+
expect(() => literalBooleanSchema.parse("asdf")).toThrow();
|
|
50
|
+
expect(() => literalBooleanSchema.parse(123)).toThrow();
|
|
51
|
+
expect(() => literalBooleanSchema.parse({})).toThrow();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("literal bigint schema", () => {
|
|
55
|
+
expect(literalBigIntSchema.parse(BigInt(42))).toBe(BigInt(42));
|
|
56
|
+
expect(() => literalBigIntSchema.parse(BigInt(43))).toThrow();
|
|
57
|
+
expect(() => literalBigIntSchema.parse("asdf")).toThrow();
|
|
58
|
+
expect(() => literalBigIntSchema.parse(123)).toThrow();
|
|
59
|
+
expect(() => literalBigIntSchema.parse({})).toThrow();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("string schema", () => {
|
|
63
|
+
stringSchema.parse("foo");
|
|
64
|
+
expect(() => stringSchema.parse(Math.random())).toThrow();
|
|
65
|
+
expect(() => stringSchema.parse(true)).toThrow();
|
|
66
|
+
expect(() => stringSchema.parse(undefined)).toThrow();
|
|
67
|
+
expect(() => stringSchema.parse(null)).toThrow();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("number schema", () => {
|
|
71
|
+
numberSchema.parse(Math.random());
|
|
72
|
+
expect(() => numberSchema.parse("foo")).toThrow();
|
|
73
|
+
expect(() => numberSchema.parse(BigInt(17))).toThrow();
|
|
74
|
+
expect(() => numberSchema.parse(true)).toThrow();
|
|
75
|
+
expect(() => numberSchema.parse(undefined)).toThrow();
|
|
76
|
+
expect(() => numberSchema.parse(null)).toThrow();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("bigint schema", () => {
|
|
80
|
+
bigintSchema.parse(BigInt(17));
|
|
81
|
+
expect(() => bigintSchema.parse("foo")).toThrow();
|
|
82
|
+
expect(() => bigintSchema.parse(Math.random())).toThrow();
|
|
83
|
+
expect(() => bigintSchema.parse(true)).toThrow();
|
|
84
|
+
expect(() => bigintSchema.parse(undefined)).toThrow();
|
|
85
|
+
expect(() => bigintSchema.parse(null)).toThrow();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("boolean schema", () => {
|
|
89
|
+
booleanSchema.parse(true);
|
|
90
|
+
expect(() => booleanSchema.parse("foo")).toThrow();
|
|
91
|
+
expect(() => booleanSchema.parse(Math.random())).toThrow();
|
|
92
|
+
expect(() => booleanSchema.parse(undefined)).toThrow();
|
|
93
|
+
expect(() => booleanSchema.parse(null)).toThrow();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("date schema", async () => {
|
|
97
|
+
dateSchema.parse(new Date());
|
|
98
|
+
expect(() => dateSchema.parse("foo")).toThrow();
|
|
99
|
+
expect(() => dateSchema.parse(Math.random())).toThrow();
|
|
100
|
+
expect(() => dateSchema.parse(true)).toThrow();
|
|
101
|
+
expect(() => dateSchema.parse(undefined)).toThrow();
|
|
102
|
+
expect(() => dateSchema.parse(null)).toThrow();
|
|
103
|
+
expect(await dateSchema.safeParseAsync(new Date("invalid"))).toMatchInlineSnapshot(`
|
|
104
|
+
{
|
|
105
|
+
"error": [ZodError: [
|
|
106
|
+
{
|
|
107
|
+
"expected": "date",
|
|
108
|
+
"code": "invalid_type",
|
|
109
|
+
"received": "Invalid Date",
|
|
110
|
+
"path": [],
|
|
111
|
+
"message": "Invalid input: expected date, received Date"
|
|
112
|
+
}
|
|
113
|
+
]],
|
|
114
|
+
"success": false,
|
|
115
|
+
}
|
|
116
|
+
`);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("symbol schema", () => {
|
|
120
|
+
symbolSchema.parse(Symbol("foo"));
|
|
121
|
+
expect(() => symbolSchema.parse("foo")).toThrow();
|
|
122
|
+
expect(() => symbolSchema.parse(Math.random())).toThrow();
|
|
123
|
+
expect(() => symbolSchema.parse(true)).toThrow();
|
|
124
|
+
expect(() => symbolSchema.parse(new Date())).toThrow();
|
|
125
|
+
expect(() => symbolSchema.parse(undefined)).toThrow();
|
|
126
|
+
expect(() => symbolSchema.parse(null)).toThrow();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("undefined schema", () => {
|
|
130
|
+
undefinedSchema.parse(undefined);
|
|
131
|
+
expect(() => undefinedSchema.parse("foo")).toThrow();
|
|
132
|
+
expect(() => undefinedSchema.parse(Math.random())).toThrow();
|
|
133
|
+
expect(() => undefinedSchema.parse(true)).toThrow();
|
|
134
|
+
expect(() => undefinedSchema.parse(null)).toThrow();
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("null schema", () => {
|
|
138
|
+
nullSchema.parse(null);
|
|
139
|
+
expect(() => nullSchema.parse("foo")).toThrow();
|
|
140
|
+
expect(() => nullSchema.parse(Math.random())).toThrow();
|
|
141
|
+
expect(() => nullSchema.parse(true)).toThrow();
|
|
142
|
+
expect(() => nullSchema.parse(undefined)).toThrow();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test("primitive inference", () => {
|
|
146
|
+
expectTypeOf<z.TypeOf<typeof literalStringSchema>>().toEqualTypeOf<"asdf">();
|
|
147
|
+
expectTypeOf<z.TypeOf<typeof literalNumberSchema>>().toEqualTypeOf<12>();
|
|
148
|
+
expectTypeOf<z.TypeOf<typeof literalBooleanSchema>>().toEqualTypeOf<true>();
|
|
149
|
+
expectTypeOf<z.TypeOf<typeof literalBigIntSchema>>().toEqualTypeOf<bigint>();
|
|
150
|
+
expectTypeOf<z.TypeOf<typeof stringSchema>>().toEqualTypeOf<string>();
|
|
151
|
+
expectTypeOf<z.TypeOf<typeof numberSchema>>().toEqualTypeOf<number>();
|
|
152
|
+
expectTypeOf<z.TypeOf<typeof bigintSchema>>().toEqualTypeOf<bigint>();
|
|
153
|
+
expectTypeOf<z.TypeOf<typeof booleanSchema>>().toEqualTypeOf<boolean>();
|
|
154
|
+
expectTypeOf<z.TypeOf<typeof dateSchema>>().toEqualTypeOf<Date>();
|
|
155
|
+
expectTypeOf<z.TypeOf<typeof symbolSchema>>().toEqualTypeOf<symbol>();
|
|
156
|
+
expectTypeOf<z.TypeOf<typeof nullSchema>>().toEqualTypeOf<null>();
|
|
157
|
+
expectTypeOf<z.TypeOf<typeof undefinedSchema>>().toEqualTypeOf<undefined>();
|
|
158
|
+
expectTypeOf<z.TypeOf<typeof stringSchemaOptional>>().toEqualTypeOf<string | undefined>();
|
|
159
|
+
expectTypeOf<z.TypeOf<typeof stringSchemaNullable>>().toEqualTypeOf<string | null>();
|
|
160
|
+
expectTypeOf<z.TypeOf<typeof numberSchemaOptional>>().toEqualTypeOf<number | undefined>();
|
|
161
|
+
expectTypeOf<z.TypeOf<typeof numberSchemaNullable>>().toEqualTypeOf<number | null>();
|
|
162
|
+
expectTypeOf<z.TypeOf<typeof bigintSchemaOptional>>().toEqualTypeOf<bigint | undefined>();
|
|
163
|
+
expectTypeOf<z.TypeOf<typeof bigintSchemaNullable>>().toEqualTypeOf<bigint | null>();
|
|
164
|
+
expectTypeOf<z.TypeOf<typeof booleanSchemaOptional>>().toEqualTypeOf<boolean | undefined>();
|
|
165
|
+
expectTypeOf<z.TypeOf<typeof booleanSchemaNullable>>().toEqualTypeOf<boolean | null>();
|
|
166
|
+
expectTypeOf<z.TypeOf<typeof dateSchemaOptional>>().toEqualTypeOf<Date | undefined>();
|
|
167
|
+
expectTypeOf<z.TypeOf<typeof dateSchemaNullable>>().toEqualTypeOf<Date | null>();
|
|
168
|
+
expectTypeOf<z.TypeOf<typeof symbolSchemaOptional>>().toEqualTypeOf<symbol | undefined>();
|
|
169
|
+
expectTypeOf<z.TypeOf<typeof symbolSchemaNullable>>().toEqualTypeOf<symbol | null>();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("get literal values", () => {
|
|
173
|
+
expect(literalStringSchema.values).toEqual(new Set(["asdf"]));
|
|
174
|
+
expect(literalStringSchema._zod.def.values).toEqual(["asdf"]);
|
|
175
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { expect, expectTypeOf, test } from "vitest";
|
|
2
|
+
import * as z from "zod/v4";
|
|
3
|
+
|
|
4
|
+
const promSchema = z.promise(
|
|
5
|
+
z.object({
|
|
6
|
+
name: z.string(),
|
|
7
|
+
age: z.number(),
|
|
8
|
+
})
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
test("promise inference", () => {
|
|
12
|
+
type promSchemaType = z.infer<typeof promSchema>;
|
|
13
|
+
expectTypeOf<promSchemaType>().toEqualTypeOf<{ name: string; age: number }>();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("promise parsing success", async () => {
|
|
17
|
+
// expect(() => promSchema.parse(Promise.resolve({ name: "Bobby", age: 10 }))).toThrow();
|
|
18
|
+
const pr = promSchema.parseAsync(Promise.resolve({ name: "Bobby", age: 10 }));
|
|
19
|
+
expect(pr).toBeInstanceOf(Promise);
|
|
20
|
+
const result = await pr;
|
|
21
|
+
expect(result).toMatchInlineSnapshot(`
|
|
22
|
+
{
|
|
23
|
+
"age": 10,
|
|
24
|
+
"name": "Bobby",
|
|
25
|
+
}
|
|
26
|
+
`);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("promise parsing fail", async () => {
|
|
30
|
+
const bad = await promSchema.safeParseAsync(Promise.resolve({ name: "Bobby", age: "10" }));
|
|
31
|
+
expect(bad.success).toBe(false);
|
|
32
|
+
expect(bad.error).toBeInstanceOf(z.ZodError);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("promise parsing fail 2", async () => {
|
|
36
|
+
const result = await promSchema.safeParseAsync(Promise.resolve({ name: "Bobby", age: "10" }));
|
|
37
|
+
expect(result.success).toBe(false);
|
|
38
|
+
expect(result.error).toBeInstanceOf(z.ZodError);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("promise parsing fail", () => {
|
|
42
|
+
const bad = () => promSchema.parse({ then: () => {}, catch: {} });
|
|
43
|
+
expect(bad).toThrow();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("sync promise parsing", () => {
|
|
47
|
+
expect(() => z.promise(z.string()).parse(Promise.resolve("asfd"))).toThrow();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const asyncFunction = z.function({
|
|
51
|
+
input: z.tuple([]),
|
|
52
|
+
output: promSchema,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("async function pass", async () => {
|
|
56
|
+
const validatedFunction = asyncFunction.implementAsync(async () => {
|
|
57
|
+
return { name: "jimmy", age: 14 };
|
|
58
|
+
});
|
|
59
|
+
await expect(validatedFunction()).resolves.toEqual({
|
|
60
|
+
name: "jimmy",
|
|
61
|
+
age: 14,
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("async function fail", async () => {
|
|
66
|
+
const validatedFunction = asyncFunction.implementAsync(() => {
|
|
67
|
+
return Promise.resolve("asdf" as any);
|
|
68
|
+
});
|
|
69
|
+
await expect(validatedFunction()).rejects.toBeInstanceOf(z.core.$ZodError);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("async promise parsing", () => {
|
|
73
|
+
const res = z.promise(z.number()).parseAsync(Promise.resolve(12));
|
|
74
|
+
expect(res).toBeInstanceOf(Promise);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("resolves", () => {
|
|
78
|
+
const foo = z.literal("foo");
|
|
79
|
+
const res = z.promise(foo);
|
|
80
|
+
expect(res.unwrap()).toEqual(foo);
|
|
81
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
import * as z from "zod/v4";
|
|
3
|
+
|
|
4
|
+
declare module "zod/v4" {
|
|
5
|
+
interface ZodType {
|
|
6
|
+
/** @deprecated */
|
|
7
|
+
_classic(): string;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
test("prototype extension", () => {
|
|
12
|
+
z.ZodType.prototype._classic = function () {
|
|
13
|
+
return "_classic";
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// should pass
|
|
17
|
+
const result = z.string()._classic();
|
|
18
|
+
expect(result).toBe("_classic");
|
|
19
|
+
// expectTypeOf<typeof result>().toEqualTypeOf<string>();
|
|
20
|
+
|
|
21
|
+
// clean up
|
|
22
|
+
z.ZodType.prototype._classic = undefined;
|
|
23
|
+
});
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { expect, expectTypeOf, test } from "vitest";
|
|
2
|
+
import * as z from "zod/v4";
|
|
3
|
+
|
|
4
|
+
enum testEnum {
|
|
5
|
+
A = 0,
|
|
6
|
+
B = 1,
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
test("flat inference", () => {
|
|
10
|
+
const readonlyString = z.string().readonly();
|
|
11
|
+
const readonlyNumber = z.number().readonly();
|
|
12
|
+
const readonlyNaN = z.nan().readonly();
|
|
13
|
+
const readonlyBigInt = z.bigint().readonly();
|
|
14
|
+
const readonlyBoolean = z.boolean().readonly();
|
|
15
|
+
const readonlyDate = z.date().readonly();
|
|
16
|
+
const readonlyUndefined = z.undefined().readonly();
|
|
17
|
+
const readonlyNull = z.null().readonly();
|
|
18
|
+
const readonlyAny = z.any().readonly();
|
|
19
|
+
const readonlyUnknown = z.unknown().readonly();
|
|
20
|
+
const readonlyVoid = z.void().readonly();
|
|
21
|
+
const readonlyStringArray = z.array(z.string()).readonly();
|
|
22
|
+
const readonlyTuple = z.tuple([z.string(), z.number()]).readonly();
|
|
23
|
+
const readonlyMap = z.map(z.string(), z.date()).readonly();
|
|
24
|
+
const readonlySet = z.set(z.string()).readonly();
|
|
25
|
+
const readonlyStringRecord = z.record(z.string(), z.string()).readonly();
|
|
26
|
+
const readonlyNumberRecord = z.record(z.string(), z.number()).readonly();
|
|
27
|
+
const readonlyObject = z.object({ a: z.string(), 1: z.number() }).readonly();
|
|
28
|
+
const readonlyEnum = z.nativeEnum(testEnum).readonly();
|
|
29
|
+
const readonlyPromise = z.promise(z.string()).readonly();
|
|
30
|
+
|
|
31
|
+
expectTypeOf<typeof readonlyString._output>().toEqualTypeOf<string>();
|
|
32
|
+
expectTypeOf<typeof readonlyNumber._output>().toEqualTypeOf<number>();
|
|
33
|
+
expectTypeOf<typeof readonlyNaN._output>().toEqualTypeOf<number>();
|
|
34
|
+
expectTypeOf<typeof readonlyBigInt._output>().toEqualTypeOf<bigint>();
|
|
35
|
+
expectTypeOf<typeof readonlyBoolean._output>().toEqualTypeOf<boolean>();
|
|
36
|
+
expectTypeOf<typeof readonlyDate._output>().toEqualTypeOf<Date>();
|
|
37
|
+
expectTypeOf<typeof readonlyUndefined._output>().toEqualTypeOf<undefined>();
|
|
38
|
+
expectTypeOf<typeof readonlyNull._output>().toEqualTypeOf<null>();
|
|
39
|
+
expectTypeOf<typeof readonlyAny._output>().toEqualTypeOf<any>();
|
|
40
|
+
expectTypeOf<typeof readonlyUnknown._output>().toEqualTypeOf<Readonly<unknown>>();
|
|
41
|
+
expectTypeOf<typeof readonlyVoid._output>().toEqualTypeOf<void>();
|
|
42
|
+
expectTypeOf<typeof readonlyStringArray._output>().toEqualTypeOf<readonly string[]>();
|
|
43
|
+
expectTypeOf<typeof readonlyTuple._output>().toEqualTypeOf<readonly [string, number]>();
|
|
44
|
+
expectTypeOf<typeof readonlyMap._output>().toEqualTypeOf<ReadonlyMap<string, Date>>();
|
|
45
|
+
expectTypeOf<typeof readonlySet._output>().toEqualTypeOf<ReadonlySet<string>>();
|
|
46
|
+
expectTypeOf<typeof readonlyStringRecord._output>().toEqualTypeOf<Readonly<Record<string, string>>>();
|
|
47
|
+
expectTypeOf<typeof readonlyNumberRecord._output>().toEqualTypeOf<Readonly<Record<string, number>>>();
|
|
48
|
+
expectTypeOf<typeof readonlyObject._output>().toEqualTypeOf<{ readonly a: string; readonly 1: number }>();
|
|
49
|
+
expectTypeOf<typeof readonlyEnum._output>().toEqualTypeOf<Readonly<testEnum>>();
|
|
50
|
+
expectTypeOf<typeof readonlyPromise._output>().toEqualTypeOf<string>();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// test("deep inference", () => {
|
|
54
|
+
// expectTypeOf<z.infer<(typeof deepReadonlySchemas_0)[0]>>().toEqualTypeOf<string>();
|
|
55
|
+
// expectTypeOf<z.infer<(typeof deepReadonlySchemas_0)[1]>>().toEqualTypeOf<number>();
|
|
56
|
+
// expectTypeOf<z.infer<(typeof deepReadonlySchemas_0)[2]>>().toEqualTypeOf<number>();
|
|
57
|
+
// expectTypeOf<z.infer<(typeof deepReadonlySchemas_0)[3]>>().toEqualTypeOf<bigint>();
|
|
58
|
+
// expectTypeOf<z.infer<(typeof deepReadonlySchemas_0)[4]>>().toEqualTypeOf<boolean>();
|
|
59
|
+
// expectTypeOf<z.infer<(typeof deepReadonlySchemas_0)[5]>>().toEqualTypeOf<Date>();
|
|
60
|
+
// expectTypeOf<z.infer<(typeof deepReadonlySchemas_0)[6]>>().toEqualTypeOf<undefined>();
|
|
61
|
+
// expectTypeOf<z.infer<(typeof deepReadonlySchemas_0)[7]>>().toEqualTypeOf<null>();
|
|
62
|
+
// expectTypeOf<z.infer<(typeof deepReadonlySchemas_0)[8]>>().toEqualTypeOf<any>();
|
|
63
|
+
// expectTypeOf<
|
|
64
|
+
// z.infer<(typeof deepReadonlySchemas_0)[9]>
|
|
65
|
+
// >().toEqualTypeOf<Readonly<unknown>>();
|
|
66
|
+
// expectTypeOf<z.infer<(typeof deepReadonlySchemas_0)[10]>>().toEqualTypeOf<void>();
|
|
67
|
+
// expectTypeOf<
|
|
68
|
+
// z.infer<(typeof deepReadonlySchemas_0)[11]>
|
|
69
|
+
// >().toEqualTypeOf<(args_0: string, args_1: number, ...args_2: unknown[]) => unknown>();
|
|
70
|
+
// expectTypeOf<
|
|
71
|
+
// z.infer<(typeof deepReadonlySchemas_0)[12]>
|
|
72
|
+
// >().toEqualTypeOf<readonly string[]>();
|
|
73
|
+
// expectTypeOf<
|
|
74
|
+
// z.infer<(typeof deepReadonlySchemas_0)[13]>
|
|
75
|
+
// >().toEqualTypeOf<readonly [string, number]>();
|
|
76
|
+
// expectTypeOf<
|
|
77
|
+
// z.infer<(typeof deepReadonlySchemas_0)[14]>
|
|
78
|
+
// >().toEqualTypeOf<ReadonlyMap<string, Date>>();
|
|
79
|
+
// expectTypeOf<
|
|
80
|
+
// z.infer<(typeof deepReadonlySchemas_0)[15]>
|
|
81
|
+
// >().toEqualTypeOf<ReadonlySet<Promise<string>>>();
|
|
82
|
+
// expectTypeOf<
|
|
83
|
+
// z.infer<(typeof deepReadonlySchemas_0)[16]>
|
|
84
|
+
// >().toEqualTypeOf<Readonly<Record<string, string>>>();
|
|
85
|
+
// expectTypeOf<
|
|
86
|
+
// z.infer<(typeof deepReadonlySchemas_0)[17]>
|
|
87
|
+
// >().toEqualTypeOf<Readonly<Record<string, number>>>();
|
|
88
|
+
// expectTypeOf<
|
|
89
|
+
// z.infer<(typeof deepReadonlySchemas_0)[18]>
|
|
90
|
+
// >().toEqualTypeOf<{ readonly a: string; readonly 1: number }>();
|
|
91
|
+
// expectTypeOf<
|
|
92
|
+
// z.infer<(typeof deepReadonlySchemas_0)[19]>
|
|
93
|
+
// >().toEqualTypeOf<Readonly<testEnum>>();
|
|
94
|
+
// expectTypeOf<
|
|
95
|
+
// z.infer<(typeof deepReadonlySchemas_0)[20]>
|
|
96
|
+
// >().toEqualTypeOf<Promise<string>>();
|
|
97
|
+
|
|
98
|
+
// expectTypeOf<
|
|
99
|
+
// z.infer<typeof crazyDeepReadonlySchema>
|
|
100
|
+
// >().toEqualTypeOf<ReadonlyMap<
|
|
101
|
+
// ReadonlySet<readonly [string, number]>,
|
|
102
|
+
// {
|
|
103
|
+
// readonly a: {
|
|
104
|
+
// readonly [x: string]: readonly any[];
|
|
105
|
+
// };
|
|
106
|
+
// readonly b: {
|
|
107
|
+
// readonly c: {
|
|
108
|
+
// readonly d: {
|
|
109
|
+
// readonly e: {
|
|
110
|
+
// readonly f: {
|
|
111
|
+
// readonly g?: {};
|
|
112
|
+
// };
|
|
113
|
+
// };
|
|
114
|
+
// };
|
|
115
|
+
// };
|
|
116
|
+
// };
|
|
117
|
+
// }
|
|
118
|
+
// >>();
|
|
119
|
+
// });
|
|
120
|
+
|
|
121
|
+
test("object freezing", async () => {
|
|
122
|
+
expect(Object.isFrozen(z.array(z.string()).readonly().parse(["a"]))).toBe(true);
|
|
123
|
+
expect(Object.isFrozen(z.tuple([z.string(), z.number()]).readonly().parse(["a", 1]))).toBe(true);
|
|
124
|
+
expect(
|
|
125
|
+
Object.isFrozen(
|
|
126
|
+
z
|
|
127
|
+
.map(z.string(), z.date())
|
|
128
|
+
.readonly()
|
|
129
|
+
.parse(new Map([["a", new Date()]]))
|
|
130
|
+
)
|
|
131
|
+
).toBe(true);
|
|
132
|
+
|
|
133
|
+
expect(Object.isFrozen(z.record(z.string(), z.string()).readonly().parse({ a: "b" }))).toBe(true);
|
|
134
|
+
expect(Object.isFrozen(z.record(z.string(), z.number()).readonly().parse({ a: 1 }))).toBe(true);
|
|
135
|
+
expect(Object.isFrozen(z.object({ a: z.string(), 1: z.number() }).readonly().parse({ a: "b", 1: 2 }))).toBe(true);
|
|
136
|
+
expect(
|
|
137
|
+
Object.isFrozen(
|
|
138
|
+
await z
|
|
139
|
+
.set(z.promise(z.string()))
|
|
140
|
+
.readonly()
|
|
141
|
+
.parseAsync(new Set([Promise.resolve("a")]))
|
|
142
|
+
)
|
|
143
|
+
).toBe(true);
|
|
144
|
+
expect(Object.isFrozen(await z.promise(z.string()).readonly().parseAsync(Promise.resolve("a")))).toBe(true);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test("async object freezing", async () => {
|
|
148
|
+
expect(Object.isFrozen(await z.array(z.string()).readonly().parseAsync(["a"]))).toBe(true);
|
|
149
|
+
expect(Object.isFrozen(await z.tuple([z.string(), z.number()]).readonly().parseAsync(["a", 1]))).toBe(true);
|
|
150
|
+
expect(
|
|
151
|
+
Object.isFrozen(
|
|
152
|
+
await z
|
|
153
|
+
.map(z.string(), z.date())
|
|
154
|
+
.readonly()
|
|
155
|
+
.parseAsync(new Map([["a", new Date()]]))
|
|
156
|
+
)
|
|
157
|
+
).toBe(true);
|
|
158
|
+
expect(
|
|
159
|
+
Object.isFrozen(
|
|
160
|
+
await z
|
|
161
|
+
.set(z.promise(z.string()))
|
|
162
|
+
.readonly()
|
|
163
|
+
.parseAsync(new Set([Promise.resolve("a")]))
|
|
164
|
+
)
|
|
165
|
+
).toBe(true);
|
|
166
|
+
expect(Object.isFrozen(await z.record(z.string(), z.string()).readonly().parseAsync({ a: "b" }))).toBe(true);
|
|
167
|
+
expect(Object.isFrozen(await z.record(z.string(), z.number()).readonly().parseAsync({ a: 1 }))).toBe(true);
|
|
168
|
+
expect(
|
|
169
|
+
Object.isFrozen(await z.object({ a: z.string(), 1: z.number() }).readonly().parseAsync({ a: "b", 1: 2 }))
|
|
170
|
+
).toBe(true);
|
|
171
|
+
expect(Object.isFrozen(await z.promise(z.string()).readonly().parseAsync(Promise.resolve("a")))).toBe(true);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("readonly inference", () => {
|
|
175
|
+
const readonlyStringArray = z.string().array().readonly();
|
|
176
|
+
const readonlyStringTuple = z.tuple([z.string()]).readonly();
|
|
177
|
+
const deepReadonly = z.object({ a: z.string() }).readonly();
|
|
178
|
+
|
|
179
|
+
type readonlyStringArray = z.infer<typeof readonlyStringArray>;
|
|
180
|
+
type readonlyStringTuple = z.infer<typeof readonlyStringTuple>;
|
|
181
|
+
type deepReadonly = z.infer<typeof deepReadonly>;
|
|
182
|
+
|
|
183
|
+
expectTypeOf<readonlyStringArray>().toEqualTypeOf<readonly string[]>();
|
|
184
|
+
expectTypeOf<readonlyStringTuple>().toEqualTypeOf<readonly [string]>();
|
|
185
|
+
expectTypeOf<deepReadonly>().toEqualTypeOf<{ readonly a: string }>();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test("readonly parse", () => {
|
|
189
|
+
const schema = z.array(z.string()).readonly();
|
|
190
|
+
const readonlyArray = ["a", "b", "c"] as const;
|
|
191
|
+
const mutableArray = ["a", "b", "c"];
|
|
192
|
+
const result1 = schema.parse(readonlyArray);
|
|
193
|
+
const result2 = schema.parse(mutableArray);
|
|
194
|
+
expect(result1).toEqual(readonlyArray);
|
|
195
|
+
expect(result2).toEqual(mutableArray);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test("readonly parse with tuples", () => {
|
|
199
|
+
const schema = z.tuple([z.string(), z.number()]).readonly();
|
|
200
|
+
schema.parse(["a", 1]);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test("readonly and the get method", () => {
|
|
204
|
+
const readonlyString = z.string().readonly();
|
|
205
|
+
const readonlyNumber1 = z.number().readonly();
|
|
206
|
+
const readonlyNumber2 = z.number().readonly();
|
|
207
|
+
const readonlyBigInt = z.bigint().readonly();
|
|
208
|
+
const readonlyBoolean = z.boolean().readonly();
|
|
209
|
+
const readonlyDate = z.date().readonly();
|
|
210
|
+
const readonlyUndefined = z.undefined().readonly();
|
|
211
|
+
const readonlyNull = z.null().readonly();
|
|
212
|
+
const readonlyAny = z.any().readonly();
|
|
213
|
+
const readonlyUnknown = z.unknown().readonly();
|
|
214
|
+
const readonlyVoid = z.void().readonly();
|
|
215
|
+
// const readonlyFunction = z.function(z.tuple([z.string(), z.number()]), z.unknown()).readonly();
|
|
216
|
+
const readonlyStringArray = z.string().array().readonly();
|
|
217
|
+
const readonlyTuple = z.tuple([z.string(), z.number()]).readonly();
|
|
218
|
+
|
|
219
|
+
expectTypeOf<z.infer<typeof readonlyString>>().toEqualTypeOf<string>();
|
|
220
|
+
expectTypeOf<z.infer<typeof readonlyNumber1>>().toEqualTypeOf<number>();
|
|
221
|
+
expectTypeOf<z.infer<typeof readonlyNumber2>>().toEqualTypeOf<number>();
|
|
222
|
+
expectTypeOf<z.infer<typeof readonlyBigInt>>().toEqualTypeOf<bigint>();
|
|
223
|
+
expectTypeOf<z.infer<typeof readonlyBoolean>>().toEqualTypeOf<boolean>();
|
|
224
|
+
expectTypeOf<z.infer<typeof readonlyDate>>().toEqualTypeOf<Date>();
|
|
225
|
+
expectTypeOf<z.infer<typeof readonlyUndefined>>().toEqualTypeOf<undefined>();
|
|
226
|
+
expectTypeOf<z.infer<typeof readonlyNull>>().toEqualTypeOf<null>();
|
|
227
|
+
expectTypeOf<z.infer<typeof readonlyAny>>().toEqualTypeOf<any>();
|
|
228
|
+
expectTypeOf<z.infer<typeof readonlyUnknown>>().toEqualTypeOf<Readonly<unknown>>();
|
|
229
|
+
expectTypeOf<z.infer<typeof readonlyVoid>>().toEqualTypeOf<void>();
|
|
230
|
+
// expectTypeOf<z.infer<typeof readonlyFunction>>().toEqualTypeOf<
|
|
231
|
+
// (args_0: string, args_1: number, ...args_2: unknown[]) => unknown
|
|
232
|
+
// >();
|
|
233
|
+
expectTypeOf<z.infer<typeof readonlyStringArray>>().toEqualTypeOf<readonly string[]>();
|
|
234
|
+
expectTypeOf<z.infer<typeof readonlyTuple>>().toEqualTypeOf<readonly [string, number]>();
|
|
235
|
+
|
|
236
|
+
expect(readonlyString.parse("asdf")).toEqual("asdf");
|
|
237
|
+
expect(readonlyNumber1.parse(1234)).toEqual(1234);
|
|
238
|
+
expect(readonlyNumber2.parse(1234)).toEqual(1234);
|
|
239
|
+
const bigIntVal = BigInt(1);
|
|
240
|
+
expect(readonlyBigInt.parse(bigIntVal)).toEqual(bigIntVal);
|
|
241
|
+
expect(readonlyBoolean.parse(true)).toEqual(true);
|
|
242
|
+
const dateVal = new Date();
|
|
243
|
+
expect(readonlyDate.parse(dateVal)).toEqual(dateVal);
|
|
244
|
+
expect(readonlyUndefined.parse(undefined)).toEqual(undefined);
|
|
245
|
+
expect(readonlyNull.parse(null)).toEqual(null);
|
|
246
|
+
expect(readonlyAny.parse("whatever")).toEqual("whatever");
|
|
247
|
+
expect(readonlyUnknown.parse("whatever")).toEqual("whatever");
|
|
248
|
+
expect(readonlyVoid.parse(undefined)).toEqual(undefined);
|
|
249
|
+
// expect(readonlyFunction.parse(() => void 0)).toEqual(() => void 0);
|
|
250
|
+
expect(readonlyStringArray.parse(["asdf"])).toEqual(["asdf"]);
|
|
251
|
+
expect(readonlyTuple.parse(["asdf", 1234])).toEqual(["asdf", 1234]);
|
|
252
|
+
});
|