|
| 1 | +import log from './log'; |
1 | 2 | import type { AffectedUser, EventContext } from '@hawk.so/types'; |
2 | 3 | import Sanitizer from '../modules/sanitizer'; |
3 | 4 |
|
4 | | -/** |
5 | | - * Validation result interface |
6 | | - */ |
7 | | -interface ValidationResult<T> { |
8 | | - isValid: boolean; |
9 | | - data?: T; |
10 | | - errors: string[]; |
11 | | -} |
12 | | - |
13 | 5 | /** |
14 | 6 | * Validates user data - basic security checks |
15 | 7 | * |
16 | 8 | * @param user |
17 | 9 | */ |
18 | | -export function validateUser(user: AffectedUser): ValidationResult<AffectedUser> { |
19 | | - const errors: string[] = []; |
20 | | - |
| 10 | +export function validateUser(user: AffectedUser): boolean { |
21 | 11 | if (!user || !Sanitizer.isObject(user)) { |
22 | | - errors.push('User must be an object'); |
| 12 | + log('validateUser: User must be an object', 'warn'); |
23 | 13 |
|
24 | | - return { isValid: false, |
25 | | - errors }; |
| 14 | + return false; |
26 | 15 | } |
27 | 16 |
|
28 | 17 | // Validate required ID |
29 | 18 | if (!user.id || typeof user.id !== 'string' || user.id.trim() === '') { |
30 | | - errors.push('User ID is required and must be a non-empty string'); |
31 | | - |
32 | | - return { isValid: false, |
33 | | - errors }; |
34 | | - } |
35 | | - |
36 | | - const validatedUser: AffectedUser = { |
37 | | - id: user.id.trim(), |
38 | | - }; |
39 | | - |
40 | | - // Add optional fields if they exist and are strings |
41 | | - if (user.name && typeof user.name === 'string') { |
42 | | - validatedUser.name = user.name.trim(); |
43 | | - } |
44 | | - |
45 | | - if (user.url && typeof user.url === 'string') { |
46 | | - validatedUser.url = user.url.trim(); |
47 | | - } |
| 19 | + log('validateUser: User ID is required and must be a non-empty string', 'warn'); |
48 | 20 |
|
49 | | - if (user.image && typeof user.image === 'string') { |
50 | | - validatedUser.image = user.image.trim(); |
| 21 | + return false; |
51 | 22 | } |
52 | 23 |
|
53 | | - return { |
54 | | - isValid: true, |
55 | | - data: validatedUser, |
56 | | - errors, |
57 | | - }; |
| 24 | + return true; |
58 | 25 | } |
59 | 26 |
|
60 | 27 | /** |
61 | 28 | * Validates context data - basic security checks |
62 | 29 | * |
63 | 30 | * @param context |
64 | 31 | */ |
65 | | -export function validateContext(context: EventContext): ValidationResult<EventContext> { |
66 | | - const errors: string[] = []; |
67 | | - |
| 32 | +export function validateContext(context: EventContext): boolean { |
68 | 33 | if (!context || !Sanitizer.isObject(context)) { |
69 | | - errors.push('Context must be an object'); |
| 34 | + log('validateContext: Context must be an object', 'warn'); |
70 | 35 |
|
71 | | - return { isValid: false, |
72 | | - errors }; |
| 36 | + return false; |
73 | 37 | } |
74 | 38 |
|
75 | | - return { |
76 | | - isValid: true, |
77 | | - data: context, |
78 | | - errors, |
79 | | - }; |
80 | | -} |
81 | | - |
82 | | -/** |
83 | | - * Logs validation errors |
84 | | - * |
85 | | - * @param prefix |
86 | | - * @param errors |
87 | | - */ |
88 | | -export function logValidationErrors(prefix: string, errors: string[]): void { |
89 | | - errors.forEach((error) => { |
90 | | - console.warn(`${prefix}: ${error}`); |
91 | | - }); |
| 39 | + return true; |
92 | 40 | } |
0 commit comments