-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharray.ts
55 lines (50 loc) · 1.58 KB
/
array.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
import * as zod from "zod";
import {ZodAccelerator} from "../accelerator";
import {ZodAcceleratorContent} from "../content";
@ZodAccelerator.autoInstance
export class ZodArrayAccelerator extends ZodAccelerator{
public get support(){
return ZodAccelerator.zod.ZodArray;
}
public makeAcceleratorContent(zodSchema: zod.ZodArray<any>, zac: ZodAcceleratorContent){
const def = zodSchema._def;
const zacType = ZodAccelerator.findAcceleratorContent(def.type);
zac.addContent(
"let $output = [];",
ZodArrayAccelerator.contentPart.typeof(),
def.minLength ? ZodArrayAccelerator.contentPart.min(def.minLength) : null,
def.maxLength ? ZodArrayAccelerator.contentPart.max(def.maxLength) : null,
def.exactLength ? ZodArrayAccelerator.contentPart.exact(def.exactLength) : null,
"for(let index = 0; index < $input.length; index++){",
[
zacType,
{
path: "[Array ${index}]",
input: "$input[index]",
output: "$output[index]",
}
],
"}",
"$input = $output"
);
return zac;
}
static contentPart = {
typeof: () => ({
if: /* js */"!($input instanceof Array)",
message: "Input is not Array.",
}),
min: ({value}: {value: number}) => ({
if: /* js */`$input.length < ${value}`,
message: `Input Array has length less than ${value}.`,
}),
max: ({value}: {value: number}) => ({
if: /* js */`$input.length > ${value}`,
message: `Input Array has length more than ${value}.`,
}),
exact: ({value}: {value: number}) => ({
if: /* js */`$input.length !== ${value}`,
message: `Input Array has length not equal to ${value}.`,
}),
};
}