All files / src/core/config validator.ts

0% Statements 0/209
0% Branches 0/1
0% Functions 0/1
0% Lines 0/209

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                                                                                                                                                                                   
/**
 * V3 Configuration Validator
 * Validation logic using Zod schemas
 */

import { z, type ZodError } from 'zod';
import {
  AgentConfigSchema,
  TaskConfigSchema,
  SwarmConfigSchema,
  MemoryConfigSchema,
  MCPServerConfigSchema,
  OrchestratorConfigSchema,
  SystemConfigSchema,
  type AgentConfig,
  type TaskConfig,
  type SwarmConfig,
  type MemoryConfig,
  type MCPServerConfig,
  type OrchestratorConfig,
  type SystemConfig,
} from './schema.js';

/**
 * Validation result
 */
export interface ValidationResult<T> {
  success: boolean;
  data?: T;
  errors?: ValidationError[];
}

/**
 * Validation error
 */
export interface ValidationError {
  path: string;
  message: string;
  code: string;
}

/**
 * Convert Zod error to validation errors
 */
function zodErrorToValidationErrors(error: ZodError): ValidationError[] {
  return error.errors.map((e) => ({
    path: e.path.join('.'),
    message: e.message,
    code: e.code,
  }));
}

/**
 * Generic validation function
 * Uses parse + try/catch to get output types with defaults applied
 */
function validate<TInput, TOutput>(
  schema: z.ZodType<TOutput, z.ZodTypeDef, TInput>,
  data: unknown
): ValidationResult<TOutput> {
  try {
    const parsed = schema.parse(data);
    return {
      success: true,
      data: parsed,
    };
  } catch (error) {
    if (error instanceof z.ZodError) {
      return {
        success: false,
        errors: zodErrorToValidationErrors(error),
      };
    }
    throw error;
  }
}

/**
 * Validate agent configuration
 */
export function validateAgentConfig(data: unknown): ValidationResult<AgentConfig> {
  return validate(AgentConfigSchema, data);
}

/**
 * Validate task configuration
 */
export function validateTaskConfig(data: unknown): ValidationResult<TaskConfig> {
  return validate(TaskConfigSchema, data);
}

/**
 * Validate swarm configuration
 */
export function validateSwarmConfig(data: unknown): ValidationResult<SwarmConfig> {
  return validate(SwarmConfigSchema, data);
}

/**
 * Validate memory configuration
 */
export function validateMemoryConfig(data: unknown): ValidationResult<MemoryConfig> {
  return validate(MemoryConfigSchema, data);
}

/**
 * Validate MCP server configuration
 */
export function validateMCPServerConfig(data: unknown): ValidationResult<MCPServerConfig> {
  return validate(MCPServerConfigSchema, data);
}

/**
 * Validate orchestrator configuration
 */
export function validateOrchestratorConfig(data: unknown): ValidationResult<OrchestratorConfig> {
  return validate(OrchestratorConfigSchema, data);
}

/**
 * Validate full system configuration
 */
export function validateSystemConfig(data: unknown): ValidationResult<SystemConfig> {
  return validate(SystemConfigSchema, data);
}

/**
 * Configuration validator class
 */
export class ConfigValidator {
  /**
   * Validate and throw on error
   */
  static validateOrThrow<TInput, TOutput>(
    schema: z.ZodType<TOutput, z.ZodTypeDef, TInput>,
    data: unknown,
    configName: string
  ): TOutput {
    const result = validate(schema, data);

    if (!result.success) {
      const errorMessages = result.errors
        ?.map((e) => `  - ${e.path}: ${e.message}`)
        .join('\n');
      throw new Error(`Invalid ${configName} configuration:\n${errorMessages}`);
    }

    return result.data!;
  }

  /**
   * Validate agent config or throw
   */
  static validateAgentOrThrow(data: unknown): AgentConfig {
    return this.validateOrThrow(AgentConfigSchema, data, 'agent');
  }

  /**
   * Validate task config or throw
   */
  static validateTaskOrThrow(data: unknown): TaskConfig {
    return this.validateOrThrow(TaskConfigSchema, data, 'task');
  }

  /**
   * Validate swarm config or throw
   */
  static validateSwarmOrThrow(data: unknown): SwarmConfig {
    return this.validateOrThrow(SwarmConfigSchema, data, 'swarm');
  }

  /**
   * Validate memory config or throw
   */
  static validateMemoryOrThrow(data: unknown): MemoryConfig {
    return this.validateOrThrow(MemoryConfigSchema, data, 'memory');
  }

  /**
   * Validate MCP server config or throw
   */
  static validateMCPServerOrThrow(data: unknown): MCPServerConfig {
    return this.validateOrThrow(MCPServerConfigSchema, data, 'MCP server');
  }

  /**
   * Validate orchestrator config or throw
   */
  static validateOrchestratorOrThrow(data: unknown): OrchestratorConfig {
    return this.validateOrThrow(OrchestratorConfigSchema, data, 'orchestrator');
  }

  /**
   * Validate system config or throw
   */
  static validateSystemOrThrow(data: unknown): SystemConfig {
    return this.validateOrThrow(SystemConfigSchema, data, 'system');
  }

  /**
   * Check if data matches schema
   */
  static isValid<TInput, TOutput>(
    schema: z.ZodType<TOutput, z.ZodTypeDef, TInput>,
    data: unknown
  ): boolean {
    return validate(schema, data).success;
  }
}