-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvalidation.ts
More file actions
94 lines (77 loc) · 2.41 KB
/
validation.ts
File metadata and controls
94 lines (77 loc) · 2.41 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { log } from '@hawk.so/core';
import type { AffectedUser, Breadcrumb, EventContext, EventData, JavaScriptAddons } from '@hawk.so/types';
import Sanitizer from '../modules/sanitizer';
/**
* Validates user data - basic security checks
*
* @param user - user data to validate
*/
export function validateUser(user: AffectedUser): boolean {
if (!user || !Sanitizer.isObject(user)) {
log('validateUser: User must be an object', 'warn');
return false;
}
// Validate required ID
if (!user.id || typeof user.id !== 'string' || user.id.trim() === '') {
log('validateUser: User ID is required and must be a non-empty string', 'warn');
return false;
}
return true;
}
/**
* Validates context data - basic security checks
*
* @param context - context data to validate
*/
export function validateContext(context: EventContext | undefined): boolean {
if (context && !Sanitizer.isObject(context)) {
log('validateContext: Context must be an object', 'warn');
return false;
}
return true;
}
/**
* Checks if value is a plain object (not array, Date, etc.)
*
* @param value - value to check
*/
function isPlainObject(value: unknown): value is Record<string, unknown> {
return Object.prototype.toString.call(value) === '[object Object]';
}
/**
* Runtime check for required EventData fields.
* Per @hawk.so/types EventData, `title` is the only non-optional field.
* Additionally validates `backtrace` shape if present (must be an array).
*
* @param payload - value to validate
*/
export function isValidEventPayload(payload: unknown): payload is EventData<JavaScriptAddons> {
if (!isPlainObject(payload)) {
return false;
}
if (typeof payload.title !== 'string' || payload.title.trim() === '') {
return false;
}
if (payload.backtrace !== undefined && !Array.isArray(payload.backtrace)) {
return false;
}
return true;
}
/**
* Runtime check that value is a valid Breadcrumb-like object.
* Must be a plain object with a string message and numeric timestamp.
*
* @param breadcrumb - value to validate
*/
export function isValidBreadcrumb(breadcrumb: unknown): breadcrumb is Breadcrumb {
if (!isPlainObject(breadcrumb)) {
return false;
}
if (typeof breadcrumb.message !== 'string' || breadcrumb.message.trim() === '') {
return false;
}
if (breadcrumb.timestamp !== undefined && typeof breadcrumb.timestamp !== 'number') {
return false;
}
return true;
}