| 1 | export type ComponentType =
|
|---|
| 2 | | 'cpu'
|
|---|
| 3 | | 'gpu'
|
|---|
| 4 | | 'memory'
|
|---|
| 5 | | 'storage'
|
|---|
| 6 | | 'power_supply'
|
|---|
| 7 | | 'motherboard'
|
|---|
| 8 | | 'case'
|
|---|
| 9 | | 'cooler'
|
|---|
| 10 | | 'memory_card'
|
|---|
| 11 | | 'optical_drive'
|
|---|
| 12 | | 'sound_card'
|
|---|
| 13 | | 'cables'
|
|---|
| 14 | | 'network_adapter'
|
|---|
| 15 | | 'network_card';
|
|---|
| 16 |
|
|---|
| 17 | export const requiredFields: Record<ComponentType, string[]> = {
|
|---|
| 18 | cpu: ['socket', 'cores', 'threads', 'baseClock', 'tdp'],
|
|---|
| 19 | gpu: ['vram', 'tdp', 'chipset', 'length'],
|
|---|
| 20 | memory: ['type', 'speed', 'capacity', 'modules'],
|
|---|
| 21 | storage: ['type', 'capacity', 'formFactor'],
|
|---|
| 22 | power_supply: ['type', 'wattage', 'formFactor'],
|
|---|
| 23 | motherboard: ['socket', 'chipset', 'formFactor', 'ramType', 'numRamSlots', 'maxRamCapacity', 'pciExpressSlots'],
|
|---|
| 24 | case: ['coolerMaxHeight', 'gpuMaxLength'],
|
|---|
| 25 | cooler: ['type', 'height', 'maxTdpSupported'],
|
|---|
| 26 | memory_card: ['numSlots', 'interface'],
|
|---|
| 27 | optical_drive: ['formFactor', 'type', 'interface', 'writeSpeed', 'readSpeed'],
|
|---|
| 28 | sound_card: ['sampleRate', 'bitDepth', 'chipset', 'interface', 'channel'],
|
|---|
| 29 | cables: ['lengthCm', 'type'],
|
|---|
| 30 | network_adapter: ['wifiVersion', 'interface', 'numAntennas'],
|
|---|
| 31 | network_card: ['numPorts', 'speed', 'interface']
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | export function validateComponentSpecificData(type: string, specificData: any): boolean {
|
|---|
| 35 | if (!(type in requiredFields)) {
|
|---|
| 36 | return false;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | const fields = requiredFields[type as ComponentType];
|
|---|
| 40 |
|
|---|
| 41 | for (const field of fields) {
|
|---|
| 42 | const value = specificData[field];
|
|---|
| 43 | if (value === undefined || value === null || value === '') {
|
|---|
| 44 | return false;
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | if (type === 'case') {
|
|---|
| 49 | if (specificData.storageFormFactors && !Array.isArray(specificData.storageFormFactors)) return false;
|
|---|
| 50 | if (specificData.psFormFactors && !Array.isArray(specificData.psFormFactors)) return false;
|
|---|
| 51 | if (specificData.moboFormFactors && !Array.isArray(specificData.moboFormFactors)) return false;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | if (type === 'cooler') {
|
|---|
| 55 | if (specificData.cpuSockets && !Array.isArray(specificData.cpuSockets)) return false;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | return true;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | export function getValidationError(type: string, specificData: any): string | null {
|
|---|
| 62 | if (!(type in requiredFields)) {
|
|---|
| 63 | return `Invalid component type: ${type}`;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | const fields = requiredFields[type as ComponentType];
|
|---|
| 67 | const missingFields: string[] = [];
|
|---|
| 68 |
|
|---|
| 69 | for (const field of fields) {
|
|---|
| 70 | const value = specificData[field];
|
|---|
| 71 | if (value === undefined || value === null || value === '') {
|
|---|
| 72 | missingFields.push(field);
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | if (missingFields.length > 0) {
|
|---|
| 77 | return `Missing required fields: ${missingFields.join(', ')}`;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | return null;
|
|---|
| 81 | } |
|---|