File size: 7,649 Bytes
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { expect, expectTypeOf, test } from "vitest";
import * as z from "zod/v4";

const literalStringSchema = z.literal("asdf");
const literalNumberSchema = z.literal(12);
const literalBooleanSchema = z.literal(true);
const literalBigIntSchema = z.literal(BigInt(42));

const stringSchema = z.string();
const numberSchema = z.number();
const bigintSchema = z.bigint();
const booleanSchema = z.boolean();
const dateSchema = z.date();
const symbolSchema = z.symbol();
const nullSchema = z.null();
const undefinedSchema = z.undefined();
const stringSchemaOptional = z.string().optional();
const stringSchemaNullable = z.string().nullable();
const numberSchemaOptional = z.number().optional();
const numberSchemaNullable = z.number().nullable();
const bigintSchemaOptional = z.bigint().optional();
const bigintSchemaNullable = z.bigint().nullable();
const booleanSchemaOptional = z.boolean().optional();
const booleanSchemaNullable = z.boolean().nullable();
const dateSchemaOptional = z.date().optional();
const dateSchemaNullable = z.date().nullable();
const symbolSchemaOptional = z.symbol().optional();
const symbolSchemaNullable = z.symbol().nullable();

test("literal string schema", () => {
  expect(literalStringSchema.parse("asdf")).toBe("asdf");
  expect(() => literalStringSchema.parse("not_asdf")).toThrow();
  expect(() => literalStringSchema.parse(123)).toThrow();
  expect(() => literalStringSchema.parse(true)).toThrow();
  expect(() => literalStringSchema.parse({})).toThrow();
});

test("literal number schema", () => {
  expect(literalNumberSchema.parse(12)).toBe(12);
  expect(() => literalNumberSchema.parse(13)).toThrow();
  expect(() => literalNumberSchema.parse("foo")).toThrow();
  expect(() => literalNumberSchema.parse(true)).toThrow();
  expect(() => literalNumberSchema.parse({})).toThrow();
});

test("literal boolean schema", () => {
  expect(literalBooleanSchema.parse(true)).toBe(true);
  expect(() => literalBooleanSchema.parse(false)).toThrow();
  expect(() => literalBooleanSchema.parse("asdf")).toThrow();
  expect(() => literalBooleanSchema.parse(123)).toThrow();
  expect(() => literalBooleanSchema.parse({})).toThrow();
});

test("literal bigint schema", () => {
  expect(literalBigIntSchema.parse(BigInt(42))).toBe(BigInt(42));
  expect(() => literalBigIntSchema.parse(BigInt(43))).toThrow();
  expect(() => literalBigIntSchema.parse("asdf")).toThrow();
  expect(() => literalBigIntSchema.parse(123)).toThrow();
  expect(() => literalBigIntSchema.parse({})).toThrow();
});

test("string schema", () => {
  stringSchema.parse("foo");
  expect(() => stringSchema.parse(Math.random())).toThrow();
  expect(() => stringSchema.parse(true)).toThrow();
  expect(() => stringSchema.parse(undefined)).toThrow();
  expect(() => stringSchema.parse(null)).toThrow();
});

test("number schema", () => {
  numberSchema.parse(Math.random());
  expect(() => numberSchema.parse("foo")).toThrow();
  expect(() => numberSchema.parse(BigInt(17))).toThrow();
  expect(() => numberSchema.parse(true)).toThrow();
  expect(() => numberSchema.parse(undefined)).toThrow();
  expect(() => numberSchema.parse(null)).toThrow();
});

test("bigint schema", () => {
  bigintSchema.parse(BigInt(17));
  expect(() => bigintSchema.parse("foo")).toThrow();
  expect(() => bigintSchema.parse(Math.random())).toThrow();
  expect(() => bigintSchema.parse(true)).toThrow();
  expect(() => bigintSchema.parse(undefined)).toThrow();
  expect(() => bigintSchema.parse(null)).toThrow();
});

test("boolean schema", () => {
  booleanSchema.parse(true);
  expect(() => booleanSchema.parse("foo")).toThrow();
  expect(() => booleanSchema.parse(Math.random())).toThrow();
  expect(() => booleanSchema.parse(undefined)).toThrow();
  expect(() => booleanSchema.parse(null)).toThrow();
});

