n8n-nodes-arubacentral
Version:
n8n community node for Aruba Central API integration with comprehensive monitoring, configuration, and management capabilities
73 lines (72 loc) • 2.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateResponseFormat = validateResponseFormat;
exports.validateRequiredParameters = validateRequiredParameters;
exports.validateAllowedValue = validateAllowedValue;
// helpers/validation.ts
const logger_1 = require("./logger");
/**
* Type guard to validate response against expected interface
*
* @param data The data to validate
* @param requiredProps Array of required property names
* @param arrayProps Object mapping property names to expected array types
* @returns boolean indicating if data matches expected format
*/
function validateResponseFormat(data, requiredProps = [], arrayProps = {}) {
if (data === null || typeof data !== 'object') {
logger_1.logger.debug('validation', 'Data is not an object');
return false;
}
// Check all required properties exist
for (const prop of requiredProps) {
if (!(prop in data)) {
logger_1.logger.debug('validation', `Required property "${prop}" missing`);
return false;
}
}
// Validate array properties if specified
for (const [prop, shouldBeArray] of Object.entries(arrayProps)) {
const value = data[prop];
const isArray = Array.isArray(value);
if (shouldBeArray && !isArray) {
logger_1.logger.debug('validation', `Property "${prop}" should be an array but isn't`);
return false;
}
else if (!shouldBeArray && isArray) {
logger_1.logger.debug('validation', `Property "${prop}" shouldn't be an array but is`);
return false;
}
}
return true;
}
/**
* Validates that all required parameters are provided
*
* @param params Object containing parameters to validate
* @param required Array of required parameter names
* @returns Error message if validation fails, otherwise null
*/
function validateRequiredParameters(params, required) {
for (const param of required) {
const value = params[param];
if (value === undefined || value === null || value === '') {
return `Required parameter "${param}" is missing or empty`;
}
}
return null;
}
/**
* Validates that a parameter's value is within a set of allowed values
*
* @param param Parameter name
* @param value Parameter value
* @param allowedValues Array of allowed values
* @returns Error message if validation fails, otherwise null
*/
function validateAllowedValue(param, value, allowedValues) {
if (!allowedValues.includes(value)) {
return `Value "${value}" for parameter "${param}" is not allowed. Allowed values: ${allowedValues.join(', ')}`;
}
return null;
}