-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.ts
More file actions
46 lines (37 loc) · 1.31 KB
/
Copy pathvalidators.ts
File metadata and controls
46 lines (37 loc) · 1.31 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
import type { ProfileFile, SchemaFile } from "./schema.ts";
export const USER_SPECIFIC_PATHS = [
"identity.name",
"identity.description",
"identity.owner",
] as const;
export type CompletenessEntry = { path: string; detail: string };
export type CompletenessResult = {
errors: CompletenessEntry[];
warnings: CompletenessEntry[];
};
type Field = SchemaFile["sections"][number]["fields"][number];
export function completenessCheck(schema: SchemaFile, profile: ProfileFile): CompletenessResult {
const answers = profile.answers;
const errors: CompletenessEntry[] = [];
const warnings: CompletenessEntry[] = [];
for (const section of schema.sections) {
for (const field of section.fields) {
if (!field.required) continue;
if (Object.prototype.hasOwnProperty.call(answers, field.path)) continue;
if (hasDefault(field)) continue;
const entry: CompletenessEntry = {
path: field.path,
detail: `required path '${field.path}' not present in profile.answers`,
};
if ((USER_SPECIFIC_PATHS as readonly string[]).includes(field.path)) {
warnings.push(entry);
} else {
errors.push(entry);
}
}
}
return { errors, warnings };
}
function hasDefault(field: Field): boolean {
return "default" in field && field.default !== undefined;
}