-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecord.ts
57 lines (49 loc) · 1.34 KB
/
record.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
import type * as zod from "zod";
import { ZodAccelerator } from "../accelerator";
import { ZodObjectAccelerator } from "./object";
import type { ZodAcceleratorContent } from "../content";
@ZodAccelerator.autoInstance
export class ZodRecordAccelerator extends ZodAccelerator {
public get support() {
return ZodAccelerator.zod.ZodRecord;
}
public makeAcceleratorContent(zodSchema: zod.ZodRecord<zod.KeySchema>, zac: ZodAcceleratorContent) {
const def = zodSchema._def;
const zacValueType = ZodAccelerator.findAcceleratorContent(def.valueType as zod.ZodType);
let keyType = def.keyType;
if (keyType instanceof ZodAccelerator.zod.ZodString) {
keyType = new ZodAccelerator.zod.ZodString({
...keyType._def,
coerce: true,
});
}
const zacKeyType = ZodAccelerator.findAcceleratorContent(keyType as zod.ZodType);
zac.addContent(
"let $output = {};",
ZodRecordAccelerator.contentPart.typeof(),
"for(let $id_key in $input){",
[
zacKeyType,
{
path: "[Record Key \"${$id_key}\"]",
input: "$id_key",
output: "$id_key",
},
],
[
zacValueType,
{
path: "[Record Value \"${$id_key}\"]",
input: "$input[$id_key]",
output: "$output[$id_key]",
},
],
"}",
"$input = $output",
);
return zac;
}
public static contentPart = {
...ZodObjectAccelerator.contentPart,
};
}