@backmatter/core 0.0.32 → 0.0.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/block.ts +10 -8
- package/src/color.ts +40 -0
- package/src/doc.ts +2 -0
- package/src/document-label.ts +24 -0
- package/src/fields.ts +264 -15
- package/src/index.ts +7 -1
- package/src/model.ts +11 -6
- package/src/node/cli.ts +10 -2
- package/src/node/create-backmatter.ts +15 -9
- package/src/node/eval-model.ts +109 -25
- package/src/node/generate.ts +14 -5
- package/src/node/index.ts +7 -1
- package/src/preview-channel.ts +44 -3
- package/src/query.ts +457 -78
- package/src/relation-match.ts +12 -0
- package/src/relation-problems.ts +189 -0
- package/src/schema-check.ts +199 -0
- package/src/sync-schema.ts +2 -0
- package/src/validate.ts +108 -21
- package/src/zod.ts +127 -10
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// packages/core/src/relation-problems.ts
|
|
2
|
+
import type { BlockInstance } from "./block";
|
|
3
|
+
import type { FieldSnapshot } from "./fields";
|
|
4
|
+
import { matchesWhere } from "./relation-match";
|
|
5
|
+
import type { ModelSnapshot, SchemaSnapshot } from "./model";
|
|
6
|
+
|
|
7
|
+
type DocEntry = { slug: string; frontmatter: Record<string, unknown> };
|
|
8
|
+
|
|
9
|
+
function docKey(type: string, slug: string): string {
|
|
10
|
+
return `${type}/${slug}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function isBlankRelationValue(value: unknown): boolean {
|
|
14
|
+
return typeof value !== "string" || value.trim() === "";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function inlineFields(field: FieldSnapshot): Record<string, FieldSnapshot> | undefined {
|
|
18
|
+
if (!field.fields || Object.keys(field.fields).length === 0) return undefined;
|
|
19
|
+
return field.fields;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function objectFieldsFor(
|
|
23
|
+
schema: SchemaSnapshot,
|
|
24
|
+
field: FieldSnapshot,
|
|
25
|
+
): Record<string, FieldSnapshot> | undefined {
|
|
26
|
+
const inline = inlineFields(field);
|
|
27
|
+
if (inline) return inline;
|
|
28
|
+
if ((field.kind === "object" || field.kind === "array") && field.object) {
|
|
29
|
+
const def = schema.objects?.find((o) => o.name === field.object);
|
|
30
|
+
return def?.fields;
|
|
31
|
+
}
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function joinPath(prefix: string, segment: string): string {
|
|
36
|
+
return prefix ? `${prefix}.${segment}` : segment;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function checkRelationValue(
|
|
40
|
+
path: string,
|
|
41
|
+
field: FieldSnapshot,
|
|
42
|
+
value: unknown,
|
|
43
|
+
modelsByType: Map<string, ModelSnapshot>,
|
|
44
|
+
docMap: Map<string, DocEntry>,
|
|
45
|
+
errors: string[],
|
|
46
|
+
): void {
|
|
47
|
+
if (field.kind !== "relation" || !field.relationTo) return;
|
|
48
|
+
if (isBlankRelationValue(value)) return;
|
|
49
|
+
const targetModel = modelsByType.get(field.relationTo);
|
|
50
|
+
if (!targetModel || targetModel.external) return;
|
|
51
|
+
|
|
52
|
+
const targetSlug = value as string;
|
|
53
|
+
const target = docMap.get(docKey(field.relationTo, targetSlug));
|
|
54
|
+
if (!target) {
|
|
55
|
+
errors.push(`${path} must name a ${field.relationTo} document`);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (!matchesWhere(target, field.where)) {
|
|
59
|
+
errors.push(`${path} must match the field's filter`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function asBlockList(value: unknown): BlockInstance[] {
|
|
64
|
+
if (!Array.isArray(value)) return [];
|
|
65
|
+
return value.filter((item): item is BlockInstance => {
|
|
66
|
+
if (!item || typeof item !== "object") return false;
|
|
67
|
+
const row = item as Record<string, unknown>;
|
|
68
|
+
return typeof row.id === "string" && typeof row.type === "string";
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function walkFields(
|
|
73
|
+
schema: SchemaSnapshot,
|
|
74
|
+
pathPrefix: string,
|
|
75
|
+
fields: Record<string, FieldSnapshot>,
|
|
76
|
+
data: Record<string, unknown>,
|
|
77
|
+
modelsByType: Map<string, ModelSnapshot>,
|
|
78
|
+
docMap: Map<string, DocEntry>,
|
|
79
|
+
errors: string[],
|
|
80
|
+
): void {
|
|
81
|
+
for (const [name, field] of Object.entries(fields)) {
|
|
82
|
+
const path = joinPath(pathPrefix, name);
|
|
83
|
+
const value = data[name];
|
|
84
|
+
|
|
85
|
+
if (field.kind === "relation") {
|
|
86
|
+
checkRelationValue(path, field, value, modelsByType, docMap, errors);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (field.kind === "blocks") {
|
|
91
|
+
walkBlockInstances(schema, name, value, modelsByType, docMap, errors);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const nested = objectFieldsFor(schema, field);
|
|
96
|
+
if (!nested) continue;
|
|
97
|
+
|
|
98
|
+
if (field.kind === "array" && Array.isArray(value)) {
|
|
99
|
+
value.forEach((item, index) => {
|
|
100
|
+
if (!item || typeof item !== "object") return;
|
|
101
|
+
walkFields(
|
|
102
|
+
schema,
|
|
103
|
+
`${path}.${index}`,
|
|
104
|
+
nested,
|
|
105
|
+
item as Record<string, unknown>,
|
|
106
|
+
modelsByType,
|
|
107
|
+
docMap,
|
|
108
|
+
errors,
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
} else if (field.kind === "object" && value && typeof value === "object") {
|
|
112
|
+
walkFields(
|
|
113
|
+
schema,
|
|
114
|
+
path,
|
|
115
|
+
nested,
|
|
116
|
+
value as Record<string, unknown>,
|
|
117
|
+
modelsByType,
|
|
118
|
+
docMap,
|
|
119
|
+
errors,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function walkBlockInstances(
|
|
126
|
+
schema: SchemaSnapshot,
|
|
127
|
+
prefix: string,
|
|
128
|
+
blocks: unknown,
|
|
129
|
+
modelsByType: Map<string, ModelSnapshot>,
|
|
130
|
+
docMap: Map<string, DocEntry>,
|
|
131
|
+
errors: string[],
|
|
132
|
+
): void {
|
|
133
|
+
const catalog = schema.blocks ?? [];
|
|
134
|
+
const byName = new Map(catalog.map((b) => [b.name, b]));
|
|
135
|
+
asBlockList(blocks).forEach((block, index) => {
|
|
136
|
+
const def = byName.get(block.type);
|
|
137
|
+
if (!def) return;
|
|
138
|
+
const props =
|
|
139
|
+
block.props && typeof block.props === "object"
|
|
140
|
+
? (block.props as Record<string, unknown>)
|
|
141
|
+
: {};
|
|
142
|
+
walkFields(
|
|
143
|
+
schema,
|
|
144
|
+
`${prefix}[${index}]`,
|
|
145
|
+
def.fields,
|
|
146
|
+
props,
|
|
147
|
+
modelsByType,
|
|
148
|
+
docMap,
|
|
149
|
+
errors,
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function walkDocument(
|
|
155
|
+
schema: SchemaSnapshot,
|
|
156
|
+
model: ModelSnapshot,
|
|
157
|
+
frontmatter: Record<string, unknown>,
|
|
158
|
+
modelsByType: Map<string, ModelSnapshot>,
|
|
159
|
+
docMap: Map<string, DocEntry>,
|
|
160
|
+
errors: string[],
|
|
161
|
+
): void {
|
|
162
|
+
walkFields(schema, "", model.fields, frontmatter, modelsByType, docMap, errors);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function relationProblems(input: {
|
|
166
|
+
schema: SchemaSnapshot;
|
|
167
|
+
docs: Array<{ type: string; slug: string; frontmatter: Record<string, unknown> }>;
|
|
168
|
+
}): Array<{ type: string; slug: string; errors: string[] }> {
|
|
169
|
+
const modelsByType = new Map(input.schema.models.map((m) => [m.type, m]));
|
|
170
|
+
const docMap = new Map<string, DocEntry>();
|
|
171
|
+
for (const doc of input.docs) {
|
|
172
|
+
docMap.set(docKey(doc.type, doc.slug), {
|
|
173
|
+
slug: doc.slug,
|
|
174
|
+
frontmatter: doc.frontmatter,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const rows: Array<{ type: string; slug: string; errors: string[] }> = [];
|
|
179
|
+
for (const doc of input.docs) {
|
|
180
|
+
const model = modelsByType.get(doc.type);
|
|
181
|
+
if (!model) continue;
|
|
182
|
+
const errors: string[] = [];
|
|
183
|
+
walkDocument(input.schema, model, doc.frontmatter, modelsByType, docMap, errors);
|
|
184
|
+
if (errors.length > 0) {
|
|
185
|
+
rows.push({ type: doc.type, slug: doc.slug, errors });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return rows;
|
|
189
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// packages/core/src/schema-check.ts
|
|
2
|
+
import type { FieldSnapshot } from "./fields";
|
|
3
|
+
import type { ModelSnapshot, SchemaSnapshot } from "./model";
|
|
4
|
+
|
|
5
|
+
type ScalarKind = "text" | "number" | "boolean";
|
|
6
|
+
|
|
7
|
+
function isScalarKind(kind: string): kind is ScalarKind {
|
|
8
|
+
return kind === "text" || kind === "number" || kind === "boolean";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function scalarJsonType(kind: ScalarKind): "string" | "number" | "boolean" {
|
|
12
|
+
if (kind === "number") return "number";
|
|
13
|
+
if (kind === "boolean") return "boolean";
|
|
14
|
+
return "string";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function fieldPath(owner: string, segments: string[]): string {
|
|
18
|
+
return `${owner}.${segments.join(".")}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function modelMap(models: ModelSnapshot[]): Map<string, ModelSnapshot> {
|
|
22
|
+
return new Map(models.map((model) => [model.type, model]));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function objectExists(schema: SchemaSnapshot, name: string): boolean {
|
|
26
|
+
return schema.objects?.some((object) => object.name === name) ?? false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function inlineFields(field: FieldSnapshot): Record<string, FieldSnapshot> | undefined {
|
|
30
|
+
if (!field.fields || Object.keys(field.fields).length === 0) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
return field.fields;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function resolveNamedObjectFields(
|
|
37
|
+
schema: SchemaSnapshot,
|
|
38
|
+
objectName: string,
|
|
39
|
+
visiting: Set<string>,
|
|
40
|
+
): Record<string, FieldSnapshot> | undefined {
|
|
41
|
+
if (visiting.has(objectName)) {
|
|
42
|
+
return {};
|
|
43
|
+
}
|
|
44
|
+
const object = schema.objects?.find((entry) => entry.name === objectName);
|
|
45
|
+
if (!object) {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
visiting.add(objectName);
|
|
49
|
+
return object.fields;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function nestedFieldsToWalk(
|
|
53
|
+
schema: SchemaSnapshot,
|
|
54
|
+
field: FieldSnapshot,
|
|
55
|
+
visiting: Set<string>,
|
|
56
|
+
): Record<string, FieldSnapshot> | undefined {
|
|
57
|
+
const inline = inlineFields(field);
|
|
58
|
+
if (inline) {
|
|
59
|
+
return inline;
|
|
60
|
+
}
|
|
61
|
+
if ((field.kind === "object" || field.kind === "array") && field.object) {
|
|
62
|
+
return resolveNamedObjectFields(schema, field.object, visiting);
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function checkRelationField(
|
|
68
|
+
path: string,
|
|
69
|
+
field: FieldSnapshot,
|
|
70
|
+
modelsByType: Map<string, ModelSnapshot>,
|
|
71
|
+
problems: string[],
|
|
72
|
+
): void {
|
|
73
|
+
if (!field.relationTo) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const target = modelsByType.get(field.relationTo);
|
|
77
|
+
if (!target) {
|
|
78
|
+
problems.push(`${path} relationTo "${field.relationTo}" names no model`);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (!field.where) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (target.external) {
|
|
85
|
+
problems.push(`${path} where is set on external model "${target.type}"`);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
for (const [key, value] of Object.entries(field.where)) {
|
|
89
|
+
if (key === "slug") {
|
|
90
|
+
if (typeof value !== "string") {
|
|
91
|
+
problems.push(`${path} where.slug must be a string`);
|
|
92
|
+
}
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const targetField = target.fields[key];
|
|
96
|
+
if (!targetField || !isScalarKind(targetField.kind)) {
|
|
97
|
+
problems.push(`${path} where.${key} is not a scalar field of ${target.type}`);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const expected = scalarJsonType(targetField.kind);
|
|
101
|
+
if (typeof value !== expected) {
|
|
102
|
+
problems.push(`${path} where.${key} must be a ${expected}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function checkObjectReference(
|
|
108
|
+
schema: SchemaSnapshot,
|
|
109
|
+
path: string,
|
|
110
|
+
field: FieldSnapshot,
|
|
111
|
+
problems: string[],
|
|
112
|
+
): void {
|
|
113
|
+
if (field.kind !== "object" && field.kind !== "array") {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (!field.object || inlineFields(field)) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (!objectExists(schema, field.object)) {
|
|
120
|
+
problems.push(`${path} object "${field.object}" names no object`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function walkFields(
|
|
125
|
+
schema: SchemaSnapshot,
|
|
126
|
+
owner: string,
|
|
127
|
+
segments: string[],
|
|
128
|
+
fields: Record<string, FieldSnapshot>,
|
|
129
|
+
modelsByType: Map<string, ModelSnapshot>,
|
|
130
|
+
visitingObjects: Set<string>,
|
|
131
|
+
problems: string[],
|
|
132
|
+
): void {
|
|
133
|
+
for (const [name, field] of Object.entries(fields)) {
|
|
134
|
+
const path = fieldPath(owner, [...segments, name]);
|
|
135
|
+
checkObjectReference(schema, path, field, problems);
|
|
136
|
+
checkRelationField(path, field, modelsByType, problems);
|
|
137
|
+
const nested = nestedFieldsToWalk(schema, field, visitingObjects);
|
|
138
|
+
if (nested) {
|
|
139
|
+
walkFields(schema, owner, [...segments, name], nested, modelsByType, visitingObjects, problems);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function assertSchema(schema: SchemaSnapshot): void {
|
|
145
|
+
const modelsByType = modelMap(schema.models);
|
|
146
|
+
const problems: string[] = [];
|
|
147
|
+
|
|
148
|
+
for (const model of schema.models) {
|
|
149
|
+
walkFields(
|
|
150
|
+
schema,
|
|
151
|
+
`model ${model.type}`,
|
|
152
|
+
[],
|
|
153
|
+
model.fields,
|
|
154
|
+
modelsByType,
|
|
155
|
+
new Set<string>(),
|
|
156
|
+
problems,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
for (const block of schema.blocks ?? []) {
|
|
161
|
+
walkFields(
|
|
162
|
+
schema,
|
|
163
|
+
`block ${block.name}`,
|
|
164
|
+
[],
|
|
165
|
+
block.fields,
|
|
166
|
+
modelsByType,
|
|
167
|
+
new Set<string>(),
|
|
168
|
+
problems,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
for (const block of schema.richtext ?? []) {
|
|
173
|
+
walkFields(
|
|
174
|
+
schema,
|
|
175
|
+
`richtext ${block.name}`,
|
|
176
|
+
[],
|
|
177
|
+
block.fields,
|
|
178
|
+
modelsByType,
|
|
179
|
+
new Set<string>(),
|
|
180
|
+
problems,
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
for (const object of schema.objects ?? []) {
|
|
185
|
+
walkFields(
|
|
186
|
+
schema,
|
|
187
|
+
`object ${object.name}`,
|
|
188
|
+
[],
|
|
189
|
+
object.fields,
|
|
190
|
+
modelsByType,
|
|
191
|
+
new Set<string>(),
|
|
192
|
+
problems,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (problems.length > 0) {
|
|
197
|
+
throw new Error(problems.join("\n"));
|
|
198
|
+
}
|
|
199
|
+
}
|
package/src/sync-schema.ts
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
type ModelDef,
|
|
5
5
|
type ModelSnapshot,
|
|
6
6
|
} from "./model";
|
|
7
|
+
import { assertSchema } from "./schema-check";
|
|
7
8
|
import type { BlockDef } from "./block";
|
|
8
9
|
import type { ObjectDef } from "./object";
|
|
9
10
|
|
|
@@ -38,6 +39,7 @@ export async function syncSchema(options: SyncSchemaOptions): Promise<void> {
|
|
|
38
39
|
options.objects,
|
|
39
40
|
options.richtext,
|
|
40
41
|
);
|
|
42
|
+
assertSchema(payload);
|
|
41
43
|
const body: {
|
|
42
44
|
payload: ReturnType<typeof serializeSchema>;
|
|
43
45
|
branch: string;
|
package/src/validate.ts
CHANGED
|
@@ -1,16 +1,60 @@
|
|
|
1
1
|
// packages/core/src/validate.ts
|
|
2
|
+
import { z } from "zod";
|
|
2
3
|
import { modelFormat, parseDoc } from "./doc-format";
|
|
4
|
+
import { relationProblems } from "./relation-problems";
|
|
3
5
|
import type { Frontmatter } from "./mdx";
|
|
4
6
|
import { parseMdx } from "./mdx";
|
|
5
7
|
import type { ModelSnapshot } from "./model";
|
|
6
8
|
import type { BlockInstance, BlockSnapshot } from "./block";
|
|
9
|
+
import type { ObjectSnapshot } from "./object";
|
|
7
10
|
import { matchAnyModelPath } from "./path";
|
|
8
|
-
import { expectedTypeFor, frontmatterZod, normalizeFrontmatter } from "./zod";
|
|
11
|
+
import { expectedTypeFor, fieldAtPath, frontmatterZod, normalizeFrontmatter } from "./zod";
|
|
9
12
|
|
|
10
13
|
export type ValidationResult =
|
|
11
14
|
| { ok: true }
|
|
12
15
|
| { ok: false; errors: string[] };
|
|
13
16
|
|
|
17
|
+
function pathKey(path: PropertyKey[]): string {
|
|
18
|
+
return path.map(String).join(".");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function valueAtPath(data: Frontmatter, path: PropertyKey[]): unknown {
|
|
22
|
+
let current: unknown = data;
|
|
23
|
+
for (const seg of path) {
|
|
24
|
+
if (current === null || current === undefined || typeof current !== "object") {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
current = (current as Record<string, unknown>)[String(seg)];
|
|
28
|
+
}
|
|
29
|
+
return current;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function issueMessage(
|
|
33
|
+
issue: z.core.$ZodIssue | z.ZodIssue,
|
|
34
|
+
model: Pick<ModelSnapshot, "fields">,
|
|
35
|
+
data: Frontmatter,
|
|
36
|
+
objects: ObjectSnapshot[],
|
|
37
|
+
): string | null {
|
|
38
|
+
const key = pathKey(issue.path);
|
|
39
|
+
const top = String(issue.path[0] ?? "");
|
|
40
|
+
const field = fieldAtPath(model.fields, issue.path, objects) ?? model.fields[top];
|
|
41
|
+
const missing = valueAtPath(data, issue.path) === undefined;
|
|
42
|
+
|
|
43
|
+
if (issue.code === "too_small" || (issue.code === "invalid_type" && missing)) {
|
|
44
|
+
return `${key} is required`;
|
|
45
|
+
}
|
|
46
|
+
if (issue.code === "custom" && issue.message.startsWith("must be")) {
|
|
47
|
+
return `${key} ${issue.message}`;
|
|
48
|
+
}
|
|
49
|
+
if (field?.kind === "media" && !missing) {
|
|
50
|
+
return `${key} must be a media URL`;
|
|
51
|
+
}
|
|
52
|
+
if (field) {
|
|
53
|
+
return `${key} must be a ${expectedTypeFor(field.kind)}`;
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
14
58
|
/**
|
|
15
59
|
* Frontmatter must satisfy the model: required fields present and non-blank,
|
|
16
60
|
* every declared field of the right primitive type. Unknown keys are ignored.
|
|
@@ -18,26 +62,23 @@ export type ValidationResult =
|
|
|
18
62
|
export function validateFrontmatter(
|
|
19
63
|
model: Pick<ModelSnapshot, "fields">,
|
|
20
64
|
frontmatter: Frontmatter,
|
|
65
|
+
objects: ObjectSnapshot[] = [],
|
|
21
66
|
): ValidationResult {
|
|
22
67
|
const data = normalizeFrontmatter(frontmatter);
|
|
23
|
-
const result = frontmatterZod(model).safeParse(data);
|
|
68
|
+
const result = frontmatterZod(model, objects).safeParse(data);
|
|
24
69
|
if (result.success) return { ok: true };
|
|
25
70
|
|
|
26
71
|
const errors: string[] = [];
|
|
27
72
|
const seen = new Set<string>();
|
|
28
73
|
for (const issue of result.error.issues) {
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
if (
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
errors.push(`${name} must be a media URL`);
|
|
38
|
-
} else {
|
|
39
|
-
errors.push(`${name} must be a ${expectedTypeFor(field.kind)}`);
|
|
40
|
-
}
|
|
74
|
+
const key = pathKey(issue.path);
|
|
75
|
+
if (!key || seen.has(key)) continue;
|
|
76
|
+
const top = String(issue.path[0] ?? "");
|
|
77
|
+
if (!model.fields[top] && issue.path.length === 1) continue;
|
|
78
|
+
const msg = issueMessage(issue, model, data, objects);
|
|
79
|
+
if (!msg) continue;
|
|
80
|
+
seen.add(key);
|
|
81
|
+
errors.push(msg);
|
|
41
82
|
}
|
|
42
83
|
// Never fail open: if every issue was filtered above, still report invalid.
|
|
43
84
|
// Unreachable through the public API today (normalizeFrontmatter always yields
|
|
@@ -61,6 +102,7 @@ export function validateBlocks(
|
|
|
61
102
|
blocks: unknown,
|
|
62
103
|
catalog: BlockSnapshot[],
|
|
63
104
|
prefix = "blocks",
|
|
105
|
+
objects: ObjectSnapshot[] = [],
|
|
64
106
|
): ValidationResult {
|
|
65
107
|
if (!Array.isArray(blocks)) return { ok: true };
|
|
66
108
|
const byName = new Map(catalog.map((b) => [b.name, b]));
|
|
@@ -72,7 +114,7 @@ export function validateBlocks(
|
|
|
72
114
|
return;
|
|
73
115
|
}
|
|
74
116
|
const props = block.props && typeof block.props === "object" ? block.props : {};
|
|
75
|
-
const result = validateFrontmatter({ fields: def.fields }, props);
|
|
117
|
+
const result = validateFrontmatter({ fields: def.fields }, props, objects);
|
|
76
118
|
if (!result.ok) {
|
|
77
119
|
for (const err of result.errors) {
|
|
78
120
|
errors.push(`${prefix}[${i}].${err}`);
|
|
@@ -90,6 +132,7 @@ export function validateBlocks(
|
|
|
90
132
|
export function validateMdxFile(
|
|
91
133
|
model: Pick<ModelSnapshot, "fields">,
|
|
92
134
|
raw: string,
|
|
135
|
+
objects: ObjectSnapshot[] = [],
|
|
93
136
|
): ValidationResult {
|
|
94
137
|
let frontmatter: Frontmatter;
|
|
95
138
|
try {
|
|
@@ -98,13 +141,14 @@ export function validateMdxFile(
|
|
|
98
141
|
const msg = err instanceof Error ? err.message : "invalid";
|
|
99
142
|
return { ok: false, errors: [`frontmatter: ${msg}`] };
|
|
100
143
|
}
|
|
101
|
-
return validateFrontmatter(model, frontmatter);
|
|
144
|
+
return validateFrontmatter(model, frontmatter, objects);
|
|
102
145
|
}
|
|
103
146
|
|
|
104
147
|
export function validateDocFile(
|
|
105
148
|
model: Pick<ModelSnapshot, "fields" | "format">,
|
|
106
149
|
raw: string,
|
|
107
150
|
catalog: BlockSnapshot[] = [],
|
|
151
|
+
objects: ObjectSnapshot[] = [],
|
|
108
152
|
): ValidationResult {
|
|
109
153
|
const format = modelFormat(model);
|
|
110
154
|
let frontmatter: Frontmatter;
|
|
@@ -115,11 +159,16 @@ export function validateDocFile(
|
|
|
115
159
|
const label = format === "page" ? "JSON" : "frontmatter";
|
|
116
160
|
return { ok: false, errors: [msg.startsWith("JSON:") ? msg : `${label}: ${msg}`] };
|
|
117
161
|
}
|
|
118
|
-
const base = validateFrontmatter(model, frontmatter);
|
|
162
|
+
const base = validateFrontmatter(model, frontmatter, objects);
|
|
119
163
|
if (format !== "page") return base;
|
|
120
164
|
const blockField = Object.entries(model.fields).find(([, f]) => f.kind === "blocks");
|
|
121
165
|
if (!blockField) return base;
|
|
122
|
-
const blockResult = validateBlocks(
|
|
166
|
+
const blockResult = validateBlocks(
|
|
167
|
+
frontmatter[blockField[0]],
|
|
168
|
+
catalog,
|
|
169
|
+
blockField[0],
|
|
170
|
+
objects,
|
|
171
|
+
);
|
|
123
172
|
if (base.ok && blockResult.ok) return { ok: true };
|
|
124
173
|
return {
|
|
125
174
|
ok: false,
|
|
@@ -132,20 +181,58 @@ export type ContentFileResult =
|
|
|
132
181
|
| { path: string; type: string; ok: false; errors: string[] }
|
|
133
182
|
| { path: string; type: null; ok: true; skipped: true };
|
|
134
183
|
|
|
184
|
+
function collectParsedDocs(input: {
|
|
185
|
+
models: ModelSnapshot[];
|
|
186
|
+
files: Array<{ path: string; raw: string }>;
|
|
187
|
+
}): Array<{ type: string; slug: string; frontmatter: Frontmatter }> {
|
|
188
|
+
const docs: Array<{ type: string; slug: string; frontmatter: Frontmatter }> = [];
|
|
189
|
+
for (const file of input.files) {
|
|
190
|
+
const hit = matchAnyModelPath(input.models, file.path);
|
|
191
|
+
if (!hit) continue;
|
|
192
|
+
const model = input.models.find((m) => m.type === hit.type);
|
|
193
|
+
if (!model) continue;
|
|
194
|
+
try {
|
|
195
|
+
const { frontmatter } = parseDoc(file.raw, modelFormat(model));
|
|
196
|
+
docs.push({ type: hit.type, slug: hit.slug, frontmatter });
|
|
197
|
+
} catch {
|
|
198
|
+
// Unparseable files are skipped for the relation pass.
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return docs;
|
|
202
|
+
}
|
|
203
|
+
|
|
135
204
|
/** Validate many repo-relative files against a model set. Order is preserved. */
|
|
136
205
|
export function validateContentTree(input: {
|
|
137
206
|
models: ModelSnapshot[];
|
|
138
207
|
files: Array<{ path: string; raw: string }>;
|
|
139
208
|
catalog?: BlockSnapshot[];
|
|
209
|
+
objects?: ObjectSnapshot[];
|
|
140
210
|
}): ContentFileResult[] {
|
|
141
211
|
const catalog = input.catalog ?? [];
|
|
212
|
+
const objects = input.objects ?? [];
|
|
213
|
+
const docs = collectParsedDocs({ models: input.models, files: input.files });
|
|
214
|
+
const relationRows = relationProblems({
|
|
215
|
+
schema: { version: 1, models: input.models, blocks: catalog, objects },
|
|
216
|
+
docs,
|
|
217
|
+
});
|
|
218
|
+
const relationByDoc = new Map(
|
|
219
|
+
relationRows.map((row) => [`${row.type}/${row.slug}`, row.errors] as const),
|
|
220
|
+
);
|
|
221
|
+
|
|
142
222
|
return input.files.map((file) => {
|
|
143
223
|
const hit = matchAnyModelPath(input.models, file.path);
|
|
144
224
|
if (!hit) return { path: file.path, type: null, ok: true, skipped: true };
|
|
145
225
|
const model = input.models.find((m) => m.type === hit.type);
|
|
146
226
|
if (!model) return { path: file.path, type: null, ok: true, skipped: true };
|
|
147
|
-
const result = validateDocFile(model, file.raw, catalog);
|
|
148
|
-
|
|
149
|
-
|
|
227
|
+
const result = validateDocFile(model, file.raw, catalog, objects);
|
|
228
|
+
const relationErrors = relationByDoc.get(`${hit.type}/${hit.slug}`) ?? [];
|
|
229
|
+
if (result.ok && relationErrors.length === 0) {
|
|
230
|
+
return { path: file.path, type: model.type, ok: true };
|
|
231
|
+
}
|
|
232
|
+
const errors = [
|
|
233
|
+
...(result.ok ? [] : result.errors),
|
|
234
|
+
...relationErrors,
|
|
235
|
+
];
|
|
236
|
+
return { path: file.path, type: model.type, ok: false, errors };
|
|
150
237
|
});
|
|
151
238
|
}
|