-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathschema.ts
More file actions
276 lines (243 loc) · 8.18 KB
/
Copy pathschema.ts
File metadata and controls
276 lines (243 loc) · 8.18 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
/**
* Dashboard report schema types and validation.
*
* Defines the core data model for repository health dashboard reports,
* including collector interfaces and runtime validation functions.
*/
// -- Types ------------------------------------------------------------------
/**
* Top-level report produced by the dashboard pipeline.
* Contains metadata about the repository state and one or more category
* reports collected by individual collectors.
*/
export interface DashboardReport {
schema: "dashboard-report/v1";
generatedAt: string;
branch: string;
commit: string;
commitMessage: string;
categories: Record<string, CategoryReport>;
}
export type CategoryStatus = "pass" | "fail" | "warn" | "skip";
/**
* Report produced by a single collector (e.g. tests, lint, coverage).
*/
export interface CategoryReport {
status: CategoryStatus;
summary: {
total: number;
passed: number;
failed: number;
warnings: number;
skipped: number;
};
items: CategoryItem[];
collectedAt: string;
collectorVersion: string;
}
export interface CategoryItem {
name: string;
status: CategoryStatus;
message?: string;
metadata?: Record<string, string | number | boolean>;
}
/** Options passed to every collector. */
export interface CollectorOptions {
cwd: string;
timeout: number;
skipRun?: boolean;
}
/** Interface that all dashboard collectors must implement. */
export interface Collector {
name: string;
version: string;
collect(options: CollectorOptions): Promise<CategoryReport>;
}
// -- Validation helpers -----------------------------------------------------
const VALID_STATUSES: readonly string[] = ["pass", "fail", "warn", "skip"];
const COMMIT_SHA_REGEX = /^[0-9a-f]{40}$/;
const ISO_8601_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/;
const SEMVER_REGEX = /^\d+\.\d+\.\d+/;
interface ValidationResult {
valid: boolean;
errors: string[];
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function isValidISODate(value: unknown): boolean {
if (typeof value !== "string") return false;
if (!ISO_8601_REGEX.test(value)) return false;
const date = new Date(value);
return !isNaN(date.getTime());
}
function isValidStatus(value: unknown): boolean {
return typeof value === "string" && VALID_STATUSES.includes(value);
}
function isNonNegativeInteger(value: unknown): boolean {
return typeof value === "number" && Number.isInteger(value) && value >= 0;
}
// -- Public validation functions --------------------------------------------
/**
* Validate a {@link CategoryReport} at runtime.
*
* Returns `{ valid: true, errors: [] }` when all required fields are present
* and correctly typed, or `{ valid: false, errors: [...] }` listing every
* violation found. Extra fields are accepted for forward compatibility.
*/
export function validateCategoryReport(data: unknown): ValidationResult {
const errors: string[] = [];
if (!isRecord(data)) {
return { valid: false, errors: ["CategoryReport must be an object"] };
}
// status
if (!("status" in data)) {
errors.push("Missing required field: status");
} else if (!isValidStatus(data.status)) {
errors.push(
`Invalid status: "${String(data.status)}", must be one of: ${VALID_STATUSES.join(", ")}`
);
}
// summary
if (!("summary" in data)) {
errors.push("Missing required field: summary");
} else if (!isRecord(data.summary)) {
errors.push("summary must be an object");
} else {
const fields = ["total", "passed", "failed", "warnings", "skipped"] as const;
for (const field of fields) {
if (!(field in data.summary)) {
errors.push(`Missing required field: summary.${field}`);
} else if (!isNonNegativeInteger(data.summary[field])) {
errors.push(
`summary.${field} must be a non-negative integer, got: ${String(data.summary[field])}`
);
}
}
}
// items
if (!("items" in data)) {
errors.push("Missing required field: items");
} else if (!Array.isArray(data.items)) {
errors.push("items must be an array");
} else {
for (let i = 0; i < data.items.length; i++) {
const item = data.items[i] as unknown;
if (!isRecord(item)) {
errors.push(`items[${i}] must be an object`);
continue;
}
if (typeof item.name !== "string") {
errors.push(`items[${i}].name must be a string`);
}
if (!isValidStatus(item.status)) {
errors.push(`items[${i}].status is invalid: "${String(item.status)}"`);
}
if ("message" in item && item.message !== undefined) {
if (typeof item.message !== "string") {
errors.push(`items[${i}].message must be a string`);
}
}
if ("metadata" in item && item.metadata !== undefined) {
if (!isRecord(item.metadata)) {
errors.push(`items[${i}].metadata must be an object`);
} else {
for (const [key, val] of Object.entries(item.metadata)) {
const t = typeof val;
if (t !== "string" && t !== "number" && t !== "boolean") {
errors.push(
`items[${i}].metadata.${key} must be string, number, or boolean`
);
}
}
}
}
}
}
// collectedAt
if (!("collectedAt" in data)) {
errors.push("Missing required field: collectedAt");
} else if (!isValidISODate(data.collectedAt)) {
errors.push(
`Invalid ISO-8601 date for collectedAt: "${String(data.collectedAt)}"`
);
}
// collectorVersion
if (!("collectorVersion" in data)) {
errors.push("Missing required field: collectorVersion");
} else if (typeof data.collectorVersion !== "string") {
errors.push("collectorVersion must be a string");
} else if (!SEMVER_REGEX.test(data.collectorVersion)) {
errors.push(
`Invalid semver for collectorVersion: "${data.collectorVersion}"`
);
}
return { valid: errors.length === 0, errors };
}
/**
* Validate a {@link DashboardReport} at runtime.
*
* Checks that all required top-level fields are present and correctly typed.
* Category reports are validated recursively. Extra top-level fields are
* accepted for forward compatibility.
*/
export function validateDashboardReport(data: unknown): ValidationResult {
const errors: string[] = [];
if (!isRecord(data)) {
return { valid: false, errors: ["DashboardReport must be an object"] };
}
// schema
if (!("schema" in data)) {
errors.push("Missing required field: schema");
} else if (data.schema !== "dashboard-report/v1") {
errors.push(
`Invalid schema: "${String(data.schema)}", expected "dashboard-report/v1"`
);
}
// generatedAt
if (!("generatedAt" in data)) {
errors.push("Missing required field: generatedAt");
} else if (!isValidISODate(data.generatedAt)) {
errors.push(
`Invalid ISO-8601 date for generatedAt: "${String(data.generatedAt)}"`
);
}
// branch
if (!("branch" in data)) {
errors.push("Missing required field: branch");
} else if (typeof data.branch !== "string") {
errors.push("branch must be a string");
}
// commit
if (!("commit" in data)) {
errors.push("Missing required field: commit");
} else if (typeof data.commit !== "string") {
errors.push("commit must be a string");
} else if (data.commit !== "" && !COMMIT_SHA_REGEX.test(data.commit)) {
errors.push(
`Invalid commit SHA: "${data.commit}", must be 40 hex characters or empty string`
);
}
// commitMessage
if (!("commitMessage" in data)) {
errors.push("Missing required field: commitMessage");
} else if (typeof data.commitMessage !== "string") {
errors.push("commitMessage must be a string");
}
// categories
if (!("categories" in data)) {
errors.push("Missing required field: categories");
} else if (!isRecord(data.categories)) {
errors.push("categories must be an object");
} else {
for (const [key, value] of Object.entries(data.categories)) {
const result = validateCategoryReport(value);
if (!result.valid) {
for (const err of result.errors) {
errors.push(`categories.${key}: ${err}`);
}
}
}
}
return { valid: errors.length === 0, errors };
}