source: database/drizzle/config/componentFieldConfig.ts@ 5c079f4

main
Last change on this file since 5c079f4 was 5c079f4, checked in by Tome <gjorgievtome@…>, 7 months ago

add componentFieldConfig

  • Property mode set to 100644
File size: 2.6 KB
RevLine 
[5c079f4]1export 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
17export 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
34export 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
61export 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}
Note: See TracBrowser for help on using the repository browser.