-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheffects.ts
89 lines (79 loc) · 3.15 KB
/
effects.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
import * as zod from "zod";
import {ZodAccelerator} from "../accelerator";
import {ZodAcceleratorContent} from "../content";
@ZodAccelerator.autoInstance
export class ZodEffectAccelerator extends ZodAccelerator{
public get support(){
return ZodAccelerator.zod.ZodEffects;
}
public makeAcceleratorContent(zodSchema: zod.ZodEffects<zod.ZodAny>, zac: ZodAcceleratorContent){
const def = zodSchema._def;
zac.addContext({
transform: def.effect.type === "transform" ? def.effect.transform : undefined,
refinement: def.effect.type === "refinement" ? def.effect.refinement : undefined,
preprocess: def.effect.type === "preprocess" ? def.effect.transform : undefined,
duploj$Never: zod.NEVER,
});
zac.addContent(
def.effect.type !== "preprocess" || ZodEffectAccelerator.contentPart.preprocess(def.effect.transform.constructor.name === "AsyncFunction"),
[
ZodAccelerator.findAcceleratorContent(def.schema),
{
path: null,
input: "$input",
output: "$input",
}
],
def.effect.type !== "transform" || ZodEffectAccelerator.contentPart.transform(def.effect.transform.constructor.name === "AsyncFunction"),
def.effect.type !== "refinement" || ZodEffectAccelerator.contentPart.refinement(def.effect.refinement.constructor.name === "AsyncFunction")
);
return zac;
}
static contentPart = {
transform: (isAsync: boolean) => `
let $id_issue;
$input = ${isAsync ? "await " : ""}$this.transform(
$input,
{
path: [],
addIssue: (issue) => {
$id_issue = {success: false, error: new ZodAcceleratorError(\`$path\`, issue.message || "Effect transform issue without message.")};
}
}
)
if($id_issue || $input === this.duploj$Never){
return $id_issue || {success: false, error: new ZodAcceleratorError(\`$path\`, "Effect return never.")};
}
`,
refinement: (isAsync: boolean) => `
let $id_issue;
${isAsync ? "await " : ""}$this.refinement(
$input,
{
path: [],
addIssue: (issue) => {
$id_issue = {success: false, error: new ZodAcceleratorError(\`$path\`, issue.message || "Effect refinement issue without message.")};
}
}
)
if($id_issue || $input === this.duploj$Never){
return $id_issue || {success: false, error: new ZodAcceleratorError(\`$path\`, "Effect return never.")};
}
`,
preprocess: (isAsync: boolean) => `
let $id_issue;
$input = ${isAsync ? "await " : ""}$this.preprocess(
$input,
{
path: [],
addIssue: (issue) => {
$id_issue = {success: false, error: new ZodAcceleratorError(\`$path\`, issue.message || "Effect preprocess issue without message.")};
}
}
)
if($id_issue || $input === this.duploj$Never){
return $id_issue || {success: false, error: new ZodAcceleratorError(\`$path\`, "Effect return never.")};
}
`,
};
}