-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
307 lines (257 loc) · 8.51 KB
/
Copy pathapp.js
File metadata and controls
307 lines (257 loc) · 8.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Simple configuration for the scanner.
// You can tweak these defaults for different systems.
const CONFIG = {
requiredFields: ["event_name", "user_id", "timestamp", "environment"],
fieldTypes: {
event_name: "string",
user_id: "number",
timestamp: "string",
environment: "string",
source: "string",
action_type: "string",
},
namingConvention: "snake_case", // future: allow "camelCase" or "mixed"
domainRules: {
environment: ["prod", "staging", "dev"],
action_type: ["LOGIN", "LOGOUT", "PURCHASE", "VIEW"],
},
};
// ---------- Helpers ----------
function safeParseJSON(text) {
try {
const parsed = JSON.parse(text);
if (parsed === null || Array.isArray(parsed) || typeof parsed !== "object") {
return { error: "Please provide a single JSON object, not an array or primitive." };
}
return { value: parsed };
} catch (err) {
return { error: err.message || "Invalid JSON." };
}
}
function isSnakeCase(key) {
return /^[a-z0-9]+(_[a-z0-9]+)*$/.test(key);
}
// ---------- Validation ----------
function validateEvent(eventObj, config) {
const issues = [];
// Required fields
config.requiredFields.forEach((field) => {
if (!(field in eventObj)) {
issues.push({
category: "required",
field,
message: `Missing required field "${field}".`,
severity: "error",
});
} else if (
eventObj[field] === null ||
eventObj[field] === "" ||
(typeof eventObj[field] === "string" && eventObj[field].trim() === "")
) {
issues.push({
category: "required",
field,
message: `Required field "${field}" is present but empty.`,
severity: "warning",
});
}
});
// Types
Object.entries(config.fieldTypes).forEach(([field, expected]) => {
if (!(field in eventObj)) return;
const value = eventObj[field];
const actual = Array.isArray(value) ? "array" : typeof value;
if (expected !== actual) {
issues.push({
category: "type",
field,
message: `Type mismatch for "${field}": expected ${expected}, got ${actual}.`,
severity: "error",
});
}
if (field === "timestamp" && typeof value === "string") {
const likelyIso = /^\d{4}-\d{2}-\d{2}T/.test(value);
if (!likelyIso) {
issues.push({
category: "type",
field,
message: `Timestamp "${value}" does not look like an ISO-8601 value.`,
severity: "warning",
});
}
}
});
// Naming convention
Object.keys(eventObj).forEach((key) => {
if (config.namingConvention === "snake_case" && !isSnakeCase(key)) {
issues.push({
category: "naming",
field: key,
message: `Field "${key}" does not follow snake_case naming.`,
severity: "warning",
});
}
});
// Domain rules
Object.entries(config.domainRules).forEach(([field, allowed]) => {
if (!(field in eventObj)) return;
const value = eventObj[field];
if (!allowed.includes(value)) {
issues.push({
category: "domain",
field,
message: `Value "${value}" for "${field}" is not in allowed set: [${allowed.join(
", "
)}].`,
severity: "warning",
});
}
});
const passed = issues.filter((i) => i.severity === "error").length === 0;
return { passed, issues };
}
// ---------- Rendering ----------
function clearIssuesLists() {
["required-issues", "type-issues", "naming-issues", "domain-issues"].forEach((id) => {
const ul = document.getElementById(id);
ul.innerHTML = "";
ul.classList.add("empty");
});
document.getElementById("raw-issues").textContent = "No issues — event meets all configured rules.";
}
function renderSummary(result) {
const container = document.getElementById("summary");
container.innerHTML = "";
const badge = document.createElement("div");
const total = result.issues.length;
const errors = result.issues.filter((i) => i.severity === "error").length;
if (total === 0) {
badge.className = "summary-badge summary-badge-ok";
badge.textContent = "All checks passed. No issues detected.";
} else if (errors === 0) {
badge.className = "summary-badge summary-badge-fail";
badge.innerHTML = `
<span class="count">${total}</span> issue${total === 1 ? "" : "s"} detected (warnings only).
`;
} else {
badge.className = "summary-badge summary-badge-fail";
badge.innerHTML = `
<span class="count">${total}</span> issue${total === 1 ? "" : "s"} detected
· <span class="count">${errors}</span> error${errors === 1 ? "" : "s"}.
`;
}
container.appendChild(badge);
}
function addIssueToList(listId, issue) {
const ul = document.getElementById(listId);
ul.classList.remove("empty");
const li = document.createElement("li");
const pill = document.createElement("span");
pill.className = "issue-pill";
pill.textContent = issue.field || "event";
const msg = document.createElement("span");
msg.className = "issue-message";
msg.textContent = issue.message;
li.appendChild(pill);
li.appendChild(msg);
ul.appendChild(li);
}
function renderIssues(result) {
clearIssuesLists();
if (result.issues.length === 0) {
renderSummary(result);
return;
}
renderSummary(result);
result.issues.forEach((issue) => {
switch (issue.category) {
case "required":
addIssueToList("required-issues", issue);
break;
case "type":
addIssueToList("type-issues", issue);
break;
case "naming":
addIssueToList("naming-issues", issue);
break;
case "domain":
addIssueToList("domain-issues", issue);
break;
default:
break;
}
});
const raw = result.issues.map((i) => `- [${i.category}] ${i.message}`).join("\n");
document.getElementById("raw-issues").textContent = raw;
}
// ---------- Example events ----------
const VALID_EVENT = {
event_name: "user.login",
user_id: 123,
timestamp: "2025-11-22T15:00:00Z",
environment: "prod",
source: "web",
action_type: "LOGIN",
};
const BROKEN_EVENT = {
// wrong name (camelCase instead of snake) -> naming issue
eventName: "user.login",
// wrong type (string instead of number) -> type issue
user_id: "123",
// non-ISO timestamp -> format warning
timestamp: "2025/11/22 15:00",
// missing "environment" (required) -> required error
// wrong field name + bad domain value
env: "production",
// invalid domain value for action_type
action_type: "LOGIN-FAILED",
};
// ---------- UI handlers ----------
function onScan() {
const input = document.getElementById("event-input").value.trim();
const parseStatus = document.getElementById("parse-status");
if (!input) {
parseStatus.textContent = "Please paste a JSON event first.";
clearIssuesLists();
document.getElementById("summary").innerHTML =
'<div class="summary-badge summary-badge-idle">No event scanned yet.</div>';
return;
}
const { value, error } = safeParseJSON(input);
if (error) {
parseStatus.textContent = "JSON error: " + error;
clearIssuesLists();
document.getElementById("summary").innerHTML =
'<div class="summary-badge summary-badge-fail">Cannot scan: invalid JSON.</div>';
document.getElementById("raw-issues").textContent = "No results due to invalid JSON.";
return;
}
parseStatus.textContent = "JSON parsed successfully.";
const result = validateEvent(value, CONFIG);
renderIssues(result);
}
function loadExample(eventObj, label) {
const textarea = document.getElementById("event-input");
textarea.value = JSON.stringify(eventObj, null, 2);
document.getElementById("parse-status").textContent = `Loaded ${label} example.`;
clearIssuesLists();
document.getElementById("summary").innerHTML =
'<div class="summary-badge summary-badge-idle">Ready to scan the loaded event.</div>';
document.getElementById("raw-issues").textContent = "No scan yet.";
}
function onLoadValid() {
loadExample(VALID_EVENT, "valid");
}
function onLoadBroken() {
loadExample(BROKEN_EVENT, "broken");
}
// ---------- Wire up ----------
document.addEventListener("DOMContentLoaded", () => {
document.getElementById("scan-button").addEventListener("click", onScan);
const validBtn = document.getElementById("load-valid");
const brokenBtn = document.getElementById("load-broken");
if (validBtn) validBtn.addEventListener("click", onLoadValid);
if (brokenBtn) brokenBtn.addEventListener("click", onLoadBroken);
// Optional: start with the broken example loaded
loadExample(BROKEN_EVENT, "broken");
});