-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidateBatchConfig.ts
More file actions
272 lines (264 loc) · 8.23 KB
/
Copy pathvalidateBatchConfig.ts
File metadata and controls
272 lines (264 loc) · 8.23 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { array, z } from "zod";
const urlParamRegex = /^[a-zA-Z0-9_-]+$/;
const customIdInstructionsSchema = z.any().superRefine((data, ctx) => {
console.log("data", data, "tyepof", typeof data);
if (typeof data === "string") {
if (data.endsWith(".md")) {
return;
} else if (data === "none") {
return;
} else {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
'Custom ID instructions should be a markdown file ending with ".md" or "none"',
});
}
} else if (
typeof data === "object" &&
data !== null &&
!Array.isArray(data)
) {
const keys = Object.keys(data);
if (keys.length === 0) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "CustomIdInstructions dictionary must not be empty",
});
return;
}
for (const key of keys) {
if (!urlParamRegex.test(key)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Keys must be valid URL parameters (alphanumeric, underscores, or hyphens) or "default"`,
});
}
const value = data[key];
if (typeof value !== "string" || !value.endsWith(".md")) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Values must be strings ending with ".md". Got "${value}" for key "${key}"`,
});
}
}
} else {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
"Custom ID instructions should be a string or a dictionary with valid URL parameters as keys and markdown files as values",
});
}
});
export const batchConfigSchema = z
.object({
batchName: z.string(),
cdn: z.enum(["test", "prod", "local"]),
treatmentFile: z.string().regex(/\.yaml$/),
// introSequence: z.literal("none").or(z.string()),
introSequence: z.string().or(
z.literal("none", {
message: `If you do not wish to use an intro sequence, enter value "none"`,
})
),
treatments: z.array(z.string()).nonempty(),
payoffs: z
.array(z.number().positive())
.nonempty()
.or(
z.literal("equal", {
message: `If you do not wish to define different payoffs for each treatment, enter value "equal"`,
})
),
knockdowns: z
.union([
z.number().gt(0).lte(1),
z.array(z.number().gt(0).lte(1)).nonempty(),
z.array(z.array(z.number().gt(0).lte(1)).nonempty()).nonempty(),
])
.or(
z.literal("none", {
message: `If you do not wish to use payoff knockdowns, enter value "none"`,
})
),
exitCodes: z
.object({
complete: z.string(),
error: z.string(),
lobbyTimeout: z.string(),
failedEquipmentCheck: z.string(),
})
.or(
z.literal("none", {
message: `If you do not wish to supply exit codes, enter value "none"`,
})
),
launchDate: z
.string()
.transform((str) => new Date(str))
.refine((date) => date > new Date(), {
message: `Launch date must be in the future. If you do not wish to use a launch date, enter value "immediate"`,
})
.or(
z.literal("immediate", {
message: `If you do not wish to use a launch date, enter value "immediate"`,
})
),
customIdInstructions: customIdInstructionsSchema,
platformConsent: z.enum(["US", "EU", "UK", "custom"]),
consentAddendum: z.string().or(
z.literal("none", {
message: `If you do not wish to use an additional consent addendum, enter value "none"`,
})
),
dispatchWait: z.number().positive(),
videoStorage: z
.object({
bucket: z.string(),
region: z.enum([
"af-south-1",
"ap-east-1",
"ap-northeast-1",
"ap-northeast-2",
"ap-northeast-3",
"ap-south-1",
"ap-south-2",
"ap-southeast-1",
"ap-southeast-2",
"ap-southeast-3",
"ap-southeast-4",
"ca-central-1",
"ca-west-1",
"eu-central-1",
"eu-central-2",
"eu-north-1",
"eu-south-1",
"eu-south-2",
"eu-west-1",
"eu-west-2",
"eu-west-3",
"il-central-1",
"me-central-1",
"me-south-1",
"sa-east-1",
"us-east-1",
"us-east-2",
"us-west-1",
"us-west-2",
]),
})
.or(
z.literal("none", {
message: `If you do not wish to store video, enter value "none"`,
})
),
preregRepos: z.array(
z.object({
owner: z.string(),
repo: z.string(),
branch: z.string(),
directory: z.string(),
}),
{
message: `If you do not wish to specify a separate preregistration repository, enter an empty array "[]"`,
}
),
dataRepos: z.array(
z.object({
owner: z.string(),
repo: z.string(),
branch: z.string(),
directory: z.string(),
})
),
centralPrereg: z.boolean({
message: `Must be a boolean. If you do not wish to preregister to the central repository, enter "false"`,
}),
checkAudio: z.boolean({
message: `Must be a boolean. If you do not wish to check participant audio, enter "false"`,
}),
checkVideo: z.boolean({
message: `Must be a boolean. If you do not wish to check participant video, enter "false"`,
}),
effortCheck: z
.union([z.boolean(), z.string()])
.optional(),
})
.strict()
.superRefine((obj, ctx) => {
// check that length of payoffs matches length of treatments
if (
obj.payoffs !== "equal" &&
obj.treatments.length !== obj.payoffs.length
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Number of payoffs must match number of treatments, or be set to "equal"`,
path: ["payoffs"],
});
}
if (obj.checkVideo && !obj.checkAudio) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Cannot check video without also checking audio`,
path: ["checkAudio"],
});
}
if (obj.knockdowns !== "none") {
if (Array.isArray(obj.knockdowns)) {
// if any row is an array, all rows must be arrays
const isMatrix = !obj.knockdowns.every((row) => !Array.isArray(row));
if (isMatrix) {
if (obj.knockdowns.length !== obj.treatments.length) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Number of rows in knockdown matrix must match number of treatments`,
path: ["knockdowns"],
});
}
obj.knockdowns.forEach((row, index) => {
// check that all rows have same length as treatments
if (!Array.isArray(row) || row.length !== obj.treatments.length) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Knockdown matrix row ${index} must match number of treatments`,
path: ["knockdowns"],
});
}
});
} else {
if (obj.knockdowns.length !== obj.treatments.length) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Number of knockdowns must match number of treatments`,
path: ["knockdowns"],
});
}
}
}
}
});
export type BatchConfigType = z.infer<typeof batchConfigSchema>;
class ValidationError extends Error {
constructor(message?: string) {
super(message);
this.name = "ValidationError";
}
}
//changed line 260 errors type so it would throw no errors in deliberation-lab-tools, if it breaks something then revert this change
export function validateBatchConfig(config: unknown) {
const result = batchConfigSchema.safeParse(config);
if (!result.success) {
const errors = result.error.format();
const generalErrors = errors["_errors"];
const keyErrors = Object.keys(errors).map((key, index) =>
key[0] !== "_" ? `${key}: ${(errors as Record<string, any>)[key]["_errors"]?.join(" - ")}` : ""
);
throw new ValidationError(
`Problem(s) in batch config:\n- ${[...generalErrors, ...keyErrors].join(
"\n- "
)}`
);
}
return result.data;
}