deno.land / x / zod@v3.19.1 / __tests__ / instanceof.test.ts

instanceof.test.ts
نووسراو ببینە
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
// @ts-ignore TS6133import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts";const test = Deno.test;
import { util } from "../helpers/util.ts";import * as z from "../index.ts";
test("instanceof", async () => { class Test {} class Subtest extends Test {} abstract class AbstractBar { constructor(public val: string) {} } class Bar extends AbstractBar {}
const TestSchema = z.instanceof(Test); const SubtestSchema = z.instanceof(Subtest); const BarSchema = z.instanceof(Bar);
TestSchema.parse(new Test()); TestSchema.parse(new Subtest()); SubtestSchema.parse(new Subtest()); const bar = BarSchema.parse(new Bar("asdf")); expect(bar.val).toEqual("asdf");
await expect(() => SubtestSchema.parse(new Test())).toThrow( /Input not instance of Subtest/ ); await expect(() => TestSchema.parse(12)).toThrow( /Input not instance of Test/ );
util.assertEqual<Test, z.infer<typeof TestSchema>>(true);});
test("instanceof fatal", () => { const schema = z.instanceof(Date).refine((d) => d.toString()); const res = schema.safeParse(null); expect(res.success).toBe(false);});
zod

Version Info

Tagged at
a year ago