forked from galaxyproject/galaxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.js
More file actions
442 lines (420 loc) · 15.3 KB
/
utilities.js
File metadata and controls
442 lines (420 loc) · 15.3 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import { isDefined, isValidNumber } from "@/utils/validation";
/** Visits tool inputs.
* @param{dict} inputs - Nested dictionary of input elements
* @param{dict} callback - Called with the mapped dictionary object and corresponding model node
*/
export function visitInputs(inputs, callback, prefix = "", context = undefined) {
context = Object.assign({}, context);
for (const key in inputs) {
const input = inputs[key];
if (input && input.type && input.name) {
context[input.name] = input;
}
}
for (var key in inputs) {
var node = inputs[key];
node.name = node.name || key;
var name = prefix ? `${prefix}|${node.name}` : node.name;
switch (node.type) {
case "repeat":
if (node.cache) {
for (const [j, cache] of Object.entries(node.cache)) {
visitInputs(cache, callback, `${name}_${j}`, context);
}
}
break;
case "conditional":
if (node.test_param) {
callback(node.test_param, `${name}|${node.test_param.name}`, context);
var selectedCase = matchCase(node, node.test_param.value);
if (selectedCase != -1) {
visitInputs(node.cases[selectedCase].inputs, callback, name, context);
} else {
console.debug(`Form.utilities::visitInputs() - Invalid case for ${name}.`);
}
} else {
console.debug(`Form.utilities::visitInputs() - Conditional test parameter missing for ${name}.`);
}
break;
case "section":
visitInputs(node.inputs, callback, name, context);
break;
default:
callback(node, name, context);
}
}
}
/** Visits ALL inputs including every conditional case (not just the active one).
* Used for syncing server attributes to all conditional branches.
* @param{Array} inputs - Nested array of input elements
* @param{Function} callback - Called with each input node and its name
* @param{String} prefix - Key prefix for nested param name construction
*/
export function visitAllInputs(inputs, callback, prefix = "") {
for (var key in inputs) {
var node = inputs[key];
var nodeName = node.name || key;
var name = prefix ? `${prefix}|${nodeName}` : nodeName;
switch (node.type) {
case "repeat":
if (node.cache) {
for (const [j, cache] of Object.entries(node.cache)) {
visitAllInputs(cache, callback, `${name}_${j}`);
}
}
break;
case "conditional":
if (node.test_param) {
callback(node.test_param, `${name}|${node.test_param.name}`);
for (var i = 0; i < node.cases.length; i++) {
visitAllInputs(node.cases[i].inputs, callback, name);
}
}
break;
case "section":
visitAllInputs(node.inputs, callback, name);
break;
default:
callback(node, name);
}
}
}
/** Matches conditional values to selected cases.
* @param{dict} input - Definition of conditional input parameter
* @param{dict} value - Current value
*/
export function matchCase(input, value) {
if (input.test_param.type == "boolean") {
if (["true", true].includes(value)) {
if (input.test_param.truevalue !== undefined) {
value = input.test_param.truevalue;
} else {
value = "true";
}
} else {
if (input.test_param.falsevalue !== undefined) {
value = input.test_param.falsevalue;
} else {
value = "false";
}
}
}
for (let i = 0; i < input.cases.length; i++) {
if (input.cases[i].value == value) {
return i;
}
}
return -1;
}
/** Match server validation response to highlight inputs
* @param{dict} index - Index of input elements
* @param{dict} response - Nested dictionary with error/warning messages
*/
export function matchInputs(index, response) {
var result = {};
function search(id, head) {
if (typeof head === "string") {
if (index[id]) {
result[id] = head;
}
} else {
for (var i in head) {
var new_id = i;
if (id !== "") {
var separator = "|";
if (head instanceof Array) {
separator = "_";
}
new_id = id + separator + new_id;
}
search(new_id, head[i]);
}
}
}
search("", response);
return result;
}
/** Builds a nested state dict from the form input tree and flat formData.
* Produces the format expected by RequestToolState (POST /api/jobs).
* @param{Array} inputs - Nested array of input elements (the tool form tree)
* @param{Object} formData - Flat dictionary with pipe-separated keys (e.g. "cond|param")
* @returns{Object} Nested dictionary matching the RequestToolState format
*/
export function buildNestedState(inputs, formData) {
return _buildLevel(inputs, formData, "");
}
function _buildLevel(inputs, formData, prefix) {
const result = {};
for (const key in inputs) {
const node = inputs[key];
const nodeName = node.name || key;
const flatKey = prefix ? `${prefix}|${nodeName}` : nodeName;
switch (node.type) {
case "repeat": {
const items = [];
if (node.cache) {
for (const [j, cache] of Object.entries(node.cache)) {
items.push(_buildLevel(cache, formData, `${flatKey}_${j}`));
}
}
result[nodeName] = items;
break;
}
case "conditional": {
const condResult = {};
if (node.test_param) {
const testKey = `${flatKey}|${node.test_param.name}`;
condResult[node.test_param.name] = _convertValue(node.test_param, formData[testKey]);
const selectedCase = matchCase(node, node.test_param.value);
if (selectedCase !== -1) {
Object.assign(condResult, _buildLevel(node.cases[selectedCase].inputs, formData, flatKey));
}
}
result[nodeName] = condResult;
break;
}
case "section":
result[nodeName] = _buildLevel(node.inputs, formData, flatKey);
break;
default:
result[nodeName] = _convertValue(node, formData[flatKey]);
}
}
return result;
}
function _convertValue(node, value) {
if (node.type === "data" || node.type === "data_collection") {
return _convertDataValue(value, node.multiple);
}
if (node.type === "data_column") {
if (value === undefined) {
return undefined;
}
if (value === null || value === "") {
return null;
}
if (Array.isArray(value)) {
return value.map((v) => (typeof v === "string" ? parseInt(v, 10) : v));
}
return typeof value === "string" ? parseInt(value, 10) : value;
}
if (node.type === "integer") {
if (value === undefined) {
return undefined;
}
if (value === null || value === "") {
return null;
}
return typeof value === "string" ? parseInt(value, 10) : value;
}
if (node.type === "float") {
if (value === undefined) {
return undefined;
}
if (value === null || value === "") {
return null;
}
return typeof value === "string" ? parseFloat(value) : value;
}
if (node.type === "boolean") {
if (typeof value === "string") {
return value === "true";
}
return value;
}
if (node.type === "select" && node.multiple) {
if (value === null || value === undefined) {
return value;
}
if (!Array.isArray(value)) {
return [value];
}
return value;
}
return value;
}
function _convertDataValue(value, multiple = false) {
if (!value || !value.values || value.values.length === 0) {
return null;
}
if (value.batch) {
return {
__class__: "Batch",
values: value.values.map((v) => _convertDataEntry(v)),
};
}
if (value.values.length === 1 && !multiple) {
return _convertDataEntry(value.values[0]);
}
return value.values.map((v) => _convertDataEntry(v));
}
function _convertDataEntry(v) {
const entry = { src: v.src, id: v.id };
if (v.map_over_type) {
entry.map_over_type = v.map_over_type;
}
return entry;
}
/** Validate value against a regular expression pattern
* @param{object} validator - Validator definition
* @param{*} value - Value to validate
* @returns{object} Object with isValid boolean and message string
*/
function validateRegex(validator, value) {
try {
const regex = new RegExp(validator.expression);
const matches = regex.test(String(value));
const isValid = validator.negate ? !matches : matches;
return {
isValid: isValid,
message: isValid ? null : validator.message,
};
} catch (error) {
return {
isValid: false,
message: `Invalid validation pattern: ${error.message}`,
};
}
}
/** Validate value length is within specified bounds
* @param{object} validator - Validator definition
* @param{*} value - Value to validate
* @returns{object} Object with isValid boolean and message string
*/
function validateLength(validator, value) {
const valueLength = String(value).length;
let isValid = true;
if (isValidNumber(validator.min) && valueLength < validator.min) {
isValid = false;
}
if (isValidNumber(validator.max) && valueLength > validator.max) {
isValid = false;
}
if (validator.negate) {
isValid = !isValid;
}
return {
isValid: isValid,
message: isValid ? null : validator.message,
};
}
/** Validate numeric value is within specified range
* @param{object} validator - Validator definition
* @param{*} value - Value to validate
* @returns{object} Object with isValid boolean and message string
*/
function validateInRange(validator, value) {
const numericValue = Number(value);
if (isNaN(numericValue)) {
return {
isValid: false,
message: "Value must be numeric for range validation",
};
}
let isValid = true;
if (isValidNumber(validator.min) && numericValue < validator.min) {
isValid = false;
}
if (isValidNumber(validator.max) && numericValue > validator.max) {
isValid = false;
}
if (validator.negate) {
isValid = !isValid;
}
return {
isValid: isValid,
message: isValid ? null : validator.message,
};
}
// Map validator types to their validation functions
const validatorFunctions = {
regex: validateRegex,
length: validateLength,
in_range: validateInRange,
};
/** Run a single validator
* @param{object} validator - Validator definition with type, expression, message, etc.
* @param{*} value - Value to validate
* @returns{object} Object with isValid boolean and message string
*/
function runValidator(validator, value) {
const validatorFunc = validatorFunctions[validator.type];
if (validatorFunc) {
return validatorFunc(validator, value);
}
// Unknown validator type - consider valid by default
return { isValid: true, message: null };
}
/** Validates input parameters to identify issues before submitting a server request, where comprehensive validation is performed.
* @param{dict} index - Index of input elements
* @param{dict} values - Dictionary of parameter values
*/
export function validateInputs(index, values, rejectEmptyRequiredInputs = false) {
let batchN = -1;
let batchSrc = null;
for (const inputId in index) {
const inputDef = index[inputId];
const inputValue = values[inputId];
const isEmpty = !isDefined(inputValue) || inputValue === "";
const hasValue = !isEmpty;
const isRequired = !inputDef.optional;
if (!inputDef || inputDef.step_linked) {
continue;
}
if (isRequired && inputDef.type != "hidden") {
if (!isDefined(inputValue) || (rejectEmptyRequiredInputs && inputValue === "")) {
return [inputId, "Please provide a value for this option."];
}
}
if (inputDef.wp_linked && inputDef.text_value == inputValue) {
return [inputId, "Please provide a value for this workflow parameter."];
}
if (inputValue && Array.isArray(inputValue.values) && inputValue.values.length == 0 && isRequired) {
return [inputId, "Please provide data for this input."];
}
if (inputValue) {
if (inputValue.rules && inputValue.rules.length == 0) {
return [inputId, "No rules defined, define at least one rule."];
}
if (inputValue.mapping && inputValue.mapping.length == 0) {
return [inputId, "No collection identifiers defined, specify at least one collection identifier."];
}
if (inputValue.rules && inputValue.rules.length > 0) {
for (const rule of inputValue.rules) {
if (rule.error) {
return [inputId, "Error detected in one or more rules."];
}
}
}
}
if (inputValue && inputValue.batch) {
const n = inputValue.values.length;
const src = n > 0 && inputValue.values[0] && inputValue.values[0].src;
if (src) {
if (batchSrc === null) {
batchSrc = src;
} else if (batchSrc !== src) {
return [inputId, "Please select either dataset or dataset list fields for all batch mode fields."];
}
}
if (batchN === -1) {
batchN = n;
} else if (batchN !== n) {
return [
inputId,
`Please make sure that you select the same number of inputs for all batch mode fields. This field contains ${n} selection(s) while a previous field contains ${batchN}.`,
];
}
}
// Run custom validators if field is required or has a value
if (inputDef.validators && (isRequired || hasValue)) {
for (const validator of inputDef.validators) {
const validationResult = runValidator(validator, inputValue);
if (!validationResult.isValid) {
return [inputId, validationResult.message];
}
}
}
}
return null;
}