{"version":3,"file":"index.cjs","names":["ConfigurationError"],"sources":["../../src/validation/index.ts"],"sourcesContent":["/**\n * Configuration validation utilities for the Eclesia indexer\n * Validates configuration parameters before initialization\n */\n\nimport * as fs from \"node:fs\";\n\nimport {\n  ConfigurationError,\n} from \"../errors/index.js\";\n\n/**\n * Validates a URL string\n * @param url - The URL to validate\n * @param fieldName - Name of the field for error messages\n * @throws {ConfigurationError} If URL is invalid\n */\nexport function validateUrl(url: string, fieldName: string = \"URL\"): void {\n  if (!url || typeof url !== \"string\") {\n    throw new ConfigurationError(`${fieldName} is required and must be a string`, {\n      fieldName,\n      value: url,\n    });\n  }\n\n  try {\n    const parsed = new URL(url);\n\n    // Ensure protocol is http or https\n    if (parsed.protocol !== \"http:\" && parsed.protocol !== \"https:\" && parsed.protocol !== \"ws:\" && parsed.protocol !== \"wss:\") {\n      throw new ConfigurationError(`${fieldName} must use http, https, ws, or wss protocol`, {\n        fieldName,\n        url,\n        protocol: parsed.protocol,\n      });\n    }\n\n    // Ensure hostname is present\n    if (!parsed.hostname) {\n      throw new ConfigurationError(`${fieldName} must include a hostname`, {\n        fieldName,\n        url,\n      });\n    }\n  }\n  catch (error) {\n    if (error instanceof ConfigurationError) {\n      throw error;\n    }\n    throw new ConfigurationError(`${fieldName} is not a valid URL: ${error}`, {\n      fieldName,\n      url,\n    });\n  }\n}\n\n/**\n * Validates a file path exists and is readable\n * @param filePath - The file path to validate\n * @param fieldName - Name of the field for error messages\n * @throws {ConfigurationError} If file doesn't exist or isn't readable\n */\nexport function validateFilePath(filePath: string, fieldName: string = \"File path\"): void {\n  if (!filePath || typeof filePath !== \"string\") {\n    throw new ConfigurationError(`${fieldName} is required and must be a string`, {\n      fieldName,\n      value: filePath,\n    });\n  }\n\n  if (!fs.existsSync(filePath)) {\n    throw new ConfigurationError(`${fieldName} does not exist: ${filePath}`, {\n      fieldName,\n      filePath,\n    });\n  }\n\n  try {\n    fs.accessSync(filePath, fs.constants.R_OK);\n  }\n  catch (error) {\n    throw new ConfigurationError(`${fieldName} is not readable: ${filePath}`, {\n      fieldName,\n      filePath,\n      error: String(error),\n    });\n  }\n}\n\n/**\n * Validates a PostgreSQL connection string\n * @param connectionString - The connection string to validate\n * @param fieldName - Name of the field for error messages\n * @throws {ConfigurationError} If connection string is invalid\n */\nexport function validatePostgresConnectionString(\n  connectionString: string,\n  fieldName: string = \"Database connection string\",\n): void {\n  if (!connectionString || typeof connectionString !== \"string\") {\n    throw new ConfigurationError(`${fieldName} is required and must be a string`, {\n      fieldName,\n      value: connectionString,\n    });\n  }\n\n  // Basic PostgreSQL connection string format validation\n  // Format: postgres://user:password@host:port/database\n  const pgRegex = /^postgres(?:ql)?:\\/\\/(?:([^:]+)(?::([^@]+))?@)?([^:/]+)(?::(\\d+))?\\/(.+)$/;\n\n  if (!pgRegex.test(connectionString)) {\n    throw new ConfigurationError(\n      `${fieldName} must be in format: postgres://user:password@host:port/database`,\n      {\n        fieldName,\n        format: \"postgres://user:password@host:port/database\",\n      },\n    );\n  }\n}\n\n/**\n * Validates a port number\n * @param port - The port number to validate\n * @param fieldName - Name of the field for error messages\n * @throws {ConfigurationError} If port is invalid\n */\nexport function validatePort(port: number, fieldName: string = \"Port\"): void {\n  if (typeof port !== \"number\" || !Number.isInteger(port)) {\n    throw new ConfigurationError(`${fieldName} must be an integer`, {\n      fieldName,\n      value: port,\n    });\n  }\n\n  if (port < 1 || port > 65535) {\n    throw new ConfigurationError(`${fieldName} must be between 1 and 65535`, {\n      fieldName,\n      value: port,\n      min: 1,\n      max: 65535,\n    });\n  }\n}\n\n/**\n * Validates a positive integer\n * @param value - The value to validate\n * @param fieldName - Name of the field for error messages\n * @throws {ConfigurationError} If value is not a positive integer\n */\nexport function validatePositiveInteger(value: number, fieldName: string = \"Value\"): void {\n  if (typeof value !== \"number\" || !Number.isInteger(value) || value <= 0) {\n    throw new ConfigurationError(`${fieldName} must be a positive integer`, {\n      fieldName,\n      value,\n    });\n  }\n}\n\n/**\n * Gets and validates the chain prefix from environment variables\n * @param defaultPrefix - Default prefix to use if environment variable is not set\n * @returns The validated chain prefix\n * @throws {ConfigurationError} If chain prefix is invalid\n */\nexport function getChainPrefix(defaultPrefix: string = \"cosmos\"): string {\n  const prefix = process.env.CHAIN_PREFIX ?? defaultPrefix;\n\n  if (!prefix || typeof prefix !== \"string\") {\n    throw new ConfigurationError(\"CHAIN_PREFIX must be a non-empty string\", {\n      value: prefix,\n      defaultPrefix,\n    });\n  }\n\n  // Validate prefix format (lowercase alphanumeric)\n  if (!/^[a-z][a-z0-9]*$/.test(prefix)) {\n    throw new ConfigurationError(\n      \"CHAIN_PREFIX must start with lowercase letter and contain only lowercase letters and numbers\",\n      {\n        value: prefix,\n        pattern: \"^[a-z][a-z0-9]*$\",\n      },\n    );\n  }\n\n  return prefix;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAiBA,SAAgB,YAAY,KAAa,YAAoB,OAAa;AACxE,KAAI,CAAC,OAAO,OAAO,QAAQ,SACzB,OAAM,IAAIA,iCAAmB,GAAG,UAAU,oCAAoC;EAC5E;EACA,OAAO;EACR,CAAC;AAGJ,KAAI;EACF,MAAM,SAAS,IAAI,IAAI,IAAI;AAG3B,MAAI,OAAO,aAAa,WAAW,OAAO,aAAa,YAAY,OAAO,aAAa,SAAS,OAAO,aAAa,OAClH,OAAM,IAAIA,iCAAmB,GAAG,UAAU,6CAA6C;GACrF;GACA;GACA,UAAU,OAAO;GAClB,CAAC;AAIJ,MAAI,CAAC,OAAO,SACV,OAAM,IAAIA,iCAAmB,GAAG,UAAU,2BAA2B;GACnE;GACA;GACD,CAAC;UAGC,OAAO;AACZ,MAAI,iBAAiBA,iCACnB,OAAM;AAER,QAAM,IAAIA,iCAAmB,GAAG,UAAU,uBAAuB,SAAS;GACxE;GACA;GACD,CAAC;;;;;;;;;AAUN,SAAgB,iBAAiB,UAAkB,YAAoB,aAAmB;AACxF,KAAI,CAAC,YAAY,OAAO,aAAa,SACnC,OAAM,IAAIA,iC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