| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674 |
- /**
- * Configuration Schema Validator Implementation
- *
- * Validates credential configuration files against defined schemas
- * to ensure data integrity and proper format before loading.
- * Supports JSON and YAML configuration formats.
- */
- import { ConfigFile } from '@/types/credential'
- // ============================================================================
- // Types and Interfaces
- // ============================================================================
- export interface ValidationResult {
- valid: boolean
- errors: ValidationError[]
- warnings: ValidationWarning[]
- normalizedConfig?: ConfigFile
- }
- export interface ValidationError {
- path: string
- message: string
- code: ValidationErrorCode
- value?: any
- }
- export interface ValidationWarning {
- path: string
- message: string
- code: ValidationWarningCode
- value?: any
- }
- export enum ValidationErrorCode {
- MISSING_REQUIRED_FIELD = 'MISSING_REQUIRED_FIELD',
- INVALID_TYPE = 'INVALID_TYPE',
- INVALID_FORMAT = 'INVALID_FORMAT',
- INVALID_ENUM_VALUE = 'INVALID_ENUM_VALUE',
- DUPLICATE_ACCOUNT_ID = 'DUPLICATE_ACCOUNT_ID',
- INVALID_CREDENTIALS = 'INVALID_CREDENTIALS',
- UNSUPPORTED_VERSION = 'UNSUPPORTED_VERSION',
- INVALID_PLATFORM = 'INVALID_PLATFORM',
- }
- export enum ValidationWarningCode {
- DEPRECATED_FIELD = 'DEPRECATED_FIELD',
- MISSING_OPTIONAL_FIELD = 'MISSING_OPTIONAL_FIELD',
- UNUSUAL_VALUE = 'UNUSUAL_VALUE',
- PERFORMANCE_CONCERN = 'PERFORMANCE_CONCERN',
- }
- export interface ValidatorOptions {
- strictMode?: boolean
- allowUnknownFields?: boolean
- normalizeData?: boolean
- validateCredentials?: boolean
- maxAccounts?: number
- }
- // ============================================================================
- // Schema Definitions
- // ============================================================================
- const SUPPORTED_VERSIONS = ['1.0']
- const PACIFICA_PRIVATE_KEY_PATTERN = /^[0-9a-fA-F]{64}$/
- const ASTER_PRIVATE_KEY_PATTERN = /^0x[0-9a-fA-F]{64}$/
- const ETHEREUM_ADDRESS_PATTERN = /^0x[0-9a-fA-F]{40}$/
- const BINANCE_API_KEY_PATTERN = /^[A-Za-z0-9]{64}$/
- // ============================================================================
- // Configuration Validator Implementation
- // ============================================================================
- export class ConfigValidator {
- private options: Required<ValidatorOptions>
- constructor(options: ValidatorOptions = {}) {
- this.options = {
- strictMode: options.strictMode ?? false,
- allowUnknownFields: options.allowUnknownFields ?? true,
- normalizeData: options.normalizeData ?? true,
- validateCredentials: options.validateCredentials ?? true,
- maxAccounts: options.maxAccounts ?? 1000,
- }
- }
- /**
- * Validate configuration file content
- */
- validateConfig(config: any): ValidationResult {
- const errors: ValidationError[] = []
- const warnings: ValidationWarning[] = []
- let normalizedConfig: ConfigFile | undefined
- try {
- // Basic structure validation
- this.validateBasicStructure(config, errors)
- if (errors.length > 0 && this.options.strictMode) {
- return { valid: false, errors, warnings }
- }
- // Version validation
- this.validateVersion(config.version, errors, warnings)
- // Accounts validation
- if (config.accounts) {
- this.validateAccounts(config.accounts, errors, warnings)
- }
- // Normalize data if requested and no critical errors
- if (this.options.normalizeData && errors.length === 0) {
- normalizedConfig = this.normalizeConfig(config)
- }
- const valid = errors.length === 0
- return {
- valid,
- errors,
- warnings,
- normalizedConfig,
- }
- } catch (error) {
- errors.push({
- path: 'root',
- message: `Validation failed: ${error.message}`,
- code: ValidationErrorCode.INVALID_FORMAT,
- })
- return { valid: false, errors, warnings }
- }
- }
- /**
- * Validate a single account configuration
- */
- validateAccount(account: any, accountIndex?: number): ValidationResult {
- const errors: ValidationError[] = []
- const warnings: ValidationWarning[] = []
- const pathPrefix = accountIndex !== undefined ? `accounts[${accountIndex}]` : 'account'
- this.validateSingleAccount(account, pathPrefix, errors, warnings)
- return {
- valid: errors.length === 0,
- errors,
- warnings,
- }
- }
- /**
- * Validate credentials for a specific platform
- */
- validateCredentials(platform: Platform, credentials: any): ValidationResult {
- const errors: ValidationError[] = []
- const warnings: ValidationWarning[] = []
- this.validateCredentialsByPlatform(platform, credentials, 'credentials', errors, warnings)
- return {
- valid: errors.length === 0,
- errors,
- warnings,
- }
- }
- /**
- * Quick validation check (returns boolean only)
- */
- isValid(config: any): boolean {
- const result = this.validateConfig(config)
- return result.valid
- }
- // ============================================================================
- // Private Validation Methods
- // ============================================================================
- /**
- * Validate basic configuration structure
- */
- private validateBasicStructure(config: any, errors: ValidationError[]): void {
- if (typeof config !== 'object' || config === null) {
- errors.push({
- path: 'root',
- message: 'Configuration must be an object',
- code: ValidationErrorCode.INVALID_TYPE,
- value: typeof config,
- })
- return
- }
- // Required fields
- if (!config.version) {
- errors.push({
- path: 'version',
- message: 'Version field is required',
- code: ValidationErrorCode.MISSING_REQUIRED_FIELD,
- })
- }
- if (!config.accounts) {
- errors.push({
- path: 'accounts',
- message: 'Accounts field is required',
- code: ValidationErrorCode.MISSING_REQUIRED_FIELD,
- })
- } else if (!Array.isArray(config.accounts)) {
- errors.push({
- path: 'accounts',
- message: 'Accounts must be an array',
- code: ValidationErrorCode.INVALID_TYPE,
- value: typeof config.accounts,
- })
- }
- }
- /**
- * Validate configuration version
- */
- private validateVersion(version: any, errors: ValidationError[], warnings: ValidationWarning[]): void {
- if (typeof version !== 'string') {
- errors.push({
- path: 'version',
- message: 'Version must be a string',
- code: ValidationErrorCode.INVALID_TYPE,
- value: typeof version,
- })
- return
- }
- if (!SUPPORTED_VERSIONS.includes(version)) {
- errors.push({
- path: 'version',
- message: `Unsupported version: ${version}. Supported versions: ${SUPPORTED_VERSIONS.join(', ')}`,
- code: ValidationErrorCode.UNSUPPORTED_VERSION,
- value: version,
- })
- }
- }
- /**
- * Validate accounts array
- */
- private validateAccounts(accounts: any[], errors: ValidationError[], warnings: ValidationWarning[]): void {
- if (accounts.length === 0) {
- warnings.push({
- path: 'accounts',
- message: 'No accounts defined',
- code: ValidationWarningCode.UNUSUAL_VALUE,
- value: 0,
- })
- return
- }
- if (accounts.length > this.options.maxAccounts) {
- errors.push({
- path: 'accounts',
- message: `Too many accounts: ${accounts.length}. Maximum allowed: ${this.options.maxAccounts}`,
- code: ValidationErrorCode.INVALID_FORMAT,
- value: accounts.length,
- })
- }
- // Check for duplicate account IDs
- const accountIds = new Set<string>()
- const duplicates = new Set<string>()
- accounts.forEach((account, index) => {
- if (account && account.id) {
- if (accountIds.has(account.id)) {
- duplicates.add(account.id)
- }
- accountIds.add(account.id)
- }
- this.validateSingleAccount(account, `accounts[${index}]`, errors, warnings)
- })
- // Report duplicate IDs
- duplicates.forEach(id => {
- errors.push({
- path: 'accounts',
- message: `Duplicate account ID: ${id}`,
- code: ValidationErrorCode.DUPLICATE_ACCOUNT_ID,
- value: id,
- })
- })
- // Performance warning for large number of accounts
- if (accounts.length > 100) {
- warnings.push({
- path: 'accounts',
- message: `Large number of accounts (${accounts.length}) may impact performance`,
- code: ValidationWarningCode.PERFORMANCE_CONCERN,
- value: accounts.length,
- })
- }
- }
- /**
- * Validate a single account
- */
- private validateSingleAccount(
- account: any,
- pathPrefix: string,
- errors: ValidationError[],
- warnings: ValidationWarning[],
- ): void {
- if (typeof account !== 'object' || account === null) {
- errors.push({
- path: pathPrefix,
- message: 'Account must be an object',
- code: ValidationErrorCode.INVALID_TYPE,
- value: typeof account,
- })
- return
- }
- // Required fields
- if (!account.id) {
- errors.push({
- path: `${pathPrefix}.id`,
- message: 'Account ID is required',
- code: ValidationErrorCode.MISSING_REQUIRED_FIELD,
- })
- } else if (typeof account.id !== 'string') {
- errors.push({
- path: `${pathPrefix}.id`,
- message: 'Account ID must be a string',
- code: ValidationErrorCode.INVALID_TYPE,
- value: typeof account.id,
- })
- } else if (account.id.length === 0) {
- errors.push({
- path: `${pathPrefix}.id`,
- message: 'Account ID cannot be empty',
- code: ValidationErrorCode.INVALID_FORMAT,
- value: account.id,
- })
- }
- // Platform validation
- if (!account.platform) {
- errors.push({
- path: `${pathPrefix}.platform`,
- message: 'Platform is required',
- code: ValidationErrorCode.MISSING_REQUIRED_FIELD,
- })
- } else if (!Object.values(Platform).includes(account.platform)) {
- errors.push({
- path: `${pathPrefix}.platform`,
- message: `Invalid platform: ${account.platform}. Valid platforms: ${Object.values(Platform).join(', ')}`,
- code: ValidationErrorCode.INVALID_PLATFORM,
- value: account.platform,
- })
- }
- // Credentials validation
- if (!account.credentials) {
- errors.push({
- path: `${pathPrefix}.credentials`,
- message: 'Credentials are required',
- code: ValidationErrorCode.MISSING_REQUIRED_FIELD,
- })
- } else if (this.options.validateCredentials && account.platform) {
- this.validateCredentialsByPlatform(
- account.platform,
- account.credentials,
- `${pathPrefix}.credentials`,
- errors,
- warnings,
- )
- }
- // Metadata validation (optional)
- if (account.metadata !== undefined) {
- if (typeof account.metadata !== 'object' || account.metadata === null) {
- errors.push({
- path: `${pathPrefix}.metadata`,
- message: 'Metadata must be an object',
- code: ValidationErrorCode.INVALID_TYPE,
- value: typeof account.metadata,
- })
- }
- }
- // Check for unknown fields in strict mode
- if (this.options.strictMode && !this.options.allowUnknownFields) {
- const allowedFields = ['id', 'platform', 'credentials', 'metadata']
- const unknownFields = Object.keys(account).filter(key => !allowedFields.includes(key))
- unknownFields.forEach(field => {
- errors.push({
- path: `${pathPrefix}.${field}`,
- message: `Unknown field: ${field}`,
- code: ValidationErrorCode.INVALID_FORMAT,
- value: account[field],
- })
- })
- }
- }
- /**
- * Validate credentials based on platform
- */
- private validateCredentialsByPlatform(
- platform: Platform,
- credentials: any,
- pathPrefix: string,
- errors: ValidationError[],
- warnings: ValidationWarning[],
- ): void {
- if (typeof credentials !== 'object' || credentials === null) {
- errors.push({
- path: pathPrefix,
- message: 'Credentials must be an object',
- code: ValidationErrorCode.INVALID_TYPE,
- value: typeof credentials,
- })
- return
- }
- switch (platform) {
- case Platform.PACIFICA:
- this.validatePacificaCredentials(credentials, pathPrefix, errors, warnings)
- break
- case Platform.ASTER:
- this.validateAsterCredentials(credentials, pathPrefix, errors, warnings)
- break
- case Platform.BINANCE:
- this.validateBinanceCredentials(credentials, pathPrefix, errors, warnings)
- break
- default:
- errors.push({
- path: pathPrefix,
- message: `Unsupported platform for credential validation: ${platform}`,
- code: ValidationErrorCode.INVALID_PLATFORM,
- value: platform,
- })
- }
- }
- /**
- * Validate Pacifica platform credentials
- */
- private validatePacificaCredentials(
- credentials: any,
- pathPrefix: string,
- errors: ValidationError[],
- warnings: ValidationWarning[],
- ): void {
- if (credentials.type !== 'pacifica') {
- errors.push({
- path: `${pathPrefix}.type`,
- message: 'Pacifica credentials must have type "pacifica"',
- code: ValidationErrorCode.INVALID_FORMAT,
- value: credentials.type,
- })
- }
- if (!credentials.privateKey) {
- errors.push({
- path: `${pathPrefix}.privateKey`,
- message: 'Pacifica private key is required',
- code: ValidationErrorCode.MISSING_REQUIRED_FIELD,
- })
- } else if (typeof credentials.privateKey !== 'string') {
- errors.push({
- path: `${pathPrefix}.privateKey`,
- message: 'Pacifica private key must be a string',
- code: ValidationErrorCode.INVALID_TYPE,
- value: typeof credentials.privateKey,
- })
- } else if (!PACIFICA_PRIVATE_KEY_PATTERN.test(credentials.privateKey)) {
- errors.push({
- path: `${pathPrefix}.privateKey`,
- message: 'Pacifica private key must be 64 hexadecimal characters',
- code: ValidationErrorCode.INVALID_FORMAT,
- value: `${credentials.privateKey.length} characters`,
- })
- }
- }
- /**
- * Validate Aster platform credentials
- */
- private validateAsterCredentials(
- credentials: any,
- pathPrefix: string,
- errors: ValidationError[],
- warnings: ValidationWarning[],
- ): void {
- if (credentials.type !== 'aster') {
- errors.push({
- path: `${pathPrefix}.type`,
- message: 'Aster credentials must have type "aster"',
- code: ValidationErrorCode.INVALID_FORMAT,
- value: credentials.type,
- })
- }
- if (!credentials.privateKey) {
- errors.push({
- path: `${pathPrefix}.privateKey`,
- message: 'Aster private key is required',
- code: ValidationErrorCode.MISSING_REQUIRED_FIELD,
- })
- } else if (typeof credentials.privateKey !== 'string') {
- errors.push({
- path: `${pathPrefix}.privateKey`,
- message: 'Aster private key must be a string',
- code: ValidationErrorCode.INVALID_TYPE,
- value: typeof credentials.privateKey,
- })
- } else if (!ASTER_PRIVATE_KEY_PATTERN.test(credentials.privateKey)) {
- errors.push({
- path: `${pathPrefix}.privateKey`,
- message: 'Aster private key must be 66 characters starting with "0x" followed by 64 hexadecimal characters',
- code: ValidationErrorCode.INVALID_FORMAT,
- value: `${credentials.privateKey.length} characters`,
- })
- }
- }
- /**
- * Validate Binance platform credentials
- */
- private validateBinanceCredentials(
- credentials: any,
- pathPrefix: string,
- errors: ValidationError[],
- warnings: ValidationWarning[],
- ): void {
- if (credentials.type !== 'binance') {
- errors.push({
- path: `${pathPrefix}.type`,
- message: 'Binance credentials must have type "binance"',
- code: ValidationErrorCode.INVALID_FORMAT,
- value: credentials.type,
- })
- }
- if (!credentials.apiKey) {
- errors.push({
- path: `${pathPrefix}.apiKey`,
- message: 'Binance API key is required',
- code: ValidationErrorCode.MISSING_REQUIRED_FIELD,
- })
- } else if (typeof credentials.apiKey !== 'string') {
- errors.push({
- path: `${pathPrefix}.apiKey`,
- message: 'Binance API key must be a string',
- code: ValidationErrorCode.INVALID_TYPE,
- value: typeof credentials.apiKey,
- })
- } else if (!BINANCE_API_KEY_PATTERN.test(credentials.apiKey)) {
- errors.push({
- path: `${pathPrefix}.apiKey`,
- message: 'Binance API key must be 64 alphanumeric characters',
- code: ValidationErrorCode.INVALID_FORMAT,
- value: `${credentials.apiKey.length} characters`,
- })
- }
- if (!credentials.secretKey) {
- errors.push({
- path: `${pathPrefix}.secretKey`,
- message: 'Binance secret key is required',
- code: ValidationErrorCode.MISSING_REQUIRED_FIELD,
- })
- } else if (typeof credentials.secretKey !== 'string') {
- errors.push({
- path: `${pathPrefix}.secretKey`,
- message: 'Binance secret key must be a string',
- code: ValidationErrorCode.INVALID_TYPE,
- value: typeof credentials.secretKey,
- })
- } else if (credentials.secretKey.length === 0) {
- errors.push({
- path: `${pathPrefix}.secretKey`,
- message: 'Binance secret key cannot be empty',
- code: ValidationErrorCode.INVALID_FORMAT,
- value: credentials.secretKey,
- })
- }
- }
- /**
- * Normalize configuration data
- */
- private normalizeConfig(config: any): ConfigFile {
- const normalized: ConfigFile = {
- version: config.version,
- accounts: [],
- }
- if (Array.isArray(config.accounts)) {
- normalized.accounts = config.accounts.map((account: any) => ({
- id: account.id,
- platform: account.platform,
- credentials: account.credentials,
- metadata: account.metadata || {},
- }))
- }
- return normalized
- }
- }
- // ============================================================================
- // Utility Functions
- // ============================================================================
- /**
- * Create a validator with default options
- */
- export function createValidator(options?: ValidatorOptions): ConfigValidator {
- return new ConfigValidator(options)
- }
- /**
- * Create a strict validator for production use
- */
- export function createStrictValidator(): ConfigValidator {
- return new ConfigValidator({
- strictMode: true,
- allowUnknownFields: false,
- normalizeData: true,
- validateCredentials: true,
- maxAccounts: 500,
- })
- }
- /**
- * Create a lenient validator for development
- */
- export function createLenientValidator(): ConfigValidator {
- return new ConfigValidator({
- strictMode: false,
- allowUnknownFields: true,
- normalizeData: true,
- validateCredentials: false,
- maxAccounts: 1000,
- })
- }
- /**
- * Quick validation function
- */
- export function validateConfig(config: any, options?: ValidatorOptions): ValidationResult {
- const validator = new ConfigValidator(options)
- return validator.validateConfig(config)
- }
- /**
- * Quick validation check (boolean only)
- */
- export function isValidConfig(config: any, options?: ValidatorOptions): boolean {
- const validator = new ConfigValidator(options)
- return validator.isValid(config)
- }
- // ============================================================================
- // Export
- // ============================================================================
- export default ConfigValidator
|