-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.ts
More file actions
60 lines (48 loc) · 1.51 KB
/
Copy pathservices.ts
File metadata and controls
60 lines (48 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
export interface ProcessingItem {
id: string;
payload: Record<string, unknown>;
priority?: number;
}
export interface ValidationResult {
isValid: boolean;
errors: string[];
}
export class ProcessingService {
private validateItem(item: unknown): ValidationResult {
const errors: string[] = [];
if (typeof item !== 'object' || item === null) {
return { isValid: false, errors: ['Item must be a non-null object'] };
}
const candidate = item as Partial<ProcessingItem>;
if (!candidate.id || typeof candidate.id !== 'string') {
errors.push('Missing or invalid "id" field');
}
if (!candidate.payload || typeof candidate.payload !== 'object') {
errors.push('Missing or invalid "payload" field');
}
if (candidate.priority !== undefined && typeof candidate.priority !== 'number') {
errors.push('Invalid "priority" field type');
}
return {
isValid: errors.length === 0,
errors
};
}
public processBatch(items: unknown[]): {
processed: string[];
skipped: Array<{ item: unknown; errors: string[] }>;
} {
const processed: string[] = [];
const skipped: Array<{ item: unknown; errors: string[] }> = [];
for (const item of items) {
const validation = this.validateItem(item);
if (!validation.isValid) {
skipped.push({ item, errors: validation.errors });
continue;
}
const validItem = item as ProcessingItem;
processed.push(validItem.id);
}
return { processed, skipped };
}
}