UNPKG

n8n-nodes-mautic-advanced

Version:

Enhanced n8n node for Mautic with comprehensive API coverage including tags, campaigns, categories, and advanced contact management

123 lines (122 loc) 5.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateOptionsStructure = exports.validateArrayParam = exports.validateDateString = exports.validateUrl = exports.validateEmail = exports.validateOptionalParam = exports.validateRequiredNumber = exports.validateRequiredString = void 0; const n8n_workflow_1 = require("n8n-workflow"); // Validate required string parameter function validateRequiredString(context, paramName, itemIndex, errorMessage) { const value = context.getNodeParameter(paramName, itemIndex); if (!value || typeof value !== 'string' || value.trim() === '') { throw new n8n_workflow_1.NodeOperationError(context.getNode(), errorMessage || `Parameter '${paramName}' is required and must be a non-empty string.`, { itemIndex }); } return value.trim(); } exports.validateRequiredString = validateRequiredString; // Validate required number parameter function validateRequiredNumber(context, paramName, itemIndex, min, max, errorMessage) { const value = context.getNodeParameter(paramName, itemIndex); if (value === undefined || value === null || isNaN(value)) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), errorMessage || `Parameter '${paramName}' is required and must be a valid number.`, { itemIndex }); } if (min !== undefined && value < min) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Parameter '${paramName}' must be at least ${min}.`, { itemIndex }); } if (max !== undefined && value > max) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Parameter '${paramName}' must be at most ${max}.`, { itemIndex }); } return value; } exports.validateRequiredNumber = validateRequiredNumber; // Validate optional parameter with type checking function validateOptionalParam(context, paramName, itemIndex, expectedType, defaultValue) { try { const value = context.getNodeParameter(paramName, itemIndex, defaultValue); if (value === undefined || value === null) { return defaultValue; } switch (expectedType) { case 'string': if (typeof value !== 'string') throw new Error(`Parameter '${paramName}' must be a string.`); return value; case 'number': if (typeof value !== 'number' || isNaN(value)) throw new Error(`Parameter '${paramName}' must be a valid number.`); return value; case 'boolean': if (typeof value !== 'boolean') throw new Error(`Parameter '${paramName}' must be a boolean.`); return value; case 'object': if (typeof value !== 'object' || value === null) throw new Error(`Parameter '${paramName}' must be an object.`); return value; default: return value; } } catch (error) { if (defaultValue !== undefined) { return defaultValue; } throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Parameter '${paramName}' validation failed: ${error.message}`, { itemIndex }); } } exports.validateOptionalParam = validateOptionalParam; // Validate email format function validateEmail(context, email, itemIndex) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(email)) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'Invalid email format provided.', { itemIndex, }); } return email; } exports.validateEmail = validateEmail; // Validate URL format function validateUrl(context, url, itemIndex, paramName = 'url') { try { // eslint-disable-next-line no-new new URL(url); return url; } catch { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Parameter '${paramName}' must be a valid URL.`, { itemIndex }); } } exports.validateUrl = validateUrl; // Validate date format function validateDateString(context, dateString, itemIndex, paramName = 'date') { const date = new Date(dateString); if (isNaN(date.getTime())) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Parameter '${paramName}' must be a valid date.`, { itemIndex }); } return dateString; } exports.validateDateString = validateDateString; // Validate array parameter function validateArrayParam(context, paramName, itemIndex, minLength, maxLength) { const value = context.getNodeParameter(paramName, itemIndex); if (!Array.isArray(value)) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Parameter '${paramName}' must be an array.`, { itemIndex, }); } if (minLength !== undefined && value.length < minLength) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Parameter '${paramName}' must contain at least ${minLength} items.`, { itemIndex }); } if (maxLength !== undefined && value.length > maxLength) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Parameter '${paramName}' must contain at most ${maxLength} items.`, { itemIndex }); } return value; } exports.validateArrayParam = validateArrayParam; // Validate options object structure function validateOptionsStructure(context, options, requiredFields, itemIndex) { for (const field of requiredFields) { if (!(field in options)) { throw new n8n_workflow_1.NodeOperationError(context.getNode(), `Options object is missing required field: '${field}'`, { itemIndex }); } } } exports.validateOptionsStructure = validateOptionsStructure;