-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypeguards.ts
59 lines (53 loc) · 1.46 KB
/
typeguards.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { z } from "zod";
export function isZodEffects<T extends z.ZodTypeAny>(
value: z.ZodTypeAny
): value is z.ZodEffects<T> {
return (
value &&
(value._def.typeName === "ZodEffects" ||
isZodEffects(value._def.innerType) ||
isZodEffects(value._def.schema))
);
}
export function isString(value: z.ZodTypeAny): value is z.ZodString {
return (
value &&
(value._def.typeName === z.string()._def.typeName ||
isString(value._def.innerType) ||
isString(value._def.schema))
);
}
export function isNumber(value: z.ZodTypeAny): value is z.ZodNumber {
return (
value &&
(value._def.typeName === z.number()._def.typeName ||
isNumber(value._def.innerType) ||
isNumber(value._def.schema))
);
}
export function isBoolean(value: z.ZodTypeAny): value is z.ZodBoolean {
return (
value &&
(value._def.typeName === z.boolean()._def.typeName ||
isBoolean(value._def.innerType) ||
isBoolean(value._def.schema))
);
}
export function isEnum<T extends [string, ...string[]]>(
value: z.ZodTypeAny
): value is z.ZodEnum<T> {
return (
value &&
(value._def.typeName === z.enum([""])._def.typeName ||
isEnum(value._def.innerType) ||
isEnum(value._def.schema))
);
}
export function isDate(value: z.ZodTypeAny): value is z.ZodDate {
return (
value &&
(value._def.typeName === z.date()._def.typeName ||
isDate(value._def.innerType) ||
isDate(value._def.schema))
);
}