-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecord.ts
54 lines (47 loc) · 1.27 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
import * as zod from "zod";
import {ZodAccelerator} from "../accelerator";
import {ZodObjectAccelerator} from "./object";
import {ZodAcceleratorContent} from "../content";
@ZodAccelerator.autoInstance
export class ZodRecordAccelerator extends ZodAccelerator{
public get support(){
return ZodAccelerator.zod.ZodRecord;
}
public makeAcceleratorContent(zodSchema: zod.ZodRecord, zac: ZodAcceleratorContent){
const def = zodSchema._def;
const zacValueType = ZodAccelerator.findAcceleratorContent(def.valueType);
const keyType = def.keyType;
if(keyType instanceof zod.ZodNumber || keyType instanceof zod.ZodString){
//@ts-ignore
keyType._def.coerce = true;
}
const zacKeyType = ZodAccelerator.findAcceleratorContent(def.keyType);
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;
}
static contentPart = {
...ZodObjectAccelerator.contentPart
};
}