-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile-validator.ts
More file actions
151 lines (135 loc) · 4.7 KB
/
Copy pathprofile-validator.ts
File metadata and controls
151 lines (135 loc) · 4.7 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
import { z } from "zod";
import type { SchemaFile } from "./meta-schema.ts";
export type ProfileIssueKind =
| "answer-unknown-path"
| "answer-type-mismatch"
| "answer-value-not-in-enum"
| "answer-array-item-type-mismatch"
| "answer-constraint-violation";
export type ProfileIssue = {
kind: ProfileIssueKind;
path: string;
detail: string;
};
const ProfileFile = z
.object({
version: z.string().regex(/^\d+\.\d+\.\d+$/, "version must be semver"),
profile: z
.object({
name: z.string().min(1),
description: z.string().min(1),
})
.strict(),
answers: z.record(z.string(), z.unknown()),
})
.strict();
export type ProfileFile = z.infer<typeof ProfileFile>;
export function parseProfileFile(input: unknown): ProfileFile {
const result = ProfileFile.safeParse(input);
if (!result.success) {
throw new Error(`profile invalid: ${formatZod(result.error)}`);
}
return result.data;
}
type Field = SchemaFile["sections"][number]["fields"][number];
export function validateProfile(schema: SchemaFile, profile: ProfileFile): ProfileIssue[] {
const fieldByPath = new Map<string, Field>();
for (const section of schema.sections) {
for (const field of section.fields) {
fieldByPath.set(field.path, field);
}
}
const issues: ProfileIssue[] = [];
for (const [path, value] of Object.entries(profile.answers)) {
const field = fieldByPath.get(path);
if (!field) {
issues.push({
kind: "answer-unknown-path",
path,
detail: `path '${path}' is not declared in schema`,
});
continue;
}
validateAnswer(path, value, field, issues);
}
return issues;
}
function validateAnswer(path: string, value: unknown, field: Field, issues: ProfileIssue[]): void {
switch (field.type) {
case "string":
if (typeof value !== "string") {
issues.push(typeMismatch(path, field.type, value));
return;
}
if (field.pattern !== undefined && !new RegExp(field.pattern).test(value)) {
issues.push(violation(path, `value '${value}' does not match pattern /${field.pattern}/`));
}
if (field.minLength !== undefined && value.length < field.minLength) {
issues.push(violation(path, `length ${value.length} below minLength ${field.minLength}`));
}
if (field.maxLength !== undefined && value.length > field.maxLength) {
issues.push(violation(path, `length ${value.length} above maxLength ${field.maxLength}`));
}
return;
case "number":
if (typeof value !== "number") {
issues.push(typeMismatch(path, field.type, value));
return;
}
if (field.min !== undefined && value < field.min) {
issues.push(violation(path, `value ${value} below min ${field.min}`));
}
if (field.max !== undefined && value > field.max) {
issues.push(violation(path, `value ${value} above max ${field.max}`));
}
return;
case "boolean":
if (typeof value !== "boolean") {
issues.push(typeMismatch(path, field.type, value));
}
return;
case "enum":
if (!field.values.includes(value as string | number | boolean)) {
issues.push({
kind: "answer-value-not-in-enum",
path,
detail: `value '${String(value)}' not in enum values [${field.values.map((v) => String(v)).join(", ")}]`,
});
}
return;
case "array":
if (!Array.isArray(value)) {
issues.push(typeMismatch(path, field.type, value));
return;
}
for (const [idx, item] of value.entries()) {
if (typeof item !== field.items) {
issues.push({
kind: "answer-array-item-type-mismatch",
path,
detail: `item[${idx}] '${String(item)}' is ${typeof item}, expected ${field.items}`,
});
}
}
if (field.minItems !== undefined && value.length < field.minItems) {
issues.push(violation(path, `has ${value.length} items, below minItems ${field.minItems}`));
}
if (field.maxItems !== undefined && value.length > field.maxItems) {
issues.push(violation(path, `has ${value.length} items, above maxItems ${field.maxItems}`));
}
return;
}
}
function typeMismatch(path: string, expected: Field["type"], value: unknown): ProfileIssue {
return {
kind: "answer-type-mismatch",
path,
detail: `expected ${expected}, got ${Array.isArray(value) ? "array" : typeof value}`,
};
}
function violation(path: string, detail: string): ProfileIssue {
return { kind: "answer-constraint-violation", path, detail };
}
function formatZod(err: z.ZodError): string {
return err.issues.map((i) => ` at ${i.path.join(".") || "<root>"}: ${i.message}`).join("\n");
}