@lacecms/content 0.1.0-alpha.1
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/LICENSE +21 -0
- package/dist/index.d.ts +399 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +935 -0
- package/dist/index.js.map +1 -0
- package/package.json +31 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,935 @@
|
|
|
1
|
+
import * as v from "valibot";
|
|
2
|
+
/** Thrown when a field builder receives a non-portable or contradictory option. */
|
|
3
|
+
export class FieldConfigurationError extends Error {
|
|
4
|
+
constructor(message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "FieldConfigurationError";
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function fail(path, message) {
|
|
10
|
+
throw new FieldConfigurationError(`${path} ${message}`);
|
|
11
|
+
}
|
|
12
|
+
function isPlainObject(value) {
|
|
13
|
+
const prototype = Object.getPrototypeOf(value);
|
|
14
|
+
return prototype === null || prototype === Object.prototype;
|
|
15
|
+
}
|
|
16
|
+
function isArrayIndex(key) {
|
|
17
|
+
if (key === "0") {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
if (!/^[1-9][0-9]*$/u.test(key)) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
const index = Number(key);
|
|
24
|
+
return Number.isSafeInteger(index) && index < 4_294_967_295;
|
|
25
|
+
}
|
|
26
|
+
function normalizeJsonValue(value, path, ancestors) {
|
|
27
|
+
if (value === null || typeof value === "boolean" || typeof value === "string") {
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
if (typeof value === "number") {
|
|
31
|
+
if (!Number.isFinite(value)) {
|
|
32
|
+
fail(path, "must not contain a non-finite number.");
|
|
33
|
+
}
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
if (typeof value !== "object") {
|
|
37
|
+
fail(path, "must be JSON-serializable.");
|
|
38
|
+
}
|
|
39
|
+
if (ancestors.has(value)) {
|
|
40
|
+
fail(path, "must not contain a cyclic reference.");
|
|
41
|
+
}
|
|
42
|
+
ancestors.add(value);
|
|
43
|
+
try {
|
|
44
|
+
if (Array.isArray(value)) {
|
|
45
|
+
const keys = Reflect.ownKeys(value);
|
|
46
|
+
for (const key of keys) {
|
|
47
|
+
if (key === "length") {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (typeof key !== "string" || !isArrayIndex(key)) {
|
|
51
|
+
fail(path, "must not contain non-index array properties or symbols.");
|
|
52
|
+
}
|
|
53
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
54
|
+
if (descriptor === undefined) {
|
|
55
|
+
fail(`${path}[${key}]`, "must contain an own data property.");
|
|
56
|
+
}
|
|
57
|
+
if ("get" in descriptor || "set" in descriptor) {
|
|
58
|
+
fail(`${path}[${key}]`, "must not contain accessor properties.");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const normalized = [];
|
|
62
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
63
|
+
if (!Object.hasOwn(value, index)) {
|
|
64
|
+
fail(`${path}[${index}]`, "must not contain sparse array entries.");
|
|
65
|
+
}
|
|
66
|
+
normalized.push(normalizeJsonValue(value[index], `${path}[${index}]`, ancestors));
|
|
67
|
+
}
|
|
68
|
+
return normalized;
|
|
69
|
+
}
|
|
70
|
+
if (!isPlainObject(value)) {
|
|
71
|
+
fail(path, "must not contain a non-plain object.");
|
|
72
|
+
}
|
|
73
|
+
const normalized = {};
|
|
74
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
75
|
+
if (typeof key !== "string") {
|
|
76
|
+
fail(path, "must not contain symbol keys.");
|
|
77
|
+
}
|
|
78
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
79
|
+
if (descriptor === undefined) {
|
|
80
|
+
fail(`${path}.${key}`, "must contain an own data property.");
|
|
81
|
+
}
|
|
82
|
+
if ("get" in descriptor || "set" in descriptor) {
|
|
83
|
+
fail(`${path}.${key}`, "must not contain accessor properties.");
|
|
84
|
+
}
|
|
85
|
+
if (!descriptor.enumerable) {
|
|
86
|
+
fail(`${path}.${key}`, "must contain an enumerable data property.");
|
|
87
|
+
}
|
|
88
|
+
normalized[key] = normalizeJsonValue(descriptor.value, `${path}.${key}`, ancestors);
|
|
89
|
+
}
|
|
90
|
+
return normalized;
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
ancestors.delete(value);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function normalizeOptions(options) {
|
|
97
|
+
const normalized = normalizeJsonValue(options, "options", new WeakSet());
|
|
98
|
+
if (normalized === null || Array.isArray(normalized) || typeof normalized !== "object") {
|
|
99
|
+
fail("options", "must be a plain object.");
|
|
100
|
+
}
|
|
101
|
+
return normalized;
|
|
102
|
+
}
|
|
103
|
+
function deepFreeze(value) {
|
|
104
|
+
if (value !== null && typeof value === "object" && !Object.isFrozen(value)) {
|
|
105
|
+
for (const child of Object.values(value)) {
|
|
106
|
+
deepFreeze(child);
|
|
107
|
+
}
|
|
108
|
+
Object.freeze(value);
|
|
109
|
+
}
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
112
|
+
function readOptionalString(options, name) {
|
|
113
|
+
const value = options[name];
|
|
114
|
+
if (value === undefined) {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
if (typeof value !== "string") {
|
|
118
|
+
fail(`options.${name}`, "must be a string.");
|
|
119
|
+
}
|
|
120
|
+
return value;
|
|
121
|
+
}
|
|
122
|
+
function readOptionalBoolean(options, name) {
|
|
123
|
+
const value = options[name];
|
|
124
|
+
if (value === undefined) {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
if (typeof value !== "boolean") {
|
|
128
|
+
fail(`options.${name}`, "must be a boolean.");
|
|
129
|
+
}
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
132
|
+
function readOptionalFiniteNumber(options, name, integer) {
|
|
133
|
+
const value = options[name];
|
|
134
|
+
if (value === undefined) {
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
if (typeof value !== "number" ||
|
|
138
|
+
!Number.isFinite(value) ||
|
|
139
|
+
(integer && !Number.isSafeInteger(value)) ||
|
|
140
|
+
(integer && value < 0)) {
|
|
141
|
+
fail(`options.${name}`, integer ? "must be a non-negative safe integer." : "must be a finite number.");
|
|
142
|
+
}
|
|
143
|
+
return value;
|
|
144
|
+
}
|
|
145
|
+
function addCommonOptions(definition, options) {
|
|
146
|
+
const label = readOptionalString(options, "label");
|
|
147
|
+
if (label !== undefined) {
|
|
148
|
+
definition.label = label;
|
|
149
|
+
}
|
|
150
|
+
const description = readOptionalString(options, "description");
|
|
151
|
+
if (description !== undefined) {
|
|
152
|
+
definition.description = description;
|
|
153
|
+
}
|
|
154
|
+
definition.required = readOptionalBoolean(options, "required") ?? false;
|
|
155
|
+
}
|
|
156
|
+
function assertAllowedOptions(options, allowed) {
|
|
157
|
+
for (const key of Object.keys(options)) {
|
|
158
|
+
if (!allowed.includes(key)) {
|
|
159
|
+
fail(`options.${key}`, "is not supported by this field type.");
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function addDefault(definition, options, isValid, message) {
|
|
164
|
+
if (!Object.hasOwn(options, "defaultValue")) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const value = options.defaultValue;
|
|
168
|
+
if (value === undefined || !isValid(value)) {
|
|
169
|
+
fail("options.defaultValue", message);
|
|
170
|
+
}
|
|
171
|
+
definition.defaultValue = value;
|
|
172
|
+
}
|
|
173
|
+
function buildStringField(type, input) {
|
|
174
|
+
const options = normalizeOptions(input ?? {});
|
|
175
|
+
assertAllowedOptions(options, [
|
|
176
|
+
"defaultValue",
|
|
177
|
+
"description",
|
|
178
|
+
"label",
|
|
179
|
+
"maxLength",
|
|
180
|
+
"minLength",
|
|
181
|
+
"required",
|
|
182
|
+
]);
|
|
183
|
+
const definition = { type };
|
|
184
|
+
addCommonOptions(definition, options);
|
|
185
|
+
const minLength = readOptionalFiniteNumber(options, "minLength", true);
|
|
186
|
+
const maxLength = readOptionalFiniteNumber(options, "maxLength", true);
|
|
187
|
+
if (minLength !== undefined && maxLength !== undefined && minLength > maxLength) {
|
|
188
|
+
fail("options", "must not set minLength greater than maxLength.");
|
|
189
|
+
}
|
|
190
|
+
if (minLength !== undefined) {
|
|
191
|
+
definition.minLength = minLength;
|
|
192
|
+
}
|
|
193
|
+
if (maxLength !== undefined) {
|
|
194
|
+
definition.maxLength = maxLength;
|
|
195
|
+
}
|
|
196
|
+
addDefault(definition, options, (value) => typeof value === "string" &&
|
|
197
|
+
(minLength === undefined || value.length >= minLength) &&
|
|
198
|
+
(maxLength === undefined || value.length <= maxLength), "must be a string that conforms to the declared length constraints.");
|
|
199
|
+
return deepFreeze(definition);
|
|
200
|
+
}
|
|
201
|
+
function buildStringValueField(type, input) {
|
|
202
|
+
const options = normalizeOptions(input ?? {});
|
|
203
|
+
assertAllowedOptions(options, ["defaultValue", "description", "label", "required"]);
|
|
204
|
+
const definition = { type };
|
|
205
|
+
addCommonOptions(definition, options);
|
|
206
|
+
addDefault(definition, options, (value) => typeof value === "string", "must be a string.");
|
|
207
|
+
return deepFreeze(definition);
|
|
208
|
+
}
|
|
209
|
+
/** Builders for portable Lace field descriptors. */
|
|
210
|
+
export const field = {
|
|
211
|
+
/** Define a single-line text field. */
|
|
212
|
+
text(options) {
|
|
213
|
+
return buildStringField("text", options);
|
|
214
|
+
},
|
|
215
|
+
/** Define a multiline text field. */
|
|
216
|
+
textarea(options) {
|
|
217
|
+
return buildStringField("textarea", options);
|
|
218
|
+
},
|
|
219
|
+
/** Define a safe Tiptap JSON field. */
|
|
220
|
+
richText(options) {
|
|
221
|
+
const normalized = normalizeOptions(options ?? {});
|
|
222
|
+
assertAllowedOptions(normalized, ["defaultValue", "description", "label", "required"]);
|
|
223
|
+
const definition = { type: "richText" };
|
|
224
|
+
addCommonOptions(definition, normalized);
|
|
225
|
+
addDefault(definition, normalized, (value) => isSafeRichTextDocument(value), "must be a safe rich-text document.");
|
|
226
|
+
return deepFreeze(definition);
|
|
227
|
+
},
|
|
228
|
+
/** Define a finite numeric field. */
|
|
229
|
+
number(options) {
|
|
230
|
+
const normalized = normalizeOptions(options ?? {});
|
|
231
|
+
assertAllowedOptions(normalized, [
|
|
232
|
+
"defaultValue",
|
|
233
|
+
"description",
|
|
234
|
+
"label",
|
|
235
|
+
"max",
|
|
236
|
+
"min",
|
|
237
|
+
"required",
|
|
238
|
+
]);
|
|
239
|
+
const definition = { type: "number" };
|
|
240
|
+
addCommonOptions(definition, normalized);
|
|
241
|
+
const min = readOptionalFiniteNumber(normalized, "min", false);
|
|
242
|
+
const max = readOptionalFiniteNumber(normalized, "max", false);
|
|
243
|
+
if (min !== undefined && max !== undefined && min > max) {
|
|
244
|
+
fail("options", "must not set min greater than max.");
|
|
245
|
+
}
|
|
246
|
+
if (min !== undefined) {
|
|
247
|
+
definition.min = min;
|
|
248
|
+
}
|
|
249
|
+
if (max !== undefined) {
|
|
250
|
+
definition.max = max;
|
|
251
|
+
}
|
|
252
|
+
addDefault(definition, normalized, (value) => typeof value === "number" &&
|
|
253
|
+
Number.isFinite(value) &&
|
|
254
|
+
(min === undefined || value >= min) &&
|
|
255
|
+
(max === undefined || value <= max), "must be a finite number that conforms to the declared numeric bounds.");
|
|
256
|
+
return deepFreeze(definition);
|
|
257
|
+
},
|
|
258
|
+
/** Define a boolean field. */
|
|
259
|
+
boolean(options) {
|
|
260
|
+
const normalized = normalizeOptions(options ?? {});
|
|
261
|
+
assertAllowedOptions(normalized, ["defaultValue", "description", "label", "required"]);
|
|
262
|
+
const definition = { type: "boolean" };
|
|
263
|
+
addCommonOptions(definition, normalized);
|
|
264
|
+
addDefault(definition, normalized, (value) => typeof value === "boolean", "must be a boolean.");
|
|
265
|
+
return deepFreeze(definition);
|
|
266
|
+
},
|
|
267
|
+
/** Define a date string field. */
|
|
268
|
+
date(options) {
|
|
269
|
+
return buildStringValueField("date", options);
|
|
270
|
+
},
|
|
271
|
+
/** Define a datetime string field. */
|
|
272
|
+
datetime(options) {
|
|
273
|
+
return buildStringValueField("datetime", options);
|
|
274
|
+
},
|
|
275
|
+
/** Define a select field with a non-empty unique list of string choices. */
|
|
276
|
+
select(options) {
|
|
277
|
+
const normalized = normalizeOptions(options);
|
|
278
|
+
assertAllowedOptions(normalized, [
|
|
279
|
+
"defaultValue",
|
|
280
|
+
"description",
|
|
281
|
+
"label",
|
|
282
|
+
"options",
|
|
283
|
+
"required",
|
|
284
|
+
]);
|
|
285
|
+
const rawChoices = normalized.options;
|
|
286
|
+
if (!Array.isArray(rawChoices) || rawChoices.length === 0) {
|
|
287
|
+
fail("options.options", "must be a non-empty array of strings.");
|
|
288
|
+
}
|
|
289
|
+
const choices = [];
|
|
290
|
+
const seen = new Set();
|
|
291
|
+
for (const [index, choice] of rawChoices.entries()) {
|
|
292
|
+
if (typeof choice !== "string") {
|
|
293
|
+
fail(`options.options[${index}]`, "must be a string.");
|
|
294
|
+
}
|
|
295
|
+
if (seen.has(choice)) {
|
|
296
|
+
fail(`options.options[${index}]`, "must not duplicate a select choice.");
|
|
297
|
+
}
|
|
298
|
+
seen.add(choice);
|
|
299
|
+
choices.push(choice);
|
|
300
|
+
}
|
|
301
|
+
const definition = { options: choices, type: "select" };
|
|
302
|
+
addCommonOptions(definition, normalized);
|
|
303
|
+
addDefault(definition, normalized, (value) => typeof value === "string" && seen.has(value), "must be one of the declared select choices.");
|
|
304
|
+
return deepFreeze(definition);
|
|
305
|
+
},
|
|
306
|
+
/** Define a URL string field; URL protocol validation is added in Session 2B. */
|
|
307
|
+
url(options) {
|
|
308
|
+
return buildStringValueField("url", options);
|
|
309
|
+
},
|
|
310
|
+
/** Define a field holding a media identifier. */
|
|
311
|
+
media(options) {
|
|
312
|
+
return buildStringValueField("media", options);
|
|
313
|
+
},
|
|
314
|
+
};
|
|
315
|
+
/**
|
|
316
|
+
* Project a field descriptor into a detached, immutable JSON-safe admin form
|
|
317
|
+
* representation. Runtime schemas and callbacks are deliberately absent.
|
|
318
|
+
*/
|
|
319
|
+
export function toFieldMetadata(definition) {
|
|
320
|
+
const normalized = normalizeJsonValue(definition, "definition", new WeakSet());
|
|
321
|
+
if (normalized === null || Array.isArray(normalized) || typeof normalized !== "object") {
|
|
322
|
+
fail("definition", "must be a field-definition object.");
|
|
323
|
+
}
|
|
324
|
+
return deepFreeze(normalized);
|
|
325
|
+
}
|
|
326
|
+
/** Thrown when submitted content fails a portable validation contract. */
|
|
327
|
+
export class ContentValidationError extends Error {
|
|
328
|
+
issues;
|
|
329
|
+
constructor(issues) {
|
|
330
|
+
super(issues.map((issue) => `${formatValidationPath(issue.path)} ${issue.message}`).join("; "));
|
|
331
|
+
this.name = "ContentValidationError";
|
|
332
|
+
this.issues = issues;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function formatValidationPath(path) {
|
|
336
|
+
if (path.length === 0) {
|
|
337
|
+
return "$";
|
|
338
|
+
}
|
|
339
|
+
return path.reduce((result, segment) => {
|
|
340
|
+
return typeof segment === "number" ? `${result}[${segment}]` : `${result}.${segment}`;
|
|
341
|
+
}, "$");
|
|
342
|
+
}
|
|
343
|
+
function invalid(path, code, message) {
|
|
344
|
+
throw new ContentValidationError([{ code, message, path }]);
|
|
345
|
+
}
|
|
346
|
+
function isRecord(value) {
|
|
347
|
+
return (value !== null && typeof value === "object" && !Array.isArray(value) && isPlainObject(value));
|
|
348
|
+
}
|
|
349
|
+
function assertExactKeys(value, required, optional, path) {
|
|
350
|
+
const allowed = new Set([...required, ...optional]);
|
|
351
|
+
for (const key of Object.keys(value)) {
|
|
352
|
+
if (!allowed.has(key)) {
|
|
353
|
+
invalid([...path, key], "unknown_key", "is not permitted.");
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
for (const key of required) {
|
|
357
|
+
if (!Object.hasOwn(value, key)) {
|
|
358
|
+
invalid([...path, key], "missing_key", "is required.");
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
/** Returns whether a URL is permitted in Lace URL fields and rich-text links. */
|
|
363
|
+
export function isSafeUrl(value) {
|
|
364
|
+
if (typeof value !== "string" ||
|
|
365
|
+
value.length === 0 ||
|
|
366
|
+
/\s/u.test(value) ||
|
|
367
|
+
value.includes(String.fromCodePoint(0))) {
|
|
368
|
+
return false;
|
|
369
|
+
}
|
|
370
|
+
if (value.startsWith("#")) {
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
if (value.startsWith("/")) {
|
|
374
|
+
return !value.startsWith("//");
|
|
375
|
+
}
|
|
376
|
+
return /^(?:https?:\/\/[^/?#\s]+(?:[/?#][^\s]*)?|mailto:[^\s@]+@[^\s@]+|tel:\+?[0-9(). -]+)$/u.test(value);
|
|
377
|
+
}
|
|
378
|
+
function assertMarks(value, path) {
|
|
379
|
+
if (!Array.isArray(value)) {
|
|
380
|
+
invalid(path, "invalid_marks", "must be an array.");
|
|
381
|
+
}
|
|
382
|
+
for (const [index, mark] of value.entries()) {
|
|
383
|
+
const markPath = [...path, index];
|
|
384
|
+
if (!isRecord(mark) || typeof mark.type !== "string") {
|
|
385
|
+
invalid(markPath, "invalid_mark", "must be a mark object.");
|
|
386
|
+
}
|
|
387
|
+
if (mark.type === "link") {
|
|
388
|
+
assertExactKeys(mark, ["attrs", "type"], [], markPath);
|
|
389
|
+
if (!isRecord(mark.attrs)) {
|
|
390
|
+
invalid([...markPath, "attrs"], "invalid_link_attributes", "must be an object.");
|
|
391
|
+
}
|
|
392
|
+
assertExactKeys(mark.attrs, ["href"], [], [...markPath, "attrs"]);
|
|
393
|
+
if (!isSafeUrl(mark.attrs.href)) {
|
|
394
|
+
invalid([...markPath, "attrs", "href"], "unsafe_url", "must be an approved URL.");
|
|
395
|
+
}
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
if (mark.type === "bold" ||
|
|
399
|
+
mark.type === "code" ||
|
|
400
|
+
mark.type === "italic" ||
|
|
401
|
+
mark.type === "strike") {
|
|
402
|
+
assertExactKeys(mark, ["type"], [], markPath);
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
invalid([...markPath, "type"], "unknown_mark", "is not permitted.");
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
function assertRichTextChildren(value, path, allowed) {
|
|
409
|
+
if (!Array.isArray(value)) {
|
|
410
|
+
invalid(path, "invalid_content", "must be an array.");
|
|
411
|
+
}
|
|
412
|
+
for (const [index, child] of value.entries()) {
|
|
413
|
+
assertRichTextNode(child, [...path, index]);
|
|
414
|
+
if (!allowed.includes(child.type)) {
|
|
415
|
+
invalid([...path, index, "type"], "invalid_child", "is not permitted in this node.");
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
function assertRichTextNode(value, path) {
|
|
420
|
+
if (!isRecord(value) || typeof value.type !== "string") {
|
|
421
|
+
invalid(path, "invalid_node", "must be a rich-text node object.");
|
|
422
|
+
}
|
|
423
|
+
if (value.type === "text") {
|
|
424
|
+
assertExactKeys(value, ["text", "type"], ["marks"], path);
|
|
425
|
+
if (typeof value.text !== "string") {
|
|
426
|
+
invalid([...path, "text"], "invalid_text", "must be a string.");
|
|
427
|
+
}
|
|
428
|
+
if (Object.hasOwn(value, "marks")) {
|
|
429
|
+
assertMarks(value.marks, [...path, "marks"]);
|
|
430
|
+
}
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
if (value.type === "hardBreak") {
|
|
434
|
+
assertExactKeys(value, ["type"], [], path);
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
if (value.type === "paragraph") {
|
|
438
|
+
assertExactKeys(value, ["type"], ["content"], path);
|
|
439
|
+
if (Object.hasOwn(value, "content")) {
|
|
440
|
+
assertRichTextChildren(value.content, [...path, "content"], ["hardBreak", "text"]);
|
|
441
|
+
}
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
if (value.type === "heading") {
|
|
445
|
+
assertExactKeys(value, ["attrs", "type"], ["content"], path);
|
|
446
|
+
if (!isRecord(value.attrs)) {
|
|
447
|
+
invalid([...path, "attrs"], "invalid_heading_attributes", "must be an object.");
|
|
448
|
+
}
|
|
449
|
+
assertExactKeys(value.attrs, ["level"], [], [...path, "attrs"]);
|
|
450
|
+
if (value.attrs.level !== 1 && value.attrs.level !== 2 && value.attrs.level !== 3) {
|
|
451
|
+
invalid([...path, "attrs", "level"], "invalid_heading_level", "must be 1, 2, or 3.");
|
|
452
|
+
}
|
|
453
|
+
if (Object.hasOwn(value, "content")) {
|
|
454
|
+
assertRichTextChildren(value.content, [...path, "content"], ["hardBreak", "text"]);
|
|
455
|
+
}
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
if (value.type === "bulletList" || value.type === "orderedList") {
|
|
459
|
+
assertExactKeys(value, ["content", "type"], [], path);
|
|
460
|
+
assertRichTextChildren(value.content, [...path, "content"], ["listItem"]);
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
if (value.type === "listItem") {
|
|
464
|
+
assertExactKeys(value, ["content", "type"], [], path);
|
|
465
|
+
assertRichTextChildren(value.content, [...path, "content"], ["bulletList", "orderedList", "paragraph"]);
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
if (value.type === "blockquote") {
|
|
469
|
+
assertExactKeys(value, ["content", "type"], [], path);
|
|
470
|
+
assertRichTextChildren(value.content, [...path, "content"], ["bulletList", "orderedList", "paragraph"]);
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
invalid([...path, "type"], "unknown_node", "is not permitted.");
|
|
474
|
+
}
|
|
475
|
+
/** Validates and returns a closed, safe rich-text document. */
|
|
476
|
+
export function validateRichTextDocument(value) {
|
|
477
|
+
if (!isRecord(value) || value.type !== "doc") {
|
|
478
|
+
invalid([], "invalid_document", "must be a doc node.");
|
|
479
|
+
}
|
|
480
|
+
assertExactKeys(value, ["content", "type"], [], []);
|
|
481
|
+
assertRichTextChildren(value.content, ["content"], ["blockquote", "bulletList", "heading", "orderedList", "paragraph"]);
|
|
482
|
+
return value;
|
|
483
|
+
}
|
|
484
|
+
function isSafeRichTextDocument(value) {
|
|
485
|
+
try {
|
|
486
|
+
validateRichTextDocument(value);
|
|
487
|
+
return true;
|
|
488
|
+
}
|
|
489
|
+
catch (error) {
|
|
490
|
+
if (error instanceof ContentValidationError) {
|
|
491
|
+
return false;
|
|
492
|
+
}
|
|
493
|
+
throw error;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
/** Visits a field descriptor exhaustively. */
|
|
497
|
+
export function visitFieldDefinition(definition, visitor) {
|
|
498
|
+
switch (definition.type) {
|
|
499
|
+
case "boolean":
|
|
500
|
+
return visitor.boolean(definition);
|
|
501
|
+
case "date":
|
|
502
|
+
return visitor.date(definition);
|
|
503
|
+
case "datetime":
|
|
504
|
+
return visitor.datetime(definition);
|
|
505
|
+
case "media":
|
|
506
|
+
return visitor.media(definition);
|
|
507
|
+
case "number":
|
|
508
|
+
return visitor.number(definition);
|
|
509
|
+
case "richText":
|
|
510
|
+
return visitor.richText(definition);
|
|
511
|
+
case "select":
|
|
512
|
+
return visitor.select(definition);
|
|
513
|
+
case "text":
|
|
514
|
+
return visitor.text(definition);
|
|
515
|
+
case "textarea":
|
|
516
|
+
return visitor.textarea(definition);
|
|
517
|
+
case "url":
|
|
518
|
+
return visitor.url(definition);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
function isCalendarDate(value) {
|
|
522
|
+
if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}$/u.test(value)) {
|
|
523
|
+
return false;
|
|
524
|
+
}
|
|
525
|
+
const parts = value.split("-");
|
|
526
|
+
const year = Number(parts[0]);
|
|
527
|
+
const month = Number(parts[1]);
|
|
528
|
+
const day = Number(parts[2]);
|
|
529
|
+
const date = new Date(Date.UTC(year, month - 1, day));
|
|
530
|
+
return (date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day);
|
|
531
|
+
}
|
|
532
|
+
function isUtcIsoDatetime(value) {
|
|
533
|
+
return (typeof value === "string" &&
|
|
534
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?Z$/u.test(value) &&
|
|
535
|
+
!Number.isNaN(Date.parse(value)));
|
|
536
|
+
}
|
|
537
|
+
function isFieldValue(definition, value) {
|
|
538
|
+
return visitFieldDefinition(definition, {
|
|
539
|
+
boolean: () => typeof value === "boolean",
|
|
540
|
+
date: () => isCalendarDate(value),
|
|
541
|
+
datetime: () => isUtcIsoDatetime(value),
|
|
542
|
+
media: () => typeof value === "string" && value.length > 0,
|
|
543
|
+
number: (fieldDefinition) => typeof value === "number" &&
|
|
544
|
+
Number.isFinite(value) &&
|
|
545
|
+
(fieldDefinition.min === undefined || value >= fieldDefinition.min) &&
|
|
546
|
+
(fieldDefinition.max === undefined || value <= fieldDefinition.max),
|
|
547
|
+
richText: () => isSafeRichTextDocument(value),
|
|
548
|
+
select: (fieldDefinition) => typeof value === "string" && fieldDefinition.options.includes(value),
|
|
549
|
+
text: (fieldDefinition) => typeof value === "string" &&
|
|
550
|
+
(fieldDefinition.minLength === undefined || value.length >= fieldDefinition.minLength) &&
|
|
551
|
+
(fieldDefinition.maxLength === undefined || value.length <= fieldDefinition.maxLength),
|
|
552
|
+
textarea: (fieldDefinition) => typeof value === "string" &&
|
|
553
|
+
(fieldDefinition.minLength === undefined || value.length >= fieldDefinition.minLength) &&
|
|
554
|
+
(fieldDefinition.maxLength === undefined || value.length <= fieldDefinition.maxLength),
|
|
555
|
+
url: () => isSafeUrl(value),
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
/** Compiles a field definition into its Valibot runtime schema. */
|
|
559
|
+
export function compileFieldSchema(definition) {
|
|
560
|
+
return v.custom((value) => isFieldValue(definition, value));
|
|
561
|
+
}
|
|
562
|
+
/** Validates a submitted field value and returns its inferred value type. */
|
|
563
|
+
export function validateFieldValue(definition, value, path = []) {
|
|
564
|
+
const result = v.safeParse(compileFieldSchema(definition), value);
|
|
565
|
+
if (!result.success) {
|
|
566
|
+
invalid(path, "invalid_field_value", "does not conform to its field definition.");
|
|
567
|
+
}
|
|
568
|
+
return result.output;
|
|
569
|
+
}
|
|
570
|
+
/** Validates field data for either an incomplete draft or a publish candidate. */
|
|
571
|
+
export function validateModelFields(definitions, value, mode, path = []) {
|
|
572
|
+
if (!isRecord(value)) {
|
|
573
|
+
invalid(path, "invalid_fields", "must be an object.");
|
|
574
|
+
}
|
|
575
|
+
for (const key of Object.keys(value)) {
|
|
576
|
+
if (!Object.hasOwn(definitions, key)) {
|
|
577
|
+
invalid([...path, key], "unknown_field", "is not defined by this model.");
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
const output = {};
|
|
581
|
+
for (const [key, definition] of Object.entries(definitions)) {
|
|
582
|
+
if (Object.hasOwn(value, key)) {
|
|
583
|
+
output[key] = validateFieldValue(definition, value[key], [...path, key]);
|
|
584
|
+
continue;
|
|
585
|
+
}
|
|
586
|
+
if (Object.hasOwn(definition, "defaultValue")) {
|
|
587
|
+
output[key] = validateFieldValue(definition, definition.defaultValue, [...path, key]);
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
if (mode === "publish" && definition.required) {
|
|
591
|
+
invalid([...path, key], "missing_required_field", "is required for publication.");
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return output;
|
|
595
|
+
}
|
|
596
|
+
const BLOCK_TYPE_PATTERN = /^[A-Za-z][A-Za-z0-9-]*$/u;
|
|
597
|
+
const BLOCK_KEY_PATTERN = /^(?:[A-Za-z][A-Za-z0-9-]*|[0-7][0-9A-HJKMNP-TV-Z]{25})$/u;
|
|
598
|
+
const FIELD_KEY_PATTERN = /^[A-Za-z][A-Za-z0-9]*$/u;
|
|
599
|
+
/** Thrown when a block definition or registry is not portable or internally consistent. */
|
|
600
|
+
export class BlockConfigurationError extends Error {
|
|
601
|
+
constructor(message) {
|
|
602
|
+
super(message);
|
|
603
|
+
this.name = "BlockConfigurationError";
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
function blockFail(message) {
|
|
607
|
+
throw new BlockConfigurationError(message);
|
|
608
|
+
}
|
|
609
|
+
function normalizeBlockFields(value) {
|
|
610
|
+
if (!isRecord(value)) {
|
|
611
|
+
blockFail("fields must be a plain object.");
|
|
612
|
+
}
|
|
613
|
+
const fields = {};
|
|
614
|
+
for (const [key, definition] of Object.entries(value)) {
|
|
615
|
+
if (!FIELD_KEY_PATTERN.test(key)) {
|
|
616
|
+
blockFail(`fields.${key} must be a valid field key.`);
|
|
617
|
+
}
|
|
618
|
+
try {
|
|
619
|
+
fields[key] = toFieldMetadata(definition);
|
|
620
|
+
}
|
|
621
|
+
catch (error) {
|
|
622
|
+
if (error instanceof FieldConfigurationError) {
|
|
623
|
+
blockFail(`fields.${key} must be a field definition.`);
|
|
624
|
+
}
|
|
625
|
+
throw error;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
return deepFreeze(fields);
|
|
629
|
+
}
|
|
630
|
+
/** Defines a versioned block using only portable field descriptors. */
|
|
631
|
+
export function defineBlock(input) {
|
|
632
|
+
if (!isRecord(input)) {
|
|
633
|
+
blockFail("block must be a plain object.");
|
|
634
|
+
}
|
|
635
|
+
for (const key of Object.keys(input)) {
|
|
636
|
+
if (!new Set(["defaultValue", "description", "fields", "label", "type", "version"]).has(key)) {
|
|
637
|
+
blockFail(`block.${key} is not permitted.`);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
if (typeof input.type !== "string" || !BLOCK_TYPE_PATTERN.test(input.type)) {
|
|
641
|
+
blockFail("type must begin with an ASCII letter and contain only letters, digits, and hyphens.");
|
|
642
|
+
}
|
|
643
|
+
if (typeof input.version !== "number" ||
|
|
644
|
+
!Number.isSafeInteger(input.version) ||
|
|
645
|
+
input.version < 1) {
|
|
646
|
+
blockFail("version must be a positive integer.");
|
|
647
|
+
}
|
|
648
|
+
if (input.label !== undefined && typeof input.label !== "string") {
|
|
649
|
+
blockFail("label must be a string when provided.");
|
|
650
|
+
}
|
|
651
|
+
if (input.description !== undefined && typeof input.description !== "string") {
|
|
652
|
+
blockFail("description must be a string when provided.");
|
|
653
|
+
}
|
|
654
|
+
const fields = normalizeBlockFields(input.fields);
|
|
655
|
+
let defaultValue;
|
|
656
|
+
if (input.defaultValue !== undefined) {
|
|
657
|
+
try {
|
|
658
|
+
const normalized = normalizeJsonValue(input.defaultValue, "defaultValue", new WeakSet());
|
|
659
|
+
if (!isRecord(normalized)) {
|
|
660
|
+
blockFail("defaultValue must be a plain object.");
|
|
661
|
+
}
|
|
662
|
+
defaultValue = deepFreeze(validateModelFields(fields, normalized, "draft"));
|
|
663
|
+
}
|
|
664
|
+
catch (error) {
|
|
665
|
+
if (error instanceof ContentValidationError) {
|
|
666
|
+
blockFail("defaultValue must conform to the block fields.");
|
|
667
|
+
}
|
|
668
|
+
throw error;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
const definition = {
|
|
672
|
+
...(defaultValue === undefined ? {} : { defaultValue }),
|
|
673
|
+
...(input.description === undefined ? {} : { description: input.description }),
|
|
674
|
+
fields,
|
|
675
|
+
...(input.label === undefined ? {} : { label: input.label }),
|
|
676
|
+
type: input.type,
|
|
677
|
+
validate: (value, mode) => {
|
|
678
|
+
if (!isRecord(value)) {
|
|
679
|
+
return validateModelFields(fields, value, mode);
|
|
680
|
+
}
|
|
681
|
+
const withDefault = defaultValue === undefined ? value : { ...defaultValue, ...value };
|
|
682
|
+
return validateModelFields(fields, withDefault, mode);
|
|
683
|
+
},
|
|
684
|
+
version: input.version,
|
|
685
|
+
};
|
|
686
|
+
return deepFreeze(definition);
|
|
687
|
+
}
|
|
688
|
+
/** Projects a block definition into detached, JSON-safe metadata. */
|
|
689
|
+
export function toBlockMetadata(definition) {
|
|
690
|
+
const metadata = {
|
|
691
|
+
...(definition.defaultValue === undefined ? {} : { defaultValue: definition.defaultValue }),
|
|
692
|
+
...(definition.description === undefined ? {} : { description: definition.description }),
|
|
693
|
+
fields: Object.fromEntries(Object.entries(definition.fields).map(([key, fieldDefinition]) => [
|
|
694
|
+
key,
|
|
695
|
+
toFieldMetadata(fieldDefinition),
|
|
696
|
+
])),
|
|
697
|
+
...(definition.label === undefined ? {} : { label: definition.label }),
|
|
698
|
+
type: definition.type,
|
|
699
|
+
version: definition.version,
|
|
700
|
+
};
|
|
701
|
+
return deepFreeze(metadata);
|
|
702
|
+
}
|
|
703
|
+
/** Creates a registry and rejects duplicate current block types. */
|
|
704
|
+
export function defineBlockRegistry(blocks) {
|
|
705
|
+
if (!Array.isArray(blocks)) {
|
|
706
|
+
blockFail("blocks must be an array of block definitions.");
|
|
707
|
+
}
|
|
708
|
+
const definitions = new Map();
|
|
709
|
+
for (const definition of blocks) {
|
|
710
|
+
if (definitions.has(definition.type)) {
|
|
711
|
+
blockFail(`blocks contains duplicate block type "${definition.type}".`);
|
|
712
|
+
}
|
|
713
|
+
definitions.set(definition.type, definition);
|
|
714
|
+
}
|
|
715
|
+
const normalized = deepFreeze([...definitions.values()]);
|
|
716
|
+
return Object.freeze({ blocks: normalized, get: (type) => definitions.get(type) });
|
|
717
|
+
}
|
|
718
|
+
/** Projects a registry to immutable JSON-safe metadata for admin and public clients. */
|
|
719
|
+
export function toBlockRegistryMetadata(registry) {
|
|
720
|
+
return deepFreeze(registry.blocks.map(toBlockMetadata));
|
|
721
|
+
}
|
|
722
|
+
/** Validates one block's field record, applying its whole-record default first. */
|
|
723
|
+
export function validateBlockData(definition, value, mode, path = []) {
|
|
724
|
+
if (!isRecord(value)) {
|
|
725
|
+
return validateModelFields(definition.fields, value, mode, path);
|
|
726
|
+
}
|
|
727
|
+
const withDefault = definition.defaultValue === undefined ? value : { ...definition.defaultValue, ...value };
|
|
728
|
+
return validateModelFields(definition.fields, withDefault, mode, path);
|
|
729
|
+
}
|
|
730
|
+
/** Starter blocks implemented exclusively through the public block and field DSL. */
|
|
731
|
+
export const builtInBlocks = deepFreeze({
|
|
732
|
+
cta: defineBlock({
|
|
733
|
+
description: "A heading and a link that prompts the reader to act.",
|
|
734
|
+
fields: {
|
|
735
|
+
actionLabel: field.text({ required: true }),
|
|
736
|
+
actionUrl: field.url({ required: true }),
|
|
737
|
+
body: field.richText(),
|
|
738
|
+
heading: field.text({ required: true }),
|
|
739
|
+
},
|
|
740
|
+
label: "Call to action",
|
|
741
|
+
type: "cta",
|
|
742
|
+
version: 1,
|
|
743
|
+
}),
|
|
744
|
+
hero: defineBlock({
|
|
745
|
+
description: "Large heading with optional text, image, and action.",
|
|
746
|
+
fields: {
|
|
747
|
+
body: field.richText(),
|
|
748
|
+
eyebrow: field.text(),
|
|
749
|
+
heading: field.text({ required: true }),
|
|
750
|
+
image: field.media(),
|
|
751
|
+
primaryActionLabel: field.text(),
|
|
752
|
+
primaryActionUrl: field.url(),
|
|
753
|
+
},
|
|
754
|
+
label: "Hero",
|
|
755
|
+
type: "hero",
|
|
756
|
+
version: 1,
|
|
757
|
+
}),
|
|
758
|
+
image: defineBlock({
|
|
759
|
+
description: "A single image with alt text and an optional caption.",
|
|
760
|
+
fields: {
|
|
761
|
+
alt: field.text({ required: true }),
|
|
762
|
+
caption: field.text(),
|
|
763
|
+
media: field.media({ required: true }),
|
|
764
|
+
},
|
|
765
|
+
label: "Image",
|
|
766
|
+
type: "image",
|
|
767
|
+
version: 1,
|
|
768
|
+
}),
|
|
769
|
+
quote: defineBlock({
|
|
770
|
+
description: "A quotation with optional attribution.",
|
|
771
|
+
fields: {
|
|
772
|
+
attribution: field.text(),
|
|
773
|
+
quote: field.text({ required: true }),
|
|
774
|
+
},
|
|
775
|
+
label: "Quote",
|
|
776
|
+
type: "quote",
|
|
777
|
+
version: 1,
|
|
778
|
+
}),
|
|
779
|
+
richText: defineBlock({
|
|
780
|
+
description: "Formatted text with headings, lists, and links.",
|
|
781
|
+
fields: { content: field.richText({ required: true }) },
|
|
782
|
+
label: "Rich text",
|
|
783
|
+
type: "richText",
|
|
784
|
+
version: 1,
|
|
785
|
+
}),
|
|
786
|
+
});
|
|
787
|
+
/** Maximum title length shared by draft, publish, REST, and admin validation. */
|
|
788
|
+
export const MAX_TITLE_LENGTH = 200;
|
|
789
|
+
/** Maximum slug length shared by publish, REST, and admin validation. */
|
|
790
|
+
export const MAX_SLUG_LENGTH = 100;
|
|
791
|
+
/** Maximum flat top-level block count for one entry. */
|
|
792
|
+
export const MAX_TOP_LEVEL_BLOCKS = 200;
|
|
793
|
+
/** Maximum UTF-8 bytes in fields JSON or a single block data JSON value. */
|
|
794
|
+
export const MAX_JSON_BYTES = 1_000_000;
|
|
795
|
+
/** Returns canonical JSON with recursively sorted object keys. */
|
|
796
|
+
export function canonicalizeJson(value) {
|
|
797
|
+
if (value === null ||
|
|
798
|
+
typeof value === "boolean" ||
|
|
799
|
+
typeof value === "number" ||
|
|
800
|
+
typeof value === "string") {
|
|
801
|
+
return JSON.stringify(value);
|
|
802
|
+
}
|
|
803
|
+
if (Array.isArray(value)) {
|
|
804
|
+
return `[${value.map(canonicalizeJson).join(",")}]`;
|
|
805
|
+
}
|
|
806
|
+
const object = value;
|
|
807
|
+
const entries = Object.keys(object)
|
|
808
|
+
.sort()
|
|
809
|
+
.map((key) => `${JSON.stringify(key)}:${canonicalizeJson(object[key])}`);
|
|
810
|
+
return `{${entries.join(",")}}`;
|
|
811
|
+
}
|
|
812
|
+
/** Gets the UTF-8 byte count of portable JSON through canonical serialization. */
|
|
813
|
+
export function canonicalJsonByteLength(value) {
|
|
814
|
+
const TextEncoderConstructor = globalThis.TextEncoder;
|
|
815
|
+
if (TextEncoderConstructor === undefined) {
|
|
816
|
+
throw new Error("TextEncoder is required for canonical JSON byte measurement.");
|
|
817
|
+
}
|
|
818
|
+
return new TextEncoderConstructor().encode(canonicalizeJson(value)).byteLength;
|
|
819
|
+
}
|
|
820
|
+
function getWebCrypto() {
|
|
821
|
+
const crypto = globalThis.crypto;
|
|
822
|
+
if (crypto === undefined) {
|
|
823
|
+
throw new Error("Web Crypto is required for canonical JSON hashing.");
|
|
824
|
+
}
|
|
825
|
+
return crypto;
|
|
826
|
+
}
|
|
827
|
+
/** Hashes canonical JSON UTF-8 bytes with Web Crypto SHA-256 as lowercase hexadecimal. */
|
|
828
|
+
export async function sha256CanonicalJson(value, webCrypto = getWebCrypto()) {
|
|
829
|
+
const TextEncoderConstructor = globalThis.TextEncoder;
|
|
830
|
+
if (TextEncoderConstructor === undefined) {
|
|
831
|
+
throw new Error("TextEncoder is required for canonical JSON hashing.");
|
|
832
|
+
}
|
|
833
|
+
const digest = new Uint8Array(await webCrypto.subtle.digest("SHA-256", new TextEncoderConstructor().encode(canonicalizeJson(value))));
|
|
834
|
+
return Array.from(digest, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
835
|
+
}
|
|
836
|
+
function assertJsonSize(value, path) {
|
|
837
|
+
if (canonicalJsonByteLength(value) > MAX_JSON_BYTES) {
|
|
838
|
+
invalid(path, "json_too_large", `must not exceed ${MAX_JSON_BYTES} UTF-8 bytes.`);
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
/** Validates system entry data and shared payload limits for a draft or publish candidate. */
|
|
842
|
+
export function validateEntryPayload(input, mode) {
|
|
843
|
+
if (input.kind !== "collection" && input.kind !== "page") {
|
|
844
|
+
invalid(["kind"], "invalid_model_kind", "must be page or collection.");
|
|
845
|
+
}
|
|
846
|
+
if (typeof input.title !== "string" || input.title.length === 0) {
|
|
847
|
+
invalid(["title"], "missing_title", "is required.");
|
|
848
|
+
}
|
|
849
|
+
if (input.title.length > MAX_TITLE_LENGTH) {
|
|
850
|
+
invalid(["title"], "title_too_long", `must not exceed ${MAX_TITLE_LENGTH} characters.`);
|
|
851
|
+
}
|
|
852
|
+
if (input.slug !== undefined &&
|
|
853
|
+
(typeof input.slug !== "string" || input.slug.length > MAX_SLUG_LENGTH)) {
|
|
854
|
+
invalid(["slug"], "slug_too_long", `must not exceed ${MAX_SLUG_LENGTH} characters.`);
|
|
855
|
+
}
|
|
856
|
+
if (mode === "publish" &&
|
|
857
|
+
input.kind === "collection" &&
|
|
858
|
+
(input.slug === undefined || input.slug.length === 0)) {
|
|
859
|
+
invalid(["slug"], "missing_slug", "is required for collection publication.");
|
|
860
|
+
}
|
|
861
|
+
if (!Array.isArray(input.blocks)) {
|
|
862
|
+
invalid(["blocks"], "invalid_blocks", "must be an array.");
|
|
863
|
+
}
|
|
864
|
+
if (input.blocks.length > MAX_TOP_LEVEL_BLOCKS) {
|
|
865
|
+
invalid(["blocks"], "too_many_blocks", `must not contain more than ${MAX_TOP_LEVEL_BLOCKS} blocks.`);
|
|
866
|
+
}
|
|
867
|
+
assertJsonSize(input.fields, ["fields"]);
|
|
868
|
+
for (const [index, block] of input.blocks.entries()) {
|
|
869
|
+
if (!isRecord(block) || !Object.hasOwn(block, "data")) {
|
|
870
|
+
invalid(["blocks", index], "invalid_block", "must contain JSON data.");
|
|
871
|
+
}
|
|
872
|
+
assertJsonSize(block.data, ["blocks", index, "data"]);
|
|
873
|
+
}
|
|
874
|
+
return input;
|
|
875
|
+
}
|
|
876
|
+
/** Validates all system, model-field, and ordered block data for one content entry. */
|
|
877
|
+
export function validateEntryAggregate(input, model, registry, mode) {
|
|
878
|
+
if (input.kind !== model.kind) {
|
|
879
|
+
invalid(["kind"], "model_kind_mismatch", "must match the content model kind.");
|
|
880
|
+
}
|
|
881
|
+
validateEntryPayload({
|
|
882
|
+
blocks: input.blocks,
|
|
883
|
+
fields: {},
|
|
884
|
+
kind: input.kind,
|
|
885
|
+
...(input.slug === undefined ? {} : { slug: input.slug }),
|
|
886
|
+
title: input.title,
|
|
887
|
+
}, mode);
|
|
888
|
+
const fields = validateModelFields(model.fields, input.fields, mode, ["fields"]);
|
|
889
|
+
assertJsonSize(fields, ["fields"]);
|
|
890
|
+
const keys = new Set();
|
|
891
|
+
const blocks = [];
|
|
892
|
+
for (const [index, block] of input.blocks.entries()) {
|
|
893
|
+
const path = ["blocks", index];
|
|
894
|
+
if (!isRecord(block)) {
|
|
895
|
+
invalid(path, "invalid_block", "must be an object.");
|
|
896
|
+
}
|
|
897
|
+
assertExactKeys(block, ["data", "key", "schemaVersion", "type"], [], path);
|
|
898
|
+
if (typeof block.key !== "string" || !BLOCK_KEY_PATTERN.test(block.key)) {
|
|
899
|
+
invalid([...path, "key"], "invalid_block_key", "must be a stable block key.");
|
|
900
|
+
}
|
|
901
|
+
if (keys.has(block.key)) {
|
|
902
|
+
invalid([...path, "key"], "duplicate_block_key", "must be unique within the entry.");
|
|
903
|
+
}
|
|
904
|
+
keys.add(block.key);
|
|
905
|
+
if (typeof block.type !== "string") {
|
|
906
|
+
invalid([...path, "type"], "invalid_block_type", "must be a string.");
|
|
907
|
+
}
|
|
908
|
+
if (!model.blocks.includes(block.type)) {
|
|
909
|
+
invalid([...path, "type"], "disallowed_block_type", "is not allowed by this model.");
|
|
910
|
+
}
|
|
911
|
+
const definition = registry.get(block.type);
|
|
912
|
+
if (definition === undefined) {
|
|
913
|
+
invalid([...path, "type"], "unregistered_block_type", "is not registered.");
|
|
914
|
+
}
|
|
915
|
+
if (block.schemaVersion !== definition.version) {
|
|
916
|
+
invalid([...path, "schemaVersion"], "stale_block_version", "does not match the registered version.");
|
|
917
|
+
}
|
|
918
|
+
assertJsonSize(block.data, [...path, "data"]);
|
|
919
|
+
const data = validateBlockData(definition, block.data, mode, [...path, "data"]);
|
|
920
|
+
blocks.push(deepFreeze({
|
|
921
|
+
data: data,
|
|
922
|
+
key: block.key,
|
|
923
|
+
schemaVersion: block.schemaVersion,
|
|
924
|
+
type: block.type,
|
|
925
|
+
}));
|
|
926
|
+
}
|
|
927
|
+
return deepFreeze({
|
|
928
|
+
blocks: deepFreeze(blocks),
|
|
929
|
+
fields,
|
|
930
|
+
kind: input.kind,
|
|
931
|
+
...(input.slug === undefined ? {} : { slug: input.slug }),
|
|
932
|
+
title: input.title,
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
//# sourceMappingURL=index.js.map
|