mirror of
https://github.com/czlonkowski/n8n-mcp.git
synced 2026-02-09 06:43:08 +00:00
fix: address critical code review issues for validation improvements
- Fix type safety vulnerability in enhanced-config-validator.ts - Added proper type checking before string operations - Return early when nodeType is invalid instead of using empty string - Improve error handling robustness in MCP server - Wrapped validation in try-catch to handle unexpected errors - Properly re-throw ValidationError instances - Add user-friendly error messages for internal errors - Write comprehensive CHANGELOG entry for v2.10.3 - Document fixes for issues #58, #68, #70, #73 - Detail new validation system features - List all enhancements and test coverage Addressed HIGH priority issues from code review: - Type safety holes in config validator - Missing error handling for validation system failures - Consistent error types across validation tools
This commit is contained in:
@@ -28,6 +28,7 @@ import { handleUpdatePartialWorkflow } from './handlers-workflow-diff';
|
||||
import { getToolDocumentation, getToolsOverview } from './tools-documentation';
|
||||
import { PROJECT_VERSION } from '../utils/version';
|
||||
import { normalizeNodeType, getNodeTypeAlternatives, getWorkflowNodeType } from '../utils/node-utils';
|
||||
import { ToolValidation, Validator, ValidationError } from '../utils/validation-schemas';
|
||||
import {
|
||||
negotiateProtocolVersion,
|
||||
logProtocolNegotiation,
|
||||
@@ -460,9 +461,77 @@ export class N8NDocumentationMCPServer {
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate required parameters for tool execution
|
||||
* Enhanced parameter validation using schemas
|
||||
*/
|
||||
private validateToolParams(toolName: string, args: any, requiredParams: string[]): void {
|
||||
private validateToolParams(toolName: string, args: any, legacyRequiredParams?: string[]): void {
|
||||
try {
|
||||
// If legacy required params are provided, use the new validation but fall back to basic if needed
|
||||
let validationResult;
|
||||
|
||||
switch (toolName) {
|
||||
case 'validate_node_operation':
|
||||
validationResult = ToolValidation.validateNodeOperation(args);
|
||||
break;
|
||||
case 'validate_node_minimal':
|
||||
validationResult = ToolValidation.validateNodeMinimal(args);
|
||||
break;
|
||||
case 'validate_workflow':
|
||||
case 'validate_workflow_connections':
|
||||
case 'validate_workflow_expressions':
|
||||
validationResult = ToolValidation.validateWorkflow(args);
|
||||
break;
|
||||
case 'search_nodes':
|
||||
validationResult = ToolValidation.validateSearchNodes(args);
|
||||
break;
|
||||
case 'list_node_templates':
|
||||
validationResult = ToolValidation.validateListNodeTemplates(args);
|
||||
break;
|
||||
case 'n8n_create_workflow':
|
||||
validationResult = ToolValidation.validateCreateWorkflow(args);
|
||||
break;
|
||||
case 'n8n_get_workflow':
|
||||
case 'n8n_get_workflow_details':
|
||||
case 'n8n_get_workflow_structure':
|
||||
case 'n8n_get_workflow_minimal':
|
||||
case 'n8n_update_full_workflow':
|
||||
case 'n8n_delete_workflow':
|
||||
case 'n8n_validate_workflow':
|
||||
case 'n8n_get_execution':
|
||||
case 'n8n_delete_execution':
|
||||
validationResult = ToolValidation.validateWorkflowId(args);
|
||||
break;
|
||||
default:
|
||||
// For tools not yet migrated to schema validation, use basic validation
|
||||
return this.validateToolParamsBasic(toolName, args, legacyRequiredParams || []);
|
||||
}
|
||||
|
||||
if (!validationResult.valid) {
|
||||
const errorMessage = Validator.formatErrors(validationResult, toolName);
|
||||
logger.error(`Parameter validation failed for ${toolName}:`, errorMessage);
|
||||
throw new ValidationError(errorMessage);
|
||||
}
|
||||
} catch (error) {
|
||||
// Handle validation errors properly
|
||||
if (error instanceof ValidationError) {
|
||||
throw error; // Re-throw validation errors as-is
|
||||
}
|
||||
|
||||
// Handle unexpected errors from validation system
|
||||
logger.error(`Validation system error for ${toolName}:`, error);
|
||||
|
||||
// Provide a user-friendly error message
|
||||
const errorMessage = error instanceof Error
|
||||
? `Internal validation error: ${error.message}`
|
||||
: `Internal validation error while processing ${toolName}`;
|
||||
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy parameter validation (fallback)
|
||||
*/
|
||||
private validateToolParamsBasic(toolName: string, args: any, requiredParams: string[]): void {
|
||||
const missing: string[] = [];
|
||||
|
||||
for (const param of requiredParams) {
|
||||
@@ -619,12 +688,17 @@ export class N8NDocumentationMCPServer {
|
||||
fix: 'Provide config as an object with node properties'
|
||||
}],
|
||||
warnings: [],
|
||||
suggestions: [],
|
||||
suggestions: [
|
||||
'🔧 RECOVERY: Invalid config detected. Fix with:',
|
||||
' • Ensure config is an object: { "resource": "...", "operation": "..." }',
|
||||
' • Use get_node_essentials to see required fields for this node type',
|
||||
' • Check if the node type is correct before configuring it'
|
||||
],
|
||||
summary: {
|
||||
hasErrors: true,
|
||||
errorCount: 1,
|
||||
warningCount: 0,
|
||||
suggestionCount: 0
|
||||
suggestionCount: 3
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -638,7 +712,10 @@ export class N8NDocumentationMCPServer {
|
||||
nodeType: args.nodeType || 'unknown',
|
||||
displayName: 'Unknown Node',
|
||||
valid: false,
|
||||
missingRequiredFields: ['Invalid config format - expected object']
|
||||
missingRequiredFields: [
|
||||
'Invalid config format - expected object',
|
||||
'🔧 RECOVERY: Use format { "resource": "...", "operation": "..." } or {} for empty config'
|
||||
]
|
||||
};
|
||||
}
|
||||
return this.validateNodeMinimal(args.nodeType, args.config);
|
||||
@@ -2141,12 +2218,12 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
|
||||
// Get properties
|
||||
const properties = node.properties || [];
|
||||
|
||||
// Extract operation context
|
||||
// Extract operation context (safely handle undefined config properties)
|
||||
const operationContext = {
|
||||
resource: config.resource,
|
||||
operation: config.operation,
|
||||
action: config.action,
|
||||
mode: config.mode
|
||||
resource: config?.resource,
|
||||
operation: config?.operation,
|
||||
action: config?.action,
|
||||
mode: config?.mode
|
||||
};
|
||||
|
||||
// Find missing required fields
|
||||
@@ -2163,7 +2240,7 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
|
||||
// Check show conditions
|
||||
if (prop.displayOptions.show) {
|
||||
for (const [key, values] of Object.entries(prop.displayOptions.show)) {
|
||||
const configValue = config[key];
|
||||
const configValue = config?.[key];
|
||||
const expectedValues = Array.isArray(values) ? values : [values];
|
||||
|
||||
if (!expectedValues.includes(configValue)) {
|
||||
@@ -2176,7 +2253,7 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
|
||||
// Check hide conditions
|
||||
if (isVisible && prop.displayOptions.hide) {
|
||||
for (const [key, values] of Object.entries(prop.displayOptions.hide)) {
|
||||
const configValue = config[key];
|
||||
const configValue = config?.[key];
|
||||
const expectedValues = Array.isArray(values) ? values : [values];
|
||||
|
||||
if (expectedValues.includes(configValue)) {
|
||||
@@ -2189,8 +2266,8 @@ Full documentation is being prepared. For now, use get_node_essentials for confi
|
||||
if (!isVisible) continue;
|
||||
}
|
||||
|
||||
// Check if field is missing
|
||||
if (!(prop.name in config)) {
|
||||
// Check if field is missing (safely handle null/undefined config)
|
||||
if (!config || !(prop.name in config)) {
|
||||
missingFields.push(prop.displayName || prop.name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,19 @@ export class EnhancedConfigValidator extends ConfigValidator {
|
||||
mode: ValidationMode = 'operation',
|
||||
profile: ValidationProfile = 'ai-friendly'
|
||||
): EnhancedValidationResult {
|
||||
// Input validation - ensure parameters are valid
|
||||
if (typeof nodeType !== 'string') {
|
||||
throw new Error(`Invalid nodeType: expected string, got ${typeof nodeType}`);
|
||||
}
|
||||
|
||||
if (!config || typeof config !== 'object') {
|
||||
throw new Error(`Invalid config: expected object, got ${typeof config}`);
|
||||
}
|
||||
|
||||
if (!Array.isArray(properties)) {
|
||||
throw new Error(`Invalid properties: expected array, got ${typeof properties}`);
|
||||
}
|
||||
|
||||
// Extract operation context from config
|
||||
const operationContext = this.extractOperationContext(config);
|
||||
|
||||
@@ -190,6 +203,17 @@ export class EnhancedConfigValidator extends ConfigValidator {
|
||||
config: Record<string, any>,
|
||||
result: EnhancedValidationResult
|
||||
): void {
|
||||
// Type safety check - this should never happen with proper validation
|
||||
if (typeof nodeType !== 'string') {
|
||||
result.errors.push({
|
||||
type: 'invalid_type',
|
||||
property: 'nodeType',
|
||||
message: `Invalid nodeType: expected string, got ${typeof nodeType}`,
|
||||
fix: 'Provide a valid node type string (e.g., "nodes-base.webhook")'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// First, validate fixedCollection properties for known problematic nodes
|
||||
this.validateFixedCollectionStructures(nodeType, config, result);
|
||||
|
||||
|
||||
@@ -79,6 +79,18 @@ export class WorkflowValidator {
|
||||
private nodeValidator: typeof EnhancedConfigValidator
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Check if a node is a Sticky Note or other non-executable node
|
||||
*/
|
||||
private isStickyNote(node: WorkflowNode): boolean {
|
||||
const stickyNoteTypes = [
|
||||
'n8n-nodes-base.stickyNote',
|
||||
'nodes-base.stickyNote',
|
||||
'@n8n/n8n-nodes-base.stickyNote'
|
||||
];
|
||||
return stickyNoteTypes.includes(node.type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a complete workflow
|
||||
*/
|
||||
@@ -127,9 +139,10 @@ export class WorkflowValidator {
|
||||
return result;
|
||||
}
|
||||
|
||||
// Update statistics after null check
|
||||
result.statistics.totalNodes = Array.isArray(workflow.nodes) ? workflow.nodes.length : 0;
|
||||
result.statistics.enabledNodes = Array.isArray(workflow.nodes) ? workflow.nodes.filter(n => !n.disabled).length : 0;
|
||||
// Update statistics after null check (exclude sticky notes from counts)
|
||||
const executableNodes = Array.isArray(workflow.nodes) ? workflow.nodes.filter(n => !this.isStickyNote(n)) : [];
|
||||
result.statistics.totalNodes = executableNodes.length;
|
||||
result.statistics.enabledNodes = executableNodes.filter(n => !n.disabled).length;
|
||||
|
||||
// Basic workflow structure validation
|
||||
this.validateWorkflowStructure(workflow, result);
|
||||
@@ -143,21 +156,26 @@ export class WorkflowValidator {
|
||||
|
||||
// Validate connections if requested
|
||||
if (validateConnections) {
|
||||
this.validateConnections(workflow, result);
|
||||
this.validateConnections(workflow, result, profile);
|
||||
}
|
||||
|
||||
// Validate expressions if requested
|
||||
if (validateExpressions && workflow.nodes.length > 0) {
|
||||
this.validateExpressions(workflow, result);
|
||||
this.validateExpressions(workflow, result, profile);
|
||||
}
|
||||
|
||||
// Check workflow patterns and best practices
|
||||
if (workflow.nodes.length > 0) {
|
||||
this.checkWorkflowPatterns(workflow, result);
|
||||
this.checkWorkflowPatterns(workflow, result, profile);
|
||||
}
|
||||
|
||||
// Add suggestions based on findings
|
||||
this.generateSuggestions(workflow, result);
|
||||
|
||||
// Add AI-specific recovery suggestions if there are errors
|
||||
if (result.errors.length > 0) {
|
||||
this.addErrorRecoverySuggestions(result);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
@@ -308,7 +326,7 @@ export class WorkflowValidator {
|
||||
profile: string
|
||||
): Promise<void> {
|
||||
for (const node of workflow.nodes) {
|
||||
if (node.disabled) continue;
|
||||
if (node.disabled || this.isStickyNote(node)) continue;
|
||||
|
||||
try {
|
||||
// Validate node name length
|
||||
@@ -500,7 +518,8 @@ export class WorkflowValidator {
|
||||
*/
|
||||
private validateConnections(
|
||||
workflow: WorkflowJson,
|
||||
result: WorkflowValidationResult
|
||||
result: WorkflowValidationResult,
|
||||
profile: string = 'runtime'
|
||||
): void {
|
||||
const nodeMap = new Map(workflow.nodes.map(n => [n.name, n]));
|
||||
const nodeIdMap = new Map(workflow.nodes.map(n => [n.id, n]));
|
||||
@@ -591,9 +610,9 @@ export class WorkflowValidator {
|
||||
}
|
||||
});
|
||||
|
||||
// Check for orphaned nodes
|
||||
// Check for orphaned nodes (exclude sticky notes)
|
||||
for (const node of workflow.nodes) {
|
||||
if (node.disabled) continue;
|
||||
if (node.disabled || this.isStickyNote(node)) continue;
|
||||
|
||||
const normalizedType = node.type.replace('n8n-nodes-base.', 'nodes-base.');
|
||||
const isTrigger = normalizedType.toLowerCase().includes('trigger') ||
|
||||
@@ -612,8 +631,8 @@ export class WorkflowValidator {
|
||||
}
|
||||
}
|
||||
|
||||
// Check for cycles
|
||||
if (this.hasCycle(workflow)) {
|
||||
// Check for cycles (skip in minimal profile to reduce false positives)
|
||||
if (profile !== 'minimal' && this.hasCycle(workflow)) {
|
||||
result.errors.push({
|
||||
type: 'error',
|
||||
message: 'Workflow contains a cycle (infinite loop)'
|
||||
@@ -757,10 +776,22 @@ export class WorkflowValidator {
|
||||
const recursionStack = new Set<string>();
|
||||
const nodeTypeMap = new Map<string, string>();
|
||||
|
||||
// Build node type map
|
||||
// Build node type map (exclude sticky notes)
|
||||
workflow.nodes.forEach(node => {
|
||||
nodeTypeMap.set(node.name, node.type);
|
||||
if (!this.isStickyNote(node)) {
|
||||
nodeTypeMap.set(node.name, node.type);
|
||||
}
|
||||
});
|
||||
|
||||
// Known legitimate loop node types
|
||||
const loopNodeTypes = [
|
||||
'n8n-nodes-base.splitInBatches',
|
||||
'nodes-base.splitInBatches',
|
||||
'n8n-nodes-base.itemLists',
|
||||
'nodes-base.itemLists',
|
||||
'n8n-nodes-base.loop',
|
||||
'nodes-base.loop'
|
||||
];
|
||||
|
||||
const hasCycleDFS = (nodeName: string, pathFromLoopNode: boolean = false): boolean => {
|
||||
visited.add(nodeName);
|
||||
@@ -789,18 +820,18 @@ export class WorkflowValidator {
|
||||
}
|
||||
|
||||
const currentNodeType = nodeTypeMap.get(nodeName);
|
||||
const isLoopNode = currentNodeType === 'n8n-nodes-base.splitInBatches';
|
||||
const isLoopNode = loopNodeTypes.includes(currentNodeType || '');
|
||||
|
||||
for (const target of allTargets) {
|
||||
if (!visited.has(target)) {
|
||||
if (hasCycleDFS(target, pathFromLoopNode || isLoopNode)) return true;
|
||||
} else if (recursionStack.has(target)) {
|
||||
// Allow cycles that involve loop nodes like SplitInBatches
|
||||
// Allow cycles that involve legitimate loop nodes
|
||||
const targetNodeType = nodeTypeMap.get(target);
|
||||
const isTargetLoopNode = targetNodeType === 'n8n-nodes-base.splitInBatches';
|
||||
const isTargetLoopNode = loopNodeTypes.includes(targetNodeType || '');
|
||||
|
||||
// If this cycle involves a loop node, it's legitimate
|
||||
if (isTargetLoopNode || pathFromLoopNode) {
|
||||
if (isTargetLoopNode || pathFromLoopNode || isLoopNode) {
|
||||
continue; // Allow this cycle
|
||||
}
|
||||
|
||||
@@ -813,9 +844,9 @@ export class WorkflowValidator {
|
||||
return false;
|
||||
};
|
||||
|
||||
// Check from all nodes
|
||||
// Check from all executable nodes (exclude sticky notes)
|
||||
for (const node of workflow.nodes) {
|
||||
if (!visited.has(node.name)) {
|
||||
if (!this.isStickyNote(node) && !visited.has(node.name)) {
|
||||
if (hasCycleDFS(node.name)) return true;
|
||||
}
|
||||
}
|
||||
@@ -828,12 +859,13 @@ export class WorkflowValidator {
|
||||
*/
|
||||
private validateExpressions(
|
||||
workflow: WorkflowJson,
|
||||
result: WorkflowValidationResult
|
||||
result: WorkflowValidationResult,
|
||||
profile: string = 'runtime'
|
||||
): void {
|
||||
const nodeNames = workflow.nodes.map(n => n.name);
|
||||
|
||||
for (const node of workflow.nodes) {
|
||||
if (node.disabled) continue;
|
||||
if (node.disabled || this.isStickyNote(node)) continue;
|
||||
|
||||
// Create expression context
|
||||
const context = {
|
||||
@@ -922,23 +954,27 @@ export class WorkflowValidator {
|
||||
*/
|
||||
private checkWorkflowPatterns(
|
||||
workflow: WorkflowJson,
|
||||
result: WorkflowValidationResult
|
||||
result: WorkflowValidationResult,
|
||||
profile: string = 'runtime'
|
||||
): void {
|
||||
// Check for error handling
|
||||
const hasErrorHandling = Object.values(workflow.connections).some(
|
||||
outputs => outputs.error && outputs.error.length > 0
|
||||
);
|
||||
|
||||
if (!hasErrorHandling && workflow.nodes.length > 3) {
|
||||
// Only suggest error handling in stricter profiles
|
||||
if (!hasErrorHandling && workflow.nodes.length > 3 && profile !== 'minimal') {
|
||||
result.warnings.push({
|
||||
type: 'warning',
|
||||
message: 'Consider adding error handling to your workflow'
|
||||
});
|
||||
}
|
||||
|
||||
// Check node-level error handling properties for ALL nodes
|
||||
// Check node-level error handling properties for ALL executable nodes
|
||||
for (const node of workflow.nodes) {
|
||||
this.checkNodeErrorHandling(node, workflow, result);
|
||||
if (!this.isStickyNote(node)) {
|
||||
this.checkNodeErrorHandling(node, workflow, result);
|
||||
}
|
||||
}
|
||||
|
||||
// Check for very long linear workflows
|
||||
@@ -1641,4 +1677,75 @@ export class WorkflowValidator {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add AI-specific error recovery suggestions
|
||||
*/
|
||||
private addErrorRecoverySuggestions(result: WorkflowValidationResult): void {
|
||||
// Categorize errors and provide specific recovery actions
|
||||
const errorTypes = {
|
||||
nodeType: result.errors.filter(e => e.message.includes('node type') || e.message.includes('Node type')),
|
||||
connection: result.errors.filter(e => e.message.includes('connection') || e.message.includes('Connection')),
|
||||
structure: result.errors.filter(e => e.message.includes('structure') || e.message.includes('nodes must be')),
|
||||
configuration: result.errors.filter(e => e.message.includes('property') || e.message.includes('field')),
|
||||
typeVersion: result.errors.filter(e => e.message.includes('typeVersion'))
|
||||
};
|
||||
|
||||
// Add recovery suggestions based on error types
|
||||
if (errorTypes.nodeType.length > 0) {
|
||||
result.suggestions.unshift(
|
||||
'🔧 RECOVERY: Invalid node types detected. Use these patterns:',
|
||||
' • For core nodes: "n8n-nodes-base.nodeName" (e.g., "n8n-nodes-base.webhook")',
|
||||
' • For AI nodes: "@n8n/n8n-nodes-langchain.nodeName"',
|
||||
' • Never use just the node name without package prefix'
|
||||
);
|
||||
}
|
||||
|
||||
if (errorTypes.connection.length > 0) {
|
||||
result.suggestions.unshift(
|
||||
'🔧 RECOVERY: Connection errors detected. Fix with:',
|
||||
' • Use node NAMES in connections, not IDs or types',
|
||||
' • Structure: { "Source Node Name": { "main": [[{ "node": "Target Node Name", "type": "main", "index": 0 }]] } }',
|
||||
' • Ensure all referenced nodes exist in the workflow'
|
||||
);
|
||||
}
|
||||
|
||||
if (errorTypes.structure.length > 0) {
|
||||
result.suggestions.unshift(
|
||||
'🔧 RECOVERY: Workflow structure errors. Fix with:',
|
||||
' • Ensure "nodes" is an array: "nodes": [...]',
|
||||
' • Ensure "connections" is an object: "connections": {...}',
|
||||
' • Add at least one node to create a valid workflow'
|
||||
);
|
||||
}
|
||||
|
||||
if (errorTypes.configuration.length > 0) {
|
||||
result.suggestions.unshift(
|
||||
'🔧 RECOVERY: Node configuration errors. Fix with:',
|
||||
' • Check required fields using validate_node_minimal first',
|
||||
' • Use get_node_essentials to see what fields are needed',
|
||||
' • Ensure operation-specific fields match the node\'s requirements'
|
||||
);
|
||||
}
|
||||
|
||||
if (errorTypes.typeVersion.length > 0) {
|
||||
result.suggestions.unshift(
|
||||
'🔧 RECOVERY: TypeVersion errors. Fix with:',
|
||||
' • Add "typeVersion": 1 (or latest version) to each node',
|
||||
' • Use get_node_info to check the correct version for each node type'
|
||||
);
|
||||
}
|
||||
|
||||
// Add general recovery workflow
|
||||
if (result.errors.length > 3) {
|
||||
result.suggestions.push(
|
||||
'📋 SUGGESTED WORKFLOW: Too many errors detected. Try this approach:',
|
||||
' 1. Fix structural issues first (nodes array, connections object)',
|
||||
' 2. Validate node types and fix invalid ones',
|
||||
' 3. Add required typeVersion to all nodes',
|
||||
' 4. Test connections step by step',
|
||||
' 5. Use validate_node_minimal on individual nodes to verify configuration'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
312
src/utils/validation-schemas.ts
Normal file
312
src/utils/validation-schemas.ts
Normal file
@@ -0,0 +1,312 @@
|
||||
/**
|
||||
* Zod validation schemas for MCP tool parameters
|
||||
* Provides robust input validation with detailed error messages
|
||||
*/
|
||||
|
||||
// Simple validation without zod for now, since it's not installed
|
||||
// We can use TypeScript's built-in validation with better error messages
|
||||
|
||||
export class ValidationError extends Error {
|
||||
constructor(message: string, public field?: string, public value?: any) {
|
||||
super(message);
|
||||
this.name = 'ValidationError';
|
||||
}
|
||||
}
|
||||
|
||||
export interface ValidationResult {
|
||||
valid: boolean;
|
||||
errors: Array<{
|
||||
field: string;
|
||||
message: string;
|
||||
value?: any;
|
||||
}>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic validation utilities
|
||||
*/
|
||||
export class Validator {
|
||||
/**
|
||||
* Validate that a value is a non-empty string
|
||||
*/
|
||||
static validateString(value: any, fieldName: string, required: boolean = true): ValidationResult {
|
||||
const errors: Array<{field: string, message: string, value?: any}> = [];
|
||||
|
||||
if (required && (value === undefined || value === null)) {
|
||||
errors.push({
|
||||
field: fieldName,
|
||||
message: `${fieldName} is required`,
|
||||
value
|
||||
});
|
||||
} else if (value !== undefined && value !== null && typeof value !== 'string') {
|
||||
errors.push({
|
||||
field: fieldName,
|
||||
message: `${fieldName} must be a string, got ${typeof value}`,
|
||||
value
|
||||
});
|
||||
} else if (required && typeof value === 'string' && value.trim().length === 0) {
|
||||
errors.push({
|
||||
field: fieldName,
|
||||
message: `${fieldName} cannot be empty`,
|
||||
value
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
valid: errors.length === 0,
|
||||
errors
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a value is a valid object (not null, not array)
|
||||
*/
|
||||
static validateObject(value: any, fieldName: string, required: boolean = true): ValidationResult {
|
||||
const errors: Array<{field: string, message: string, value?: any}> = [];
|
||||
|
||||
if (required && (value === undefined || value === null)) {
|
||||
errors.push({
|
||||
field: fieldName,
|
||||
message: `${fieldName} is required`,
|
||||
value
|
||||
});
|
||||
} else if (value !== undefined && value !== null) {
|
||||
if (typeof value !== 'object') {
|
||||
errors.push({
|
||||
field: fieldName,
|
||||
message: `${fieldName} must be an object, got ${typeof value}`,
|
||||
value
|
||||
});
|
||||
} else if (Array.isArray(value)) {
|
||||
errors.push({
|
||||
field: fieldName,
|
||||
message: `${fieldName} must be an object, not an array`,
|
||||
value
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
valid: errors.length === 0,
|
||||
errors
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a value is an array
|
||||
*/
|
||||
static validateArray(value: any, fieldName: string, required: boolean = true): ValidationResult {
|
||||
const errors: Array<{field: string, message: string, value?: any}> = [];
|
||||
|
||||
if (required && (value === undefined || value === null)) {
|
||||
errors.push({
|
||||
field: fieldName,
|
||||
message: `${fieldName} is required`,
|
||||
value
|
||||
});
|
||||
} else if (value !== undefined && value !== null && !Array.isArray(value)) {
|
||||
errors.push({
|
||||
field: fieldName,
|
||||
message: `${fieldName} must be an array, got ${typeof value}`,
|
||||
value
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
valid: errors.length === 0,
|
||||
errors
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a value is a number
|
||||
*/
|
||||
static validateNumber(value: any, fieldName: string, required: boolean = true, min?: number, max?: number): ValidationResult {
|
||||
const errors: Array<{field: string, message: string, value?: any}> = [];
|
||||
|
||||
if (required && (value === undefined || value === null)) {
|
||||
errors.push({
|
||||
field: fieldName,
|
||||
message: `${fieldName} is required`,
|
||||
value
|
||||
});
|
||||
} else if (value !== undefined && value !== null) {
|
||||
if (typeof value !== 'number' || isNaN(value)) {
|
||||
errors.push({
|
||||
field: fieldName,
|
||||
message: `${fieldName} must be a number, got ${typeof value}`,
|
||||
value
|
||||
});
|
||||
} else {
|
||||
if (min !== undefined && value < min) {
|
||||
errors.push({
|
||||
field: fieldName,
|
||||
message: `${fieldName} must be at least ${min}, got ${value}`,
|
||||
value
|
||||
});
|
||||
}
|
||||
if (max !== undefined && value > max) {
|
||||
errors.push({
|
||||
field: fieldName,
|
||||
message: `${fieldName} must be at most ${max}, got ${value}`,
|
||||
value
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
valid: errors.length === 0,
|
||||
errors
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a value is one of allowed values
|
||||
*/
|
||||
static validateEnum<T>(value: any, fieldName: string, allowedValues: T[], required: boolean = true): ValidationResult {
|
||||
const errors: Array<{field: string, message: string, value?: any}> = [];
|
||||
|
||||
if (required && (value === undefined || value === null)) {
|
||||
errors.push({
|
||||
field: fieldName,
|
||||
message: `${fieldName} is required`,
|
||||
value
|
||||
});
|
||||
} else if (value !== undefined && value !== null && !allowedValues.includes(value)) {
|
||||
errors.push({
|
||||
field: fieldName,
|
||||
message: `${fieldName} must be one of: ${allowedValues.join(', ')}, got "${value}"`,
|
||||
value
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
valid: errors.length === 0,
|
||||
errors
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Combine multiple validation results
|
||||
*/
|
||||
static combineResults(...results: ValidationResult[]): ValidationResult {
|
||||
const allErrors = results.flatMap(r => r.errors);
|
||||
return {
|
||||
valid: allErrors.length === 0,
|
||||
errors: allErrors
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a detailed error message from validation result
|
||||
*/
|
||||
static formatErrors(result: ValidationResult, toolName?: string): string {
|
||||
if (result.valid) return '';
|
||||
|
||||
const prefix = toolName ? `${toolName}: ` : '';
|
||||
const errors = result.errors.map(e => ` • ${e.field}: ${e.message}`).join('\n');
|
||||
|
||||
return `${prefix}Validation failed:\n${errors}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tool-specific validation schemas
|
||||
*/
|
||||
export class ToolValidation {
|
||||
/**
|
||||
* Validate parameters for validate_node_operation tool
|
||||
*/
|
||||
static validateNodeOperation(args: any): ValidationResult {
|
||||
const nodeTypeResult = Validator.validateString(args.nodeType, 'nodeType');
|
||||
const configResult = Validator.validateObject(args.config, 'config');
|
||||
const profileResult = Validator.validateEnum(
|
||||
args.profile,
|
||||
'profile',
|
||||
['minimal', 'runtime', 'ai-friendly', 'strict'],
|
||||
false // optional
|
||||
);
|
||||
|
||||
return Validator.combineResults(nodeTypeResult, configResult, profileResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate parameters for validate_node_minimal tool
|
||||
*/
|
||||
static validateNodeMinimal(args: any): ValidationResult {
|
||||
const nodeTypeResult = Validator.validateString(args.nodeType, 'nodeType');
|
||||
const configResult = Validator.validateObject(args.config, 'config');
|
||||
|
||||
return Validator.combineResults(nodeTypeResult, configResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate parameters for validate_workflow tool
|
||||
*/
|
||||
static validateWorkflow(args: any): ValidationResult {
|
||||
const workflowResult = Validator.validateObject(args.workflow, 'workflow');
|
||||
|
||||
// Validate workflow structure if it's an object
|
||||
let nodesResult: ValidationResult = { valid: true, errors: [] };
|
||||
let connectionsResult: ValidationResult = { valid: true, errors: [] };
|
||||
|
||||
if (workflowResult.valid && args.workflow) {
|
||||
nodesResult = Validator.validateArray(args.workflow.nodes, 'workflow.nodes');
|
||||
connectionsResult = Validator.validateObject(args.workflow.connections, 'workflow.connections');
|
||||
}
|
||||
|
||||
const optionsResult = args.options ?
|
||||
Validator.validateObject(args.options, 'options', false) :
|
||||
{ valid: true, errors: [] };
|
||||
|
||||
return Validator.combineResults(workflowResult, nodesResult, connectionsResult, optionsResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate parameters for search_nodes tool
|
||||
*/
|
||||
static validateSearchNodes(args: any): ValidationResult {
|
||||
const queryResult = Validator.validateString(args.query, 'query');
|
||||
const limitResult = Validator.validateNumber(args.limit, 'limit', false, 1, 200);
|
||||
const modeResult = Validator.validateEnum(
|
||||
args.mode,
|
||||
'mode',
|
||||
['OR', 'AND', 'FUZZY'],
|
||||
false
|
||||
);
|
||||
|
||||
return Validator.combineResults(queryResult, limitResult, modeResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate parameters for list_node_templates tool
|
||||
*/
|
||||
static validateListNodeTemplates(args: any): ValidationResult {
|
||||
const nodeTypesResult = Validator.validateArray(args.nodeTypes, 'nodeTypes');
|
||||
const limitResult = Validator.validateNumber(args.limit, 'limit', false, 1, 50);
|
||||
|
||||
return Validator.combineResults(nodeTypesResult, limitResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate parameters for n8n workflow operations
|
||||
*/
|
||||
static validateWorkflowId(args: any): ValidationResult {
|
||||
return Validator.validateString(args.id, 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate parameters for n8n_create_workflow tool
|
||||
*/
|
||||
static validateCreateWorkflow(args: any): ValidationResult {
|
||||
const nameResult = Validator.validateString(args.name, 'name');
|
||||
const nodesResult = Validator.validateArray(args.nodes, 'nodes');
|
||||
const connectionsResult = Validator.validateObject(args.connections, 'connections');
|
||||
const settingsResult = args.settings ?
|
||||
Validator.validateObject(args.settings, 'settings', false) :
|
||||
{ valid: true, errors: [] };
|
||||
|
||||
return Validator.combineResults(nameResult, nodesResult, connectionsResult, settingsResult);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user