-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.ts
297 lines (265 loc) · 7.83 KB
/
file.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import {
ZodType,
ZodIssueCode,
ZodTypeDef,
ParseInput,
ParseReturnType,
ZodErrorMap,
ZodFirstPartyTypeKind,
ZodIssue,
IssueData,
INVALID
} from "zod"
import { errorUtil } from "./helpers/errorUtil"
import { RawCreateParams } from "../file"
enum MyTypes {
ZodFile = "ZodFile"
}
const Types = { ...ZodFirstPartyTypeKind, ...MyTypes }
type ITypes = ZodFirstPartyTypeKind | MyTypes
type ProcessedCreateParams = { errorMap?: ZodErrorMap; description?: string }
export const makeIssue = (params: {
// eslint-disable-next-line
data: any
path: (string | number)[]
errorMaps: ZodErrorMap[]
issueData: IssueData
}): ZodIssue => {
const { data, path, errorMaps, issueData } = params
const fullPath = [...path, ...(issueData.path || [])]
const fullIssue = {
...issueData,
path: fullPath
}
let errorMessage = ""
const maps = errorMaps
.filter((m) => !!m)
.slice()
.reverse() as ZodErrorMap[]
for (const map of maps) {
errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message
}
return {
...issueData,
path: fullPath,
message: issueData.message || errorMessage
}
}
function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
if (!params) return {}
const { errorMap, invalid_type_error, required_error, description } = params
if (errorMap && (invalid_type_error || required_error)) {
throw new Error(
"Can't use 'invalid' or 'required' in conjunction with custom error map."
)
}
if (errorMap) return { errorMap: errorMap, description }
const customMap: ZodErrorMap = (iss, ctx) => {
if (iss.code !== "invalid_type") return { message: ctx.defaultError }
if (typeof ctx.data === "undefined" && required_error)
return { message: required_error }
if (params.invalid_type_error) return { message: params.invalid_type_error }
return { message: ctx.defaultError }
}
return { errorMap: customMap, description }
}
/////////////////////////////////////////
/////////////////////////////////////////
////////// //////////
////////// ZodFile //////////
////////// //////////
/////////////////////////////////////////
/////////////////////////////////////////
type ZodFileCheck =
| { kind: "max"; value: number; message?: string }
| { kind: "min"; value: number; message?: string }
| { kind: "type"; value: string | string[]; message?: string }
export interface ZodFileDef extends ZodTypeDef {
typeName: ITypes
checks: ZodFileCheck[]
}
export interface IFile {
get lastModified(): number
get lastModifiedDate(): Date
get name(): string
get size(): number
get type(): string
}
export class ZodFile extends ZodType<IFile, ZodFileDef> {
_parse(input: ParseInput): ParseReturnType<this["_output"]> {
const { status, ctx } = this._processInputParams(input)
const hasSize = !!input.data?.size
const hasType = !!input.data?.type
if (!hasSize || !hasType) {
const issue = makeIssue({
issueData: {
code: ZodIssueCode.invalid_type,
// eslint-disable-next-line
expected: "file" as any,
received: ctx.parsedType
},
data: ctx.data,
path: ctx.path,
errorMaps: [ctx.schemaErrorMap, ctx.common.contextualErrorMap].filter(
(x) => !!x
) as ZodErrorMap[]
})
// eslint-disable-next-line
// @ts-disable
ctx.common.issues.push(issue as ZodIssue)
return INVALID
}
for (const check of this._def.checks) {
if (check.kind === "max") {
if (ctx.data.size > check.value) {
const issue = makeIssue({
issueData: {
code: ZodIssueCode.too_big,
maximum: check.value,
// eslint-disable-next-line
type: "file" as any,
inclusive: true,
message: check.message
},
data: ctx.data,
path: ctx.path,
errorMaps: [
ctx.schemaErrorMap,
ctx.common.contextualErrorMap
].filter((x) => !!x) as ZodErrorMap[]
})
// eslint-disable-next-line
// @ts-disable
ctx.common.issues.push(issue as ZodIssue)
status.dirty()
}
} else if (check.kind === "min") {
if (ctx.data.size < check.value) {
const issue = makeIssue({
issueData: {
code: ZodIssueCode.too_small,
minimum: check.value,
// eslint-disable-next-line
type: "file" as any,
inclusive: true,
message: check.message
},
data: ctx.data,
path: ctx.path,
errorMaps: [
ctx.schemaErrorMap,
ctx.common.contextualErrorMap
].filter((x) => !!x) as ZodErrorMap[]
})
// eslint-disable-next-line
// @ts-disable
ctx.common.issues.push(issue as ZodIssue)
status.dirty()
}
} else if (check.kind === "type") {
const types = Array.isArray(check.value) ? check.value : [check.value]
if (!types.includes(ctx.data.type)) {
const issue = makeIssue({
issueData: {
// eslint-disable-next-line
code: "invalid_file_type" as any,
// eslint-disable-next-line
expected: types as any,
received: ctx.data.type,
path: ctx.path,
message: check.message
},
data: ctx.data,
path: ctx.path,
errorMaps: [
ctx.schemaErrorMap,
ctx.common.contextualErrorMap
].filter((x) => !!x) as ZodErrorMap[]
})
// eslint-disable-next-line
// @ts-disable
ctx.common.issues.push(issue as ZodIssue)
status.dirty()
}
}
}
return {
status: status.value,
value: ctx.data
}
}
_addCheck(check: ZodFileCheck) {
return new ZodFile({
...this._def,
checks: [...this._def.checks, check]
})
}
max(maxSize: number, message?: errorUtil.ErrMessage) {
return this._addCheck({
kind: "max",
value: maxSize,
...errorUtil.errToObj(message)
})
}
min(minSize: number, message?: errorUtil.ErrMessage) {
return this._addCheck({
kind: "min",
value: minSize,
...errorUtil.errToObj(message)
})
}
types(accept: string[], message?: errorUtil.ErrMessage) {
return new ZodFile({
...this._def,
// filter out pre-existing `type` checks
checks: [
...this._def.checks.filter((ch) => ch.kind !== "type"),
{
kind: "type",
value: accept,
...errorUtil.errToObj(message)
}
]
})
}
get minSize() {
let min: number | null = null
for (const ch of this._def.checks) {
if (ch.kind === "min") {
if (min === null || ch.value > min) min = ch.value
}
}
return min
}
get maxSize() {
let max: number | null = null
for (const ch of this._def.checks) {
if (ch.kind === "max") {
if (max === null || ch.value < max) max = ch.value
}
}
return max
}
get allowedTypes() {
const types: string[] = []
for (const ch of this._def.checks) {
if (ch.kind === "type") {
const checkTypes = Array.isArray(ch.value) ? ch.value : [ch.value]
checkTypes.forEach(
(value) => !types.find((type) => type === value) && types.push(value)
)
}
}
return types
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static create = (params?: RawCreateParams): ZodFile => {
return new ZodFile({
typeName: Types.ZodFile,
checks: [],
...processCreateParams(params)
})
}
}
const fileType = ZodFile.create
export { fileType as file }