-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathintersection.ts
132 lines (117 loc) · 3.14 KB
/
intersection.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
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import * as zod from "zod";
import { ZodAccelerator } from "../accelerator";
import type { ZodAcceleratorContent } from "../content";
import { ZodAcceleratorError } from "../error";
import type { AcceleratorSafeParseError } from "../parser";
@ZodAccelerator.autoInstance
export class ZodIntersectionAccelerator extends ZodAccelerator {
public get support() {
return ZodAccelerator.zod.ZodIntersection;
}
public makeAcceleratorContent(
zodSchema: zod.ZodIntersection<zod.ZodType, zod.ZodType>,
zac: ZodAcceleratorContent,
) {
const def = zodSchema._def;
zac.addContext({
duploj$mergeValues: ZodIntersectionAccelerator.mergeValues,
});
zac.addContent(
/* js */`
let $id_left_output;
let $id_right_output;
`,
);
(["left", "right"] as const).forEach((value) => {
zac.addContent(
[
ZodAccelerator.findAcceleratorContent(def[value]),
{
path: null,
input: "$input",
output: `$id_${value}_output`,
},
],
);
});
zac.addContent(ZodIntersectionAccelerator.contentPart.mergeValues());
return zac;
}
public static contentPart = {
mergeValues: () => `
let $id_resultMergeValues = this.duploj$mergeValues($id_left_output, $id_right_output, \`$path\`);
if(!$id_resultMergeValues.success){
return $id_resultMergeValues;
}
$input = $id_resultMergeValues.data;
`,
};
public static mergeValues(
aa: any,
bb: any,
path: string,
): AcceleratorSafeParseError<any> {
const aType = zod.getParsedType(aa);
const bType = zod.getParsedType(bb);
if (aa === bb) {
return {
success: true,
data: aa,
};
} else if (aType === zod.ZodParsedType.object && bType === zod.ZodParsedType.object) {
const bKeys = Object.keys(bb as never);
const sharedKeys = Object.keys(aa as never).filter((key) => bKeys.includes(key));
const newObj: any = {
...aa,
...bb,
};
for (const key of sharedKeys) {
const result = ZodIntersectionAccelerator.mergeValues(aa[key], bb[key], path);
if (!result.success) {
return result;
}
newObj[key] = result.data;
}
return {
success: true,
data: newObj,
};
} else if (aType === zod.ZodParsedType.array && bType === zod.ZodParsedType.array) {
if (aa.length !== bb.length) {
return {
success: false,
error: new ZodAcceleratorError(path, "Intersection results could not be merged."),
};
}
const newArray = [];
for (let index = 0; index < aa.length; index++) {
const itemA = aa[index];
const itemB = bb[index];
const result = ZodIntersectionAccelerator.mergeValues(itemA, itemB, path);
if (!result.success) {
return result;
}
newArray.push(result.data);
}
return {
success: true,
data: newArray,
};
} else if (
aType === zod.ZodParsedType.date
&& bType === zod.ZodParsedType.date
&& Number(aa) === Number(bb)
) {
return {
success: true,
data: aa,
};
} else {
return {
success: false,
error: new ZodAcceleratorError(path, "Intersection results could not be merged."),
};
}
}
}