forked from planetarium/Corvette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiSchema.ts
97 lines (93 loc) · 2.45 KB
/
apiSchema.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
import Ajv, { type JSONSchemaType } from "https://esm.sh/[email protected]";
import ajvFormats from "https://esm.sh/[email protected]";
const ajv = new Ajv({ allowUnionTypes: true });
ajvFormats(ajv);
interface EventRequest {
blockIndex?: number;
blockHash?: string;
blockFrom?: number | string;
blockTo?: number | string;
logIndex?: number;
transactionHash?: string;
sourceAddress?: string;
abiHash?: string;
abiSignature?: string;
after?: string;
before?: string;
}
const eventRequestSchema: JSONSchemaType<EventRequest> = {
type: "object",
properties: {
blockIndex: { type: "number", nullable: true },
blockHash: {
type: "string",
nullable: true,
pattern: "^(0x)?[a-fA-F0-9]{64}$",
},
blockFrom: {
type: ["number", "string"],
nullable: true,
anyOf: [
{ type: "number" },
{ type: "string", pattern: "^(0x)?[a-fA-F0-9]{64}$" },
],
},
blockTo: {
type: ["number", "string"],
nullable: true,
anyOf: [
{ type: "number" },
{ type: "string", pattern: "^(0x)?[a-fA-F0-9]{64}$" },
],
},
logIndex: { type: "number", nullable: true },
transactionHash: {
type: "string",
nullable: true,
pattern: "^(0x)?[a-fA-F0-9]{64}$",
},
sourceAddress: {
type: "string",
nullable: true,
pattern: "^(0x)?[a-fA-F0-9]{40}$",
},
abiHash: {
type: "string",
nullable: true,
pattern: "^(0x)?[a-fA-F0-9]{64}$",
},
abiSignature: { type: "string", nullable: true },
after: { type: "string", nullable: true, format: "date-time" },
before: { type: "string", nullable: true, format: "date-time" },
},
allOf: [
{
oneOf: [
{
allOf: [
{ not: { required: ["blockHash"] } },
{ not: { required: ["blockIndex"] } },
{ not: { required: ["blockFrom"] } },
{ not: { required: ["blockTo"] } },
],
},
{ required: ["blockHash"] },
{ required: ["blockIndex"] },
{ anyOf: [{ required: ["blockFrom"] }, { required: ["blockTo"] }] },
],
},
{
oneOf: [
{
allOf: [
{ not: { required: ["abiHash"] } },
{ not: { required: ["abiSignature"] } },
],
},
{ required: ["abiHash"] },
{ required: ["abiSignature"] },
],
},
],
};
export const validateEventRequest = ajv.compile(eventRequestSchema);