/**
* Binds arguments of queues for hypothesis validation with basic sanity checks.
*
* @param {Array<Object>} queues An array of queue objects. Each queue object should have a 'name' and 'arguments' property.
* @param {Object} validationConfig A configuration object specifying validation rules for each queue.
* Example: { queueName: { expectedType: 'string', requiredFields: ['field1', 'field2'] } }
* @returns {Array<Object>} An array of validated queue objects. Returns an empty array if queues is invalid.
*/
function validateQueues(queues, validationConfig) {
if (!Array.isArray(queues)) {
console.error("Invalid input: queues must be an array.");
return [];
}
if (!validationConfig || typeof validationConfig !== 'object') {
console.error("Invalid input: validationConfig must be an object.");
return [];
}
const validatedQueues = [];
for (const queue of queues) {
if (typeof queue !== 'object' || !queue.hasOwnProperty('name') || !queue.hasOwnProperty('arguments')) {
console.warn("Skipping invalid queue:", queue);
continue;
}
const queueName = queue.name;
const arguments = queue.arguments;
if (!validationConfig.hasOwnProperty(queueName)) {
console.warn(`No validation config found for queue: ${queueName}. Skipping validation.`);
validatedQueues.push(queue); //push the queue even if no validation is done
continue;
}
const config = validationConfig[queueName];
if (typeof config.expectedType === 'string' && arguments.length > 0) {
const firstArgument = arguments[0];
if (typeof firstArgument !== config.expectedType) {
console.error(`Validation failed for queue ${queueName}: Argument type is incorrect. Expected ${config.expectedType}, got ${typeof firstArgument}`);
continue; // Skip this queue if type validation fails
}
}
if (config.requiredFields && Array.isArray(config.requiredFields)) {
for (const field of config.requiredFields) {
if (!arguments.includes(field)) {
console.error(`Validation failed for queue ${queueName}: Missing required field ${field}`);
continue; // Skip this queue if required field validation fails.
}
}
}
validatedQueues.push(queue);
}
return validatedQueues;
}
Add your comment