UNPKG

code-transmute

Version:

Convert any codebase into any language — without changing its brain.

211 lines 8.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ValidationUtils = void 0; class ValidationUtils { static validateProjectPath(projectPath) { const errors = []; if (!projectPath || projectPath.trim() === '') { errors.push('Project path is required'); } // Additional validation would check if path exists, is readable, etc. return errors; } static validateApiKey(apiKey) { const errors = []; if (!apiKey || apiKey.trim() === '') { errors.push('API key is required'); } else if (!apiKey.startsWith('sk-')) { errors.push('OpenAI API key must start with "sk-"'); } else if (apiKey.length < 20) { errors.push('API key appears to be too short'); } return errors; } static validateTargetLanguage(language) { const errors = []; const supportedLanguages = [ 'javascript', 'typescript', 'python', 'java', 'csharp', 'go', 'rust', 'php', 'ruby', 'swift', 'kotlin', 'dart', 'cpp', 'c' ]; if (!language || language.trim() === '') { errors.push('Target language is required'); } else if (!supportedLanguages.includes(language.toLowerCase())) { errors.push(`Unsupported target language: ${language}. Supported languages: ${supportedLanguages.join(', ')}`); } return errors; } static validateTargetFramework(framework, language) { const errors = []; if (!framework) { return errors; // Framework is optional } const frameworkMap = { typescript: ['express', 'nestjs', 'fastify', 'react', 'nextjs', 'vue', 'angular'], javascript: ['express', 'fastify', 'react', 'vue'], python: ['django', 'fastapi', 'flask', 'streamlit'], java: ['spring-boot', 'quarkus', 'micronaut'], csharp: ['aspnet-core', 'blazor', 'wpf'], go: ['gin', 'echo', 'fiber'], rust: ['actix-web', 'rocket', 'warp'], php: ['laravel', 'symfony', 'codeigniter'], ruby: ['rails', 'sinatra'] }; const supportedFrameworks = frameworkMap[language.toLowerCase()]; if (supportedFrameworks && !supportedFrameworks.includes(framework.toLowerCase())) { errors.push(`Framework ${framework} is not supported for ${language}. Supported frameworks: ${supportedFrameworks.join(', ')}`); } return errors; } static validateOpenAIModel(model) { const errors = []; const supportedModels = ['gpt-4', 'gpt-4-turbo', 'gpt-3.5-turbo']; if (!model || model.trim() === '') { errors.push('OpenAI model is required'); } else if (!supportedModels.includes(model)) { errors.push(`Unsupported model: ${model}. Supported models: ${supportedModels.join(', ')}`); } return errors; } static validateBoolean(value, fieldName) { const errors = []; if (typeof value !== 'boolean') { errors.push(`${fieldName} must be a boolean value`); } return errors; } static validateString(value, fieldName, options = {}) { const errors = []; if (options.required && (!value || value.trim() === '')) { errors.push(`${fieldName} is required`); return errors; } if (value !== null && value !== undefined) { if (typeof value !== 'string') { errors.push(`${fieldName} must be a string`); return errors; } if (options.minLength && value.length < options.minLength) { errors.push(`${fieldName} must be at least ${options.minLength} characters long`); } if (options.maxLength && value.length > options.maxLength) { errors.push(`${fieldName} must be no more than ${options.maxLength} characters long`); } if (options.pattern && !options.pattern.test(value)) { errors.push(`${fieldName} format is invalid`); } } return errors; } static validateArray(value, fieldName, options = {}) { const errors = []; if (options.required && (!value || !Array.isArray(value))) { errors.push(`${fieldName} is required and must be an array`); return errors; } if (value !== null && value !== undefined) { if (!Array.isArray(value)) { errors.push(`${fieldName} must be an array`); return errors; } if (options.minLength && value.length < options.minLength) { errors.push(`${fieldName} must have at least ${options.minLength} items`); } if (options.maxLength && value.length > options.maxLength) { errors.push(`${fieldName} must have no more than ${options.maxLength} items`); } if (options.itemValidator) { value.forEach((item, index) => { const itemErrors = options.itemValidator(item); if (itemErrors.length > 0) { errors.push(`${fieldName}[${index}]: ${itemErrors.join(', ')}`); } }); } } return errors; } static validateObject(value, fieldName, options = {}) { const errors = []; if (options.required && (!value || typeof value !== 'object')) { errors.push(`${fieldName} is required and must be an object`); return errors; } if (value !== null && value !== undefined) { if (typeof value !== 'object' || Array.isArray(value)) { errors.push(`${fieldName} must be an object`); return errors; } if (options.schema) { Object.entries(options.schema).forEach(([key, validator]) => { const fieldErrors = validator(value[key]); if (fieldErrors.length > 0) { errors.push(`${fieldName}.${key}: ${fieldErrors.join(', ')}`); } }); } } return errors; } static sanitizeFilename(filename) { // Remove or replace invalid characters return filename .replace(/[<>:"/\\|?*]/g, '_') .replace(/\s+/g, '_') .replace(/_{2,}/g, '_') .replace(/^_|_$/g, ''); } static sanitizePath(filePath) { // Remove path traversal attempts and invalid characters return filePath .replace(/\.\./g, '_') .replace(/[<>:"|?*]/g, '_') .replace(/\/{2,}/g, '/'); } static isValidUrl(url) { try { new URL(url); return true; } catch { return false; } } static isValidEmail(email) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); } static isValidSemanticVersion(version) { const versionRegex = /^\d+\.\d+\.\d+(-[a-zA-Z0-9.-]+)?(\+[a-zA-Z0-9.-]+)?$/; return versionRegex.test(version); } static validatePort(port) { const errors = []; const portNum = typeof port === 'string' ? parseInt(port, 10) : port; if (isNaN(portNum) || portNum < 1 || portNum > 65535) { errors.push('Port must be a number between 1 and 65535'); } return errors; } static validateTimeout(timeout) { const errors = []; const timeoutNum = typeof timeout === 'string' ? parseInt(timeout, 10) : timeout; if (isNaN(timeoutNum) || timeoutNum < 0 || timeoutNum > 300000) { // 5 minutes max errors.push('Timeout must be a number between 0 and 300000 milliseconds'); } return errors; } static validateRetryCount(retryCount) { const errors = []; const retryNum = typeof retryCount === 'string' ? parseInt(retryCount, 10) : retryCount; if (isNaN(retryNum) || retryNum < 0 || retryNum > 10) { errors.push('Retry count must be a number between 0 and 10'); } return errors; } } exports.ValidationUtils = ValidationUtils; //# sourceMappingURL=validation.js.map