test("date schema", async () => {
  dateSchema.parse(new Date());
  expect(() => dateSchema.parse("foo")).toThrow();
  expect(() => dateSchema.parse(Math.random())).toThrow();
  expect(() => dateSchema.parse(true)).toThrow();
  expect(() => dateSchema.parse(undefined)).toThrow();
  expect(() => dateSchema.parse(null)).toThrow();
  expect(await dateSchema.safeParseAsync(new Date("invalid"))).toMatchInlineSnapshot(`
    {
      "error": [ZodError: [
      {
        "expected": "date",
        "code": "invalid_type",
        "received": "Invalid Date",
        "path": [],
        "message": "Invalid input: expected date, received Date"
      }
    ]],
      "success": false,
    }
  `);
});

test("symbol schema", () => {
  symbolSchema.parse(Symbol("foo"));
  expect(() => symbolSchema.parse("foo")).toThrow();
  expect(() => symbolSchema.parse(Math.random())).toThrow();
  expect(() => symbolSchema.parse(true)).toThrow();
  expect(() => symbolSchema.parse(new Date())).toThrow();
  expect(() => symbolSchema.parse(undefined)).toThrow();
  expect(() => symbolSchema.parse(null)).toThrow();
});

test("undefined schema", () => {
  undefinedSchema.parse(undefined);
  expect(() => undefinedSchema.parse("foo")).toThrow();
  expect(() => undefinedSchema.parse(Math.random())).toThrow();
  expect(() => undefinedSchema.parse(true)).toThrow();
  expect(() => undefinedSchema.parse(null)).toThrow();
});

test("null schema", () => {
  nullSchema.parse(null);
  expect(() => nullSchema.parse("foo")).toThrow();
  expect(() => nullSchema.parse(Math.random())).toThrow();
  expect(() => nullSchema.parse(true)).toThrow();
  expect(() => nullSchema.parse(undefined)).toThrow();
});

test("primitive inference", () => {
  expectTypeOf<z.TypeOf<typeof literalStringSchema>>().toEqualTypeOf<"asdf">();
  expectTypeOf<z.TypeOf<typeof literalNumberSchema>>().toEqualTypeOf<12>();
  expectTypeOf<z.TypeOf<typeof literalBooleanSchema>>().toEqualTypeOf<true>();
  expectTypeOf<z.TypeOf<typeof literalBigIntSchema>>().toEqualTypeOf<bigint>();
  expectTypeOf<z.TypeOf<typeof stringSchema>>().toEqualTypeOf<string>();
  expectTypeOf<z.TypeOf<typeof numberSchema>>().toEqualTypeOf<number>();
  expectTypeOf<z.TypeOf<typeof bigintSchema>>().toEqualTypeOf<bigint>();
  expectTypeOf<z.TypeOf<typeof booleanSchema>>().toEqualTypeOf<boolean>();
  expectTypeOf<z.TypeOf<typeof dateSchema>>().toEqualTypeOf<Date>();
  expectTypeOf<z.TypeOf<typeof symbolSchema>>().toEqualTypeOf<symbol>();
  expectTypeOf<z.TypeOf<typeof nullSchema>>().toEqualTypeOf<null>();
  expectTypeOf<z.TypeOf<typeof undefinedSchema>>().toEqualTypeOf<undefined>();
  expectTypeOf<z.TypeOf<typeof stringSchemaOptional>>().toEqualTypeOf<string | undefined>();
  expectTypeOf<z.TypeOf<typeof stringSchemaNullable>>().toEqualTypeOf<string | null>();
  expectTypeOf<z.TypeOf<typeof numberSchemaOptional>>().toEqualTypeOf<number | undefined>();
  expectTypeOf<z.TypeOf<typeof numberSchemaNullable>>().toEqualTypeOf<number | null>();
  expectTypeOf<z.TypeOf<typeof bigintSchemaOptional>>().toEqualTypeOf<bigint | undefined>();
  expectTypeOf<z.TypeOf<typeof bigintSchemaNullable>>().toEqualTypeOf<bigint | null>();
  expectTypeOf<z.TypeOf<typeof booleanSchemaOptional>>().toEqualTypeOf<boolean | undefined>();
  expectTypeOf<z.TypeOf<typeof booleanSchemaNullable>>().toEqualTypeOf<boolean | null>();
  expectTypeOf<z.TypeOf<typeof dateSchemaOptional>>().toEqualTypeOf<Date | undefined>();
  expectTypeOf<z.TypeOf<typeof dateSchemaNullable>>().toEqualTypeOf<Date | null>();
  expectTypeOf<z.TypeOf<typeof symbolSchemaOptional>>().toEqualTypeOf<symbol | undefined>();
  expectTypeOf<z.TypeOf<typeof symbolSchemaNullable>>().toEqualTypeOf<symbol | null>();
});

test("get literal values", () => {
  expect(literalStringSchema.values).toEqual(new Set(["asdf"]));
  expect(literalStringSchema._zod.def.values).toEqual(["asdf"]);
});