-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
162 lines (151 loc) · 3.76 KB
/
index.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
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
import { GraphQLScalarType } from "graphql";
import type { ZodError, ZodIssue } from "zod";
export const zodTypeDef = `
scalar StringNumber
scalar ZodParsedType
scalar ZodIssueCode
enum ZodValidation {
url
email
uuid
}
enum ZodType {
string
number
array
}
type ZodUnionIssue {
code: ZodIssueCode!
expected: ZodParsedType
received: ZodParsedType
path: [StringNumber]
message: String!
}
type ZodIssue {
code: ZodIssueCode!
path: [StringNumber]
message: String!
expected: ZodParsedType
received: String
keys: [String]
unionIssues: [[ZodUnionIssue]]
options: [String]
argumentsError: ZodIssue
returnTypeError: ZodIssue
validation: ZodValidation
type: ZodType
minimum: Int
maximum: Int
inclusive: Boolean
multipleOf: Int
}
`;
export const zodParsedTypes: Set<string> = new Set([
"string",
"nan",
"number",
"integer",
"float",
"boolean",
"date",
"bigint",
"symbol",
"function",
"undefined",
"null",
"array",
"object",
"unknown",
"promise",
"void",
"never",
"map",
"set",
]);
const ZodParsedType = new GraphQLScalarType({
name: "ZodParsedType",
description:
"This is an enum used by Zod internally to represent the type of a parsed value.",
serialize: zodParsedType,
parseValue: zodParsedType,
parseLiteral: zodParsedType,
});
const StringNumber = new GraphQLScalarType({
name: "StringNumber",
description: "string | number",
serialize: stringNumber,
parseValue: stringNumber,
parseLiteral: stringNumber,
});
export const issueCodes: Set<string> = new Set([
"invalid_type",
"unrecognized_keys",
"invalid_union",
"invalid_enum_value",
"invalid_arguments",
"invalid_return_type",
"invalid_date",
"invalid_string",
"too_small",
"too_big",
"not_multiple_of",
"custom",
]);
const ZodIssueCode = new GraphQLScalarType({
name: "ZodIssueCode",
description: "const enum for zod issue codes",
serialize: validateZodIssueCode,
parseValue: validateZodIssueCode,
parseLiteral: validateZodIssueCode,
});
export function validateZodIssueCode(value: unknown) {
if (typeof value !== "string") {
throw new Error("Provided value must be a string.");
}
if (!issueCodes.has(value)) {
throw new Error(
`Provided value is not one of ${Array.from(issueCodes).join(", ")}`
);
}
return value;
}
export function zodParsedType(value: unknown) {
if (typeof value !== "string") {
throw new Error("Provided value must be a string.");
}
if (!zodParsedTypes.has(value)) {
throw new Error(
`Provided value is not one of ${Array.from(zodParsedTypes).join(", ")}`
);
}
return value;
}
export function stringNumber(value: unknown) {
if (typeof value === "string" || typeof value === "number") {
return value;
} else {
throw new Error("Provided value is not a string or a number.");
}
}
type ZodIssueWithUnionErrors = ZodIssue & { unionErrors: ZodError[] };
type ZodIssuesWithUnionIssues = ZodIssue & {
unionIssues?: ZodIssue[][];
unionErrors?: ZodError[];
};
export function zodErrorsTozodIssues(zodError: ZodError) {
const zodIssues = zodError.errors as ZodIssueWithUnionErrors[];
return zodIssues.map((issue) => {
// If the issue has unionErrors format them so we don't return Error class instances to GraphQL
if (Object.prototype.hasOwnProperty.call(issue, "unionErrors")) {
const formattedObject: ZodIssuesWithUnionIssues = { ...issue };
formattedObject.unionIssues = issue.unionErrors.map((unionError) => {
return unionError.errors;
});
delete formattedObject.unionErrors;
return formattedObject;
} else {
return issue;
}
});
}
export const zodErrorScalars = { StringNumber, ZodParsedType, ZodIssueCode };