-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbigInt.ts
51 lines (46 loc) · 1.46 KB
/
bigInt.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
import * as zod from "zod";
import {ZodAccelerator} from "../accelerator";
import {ZodAcceleratorContent} from "../content";
@ZodAccelerator.autoInstance
export class ZodBigIntAccelerator extends ZodAccelerator{
public get support(){
return ZodAccelerator.zod.ZodBigInt;
}
public makeAcceleratorContent(zodSchema: zod.ZodBigInt, zac: ZodAcceleratorContent){
const def = zodSchema._def;
zac.addContent(
def.coerce
? ZodBigIntAccelerator.contentPart.coerce()
: ZodBigIntAccelerator.contentPart.typeof(),
...def.checks.map(
check => ZodBigIntAccelerator.contentPart[check.kind]?.(check as any)
)
);
return zac;
}
static contentPart = {
coerce: () => `
try {
$input = BigInt($input).valueOf();
} catch {
return {success: false, error: new ZodAcceleratorError(\`$path\`, "Input is not BigInt.")};
}
`,
typeof: () => ({
if: /* js */"(typeof $input !== \"bigint\")",
message: "Input is not BigInt.",
}),
multipleOf: ({value}: {value: bigint}) => ({
if: /* js */`$input % ${value.toString()}n !== BigInt(0)`,
message: `Input BigInt is not multiple of ${value}.`,
}),
min: ({value}: {value: number}) => ({
if: /* js */`$input <= ${value}`,
message: `Input BigInt is less or equal than ${value}.`,
}),
max: ({value}: {value: number}) => ({
if: /* js */`$input >= ${value}`,
message: `Input BigInt is more or equal than ${value}.`,
}),
};
}