@backmatter/core 0.0.33 → 0.0.35

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backmatter/core",
3
- "version": "0.0.33",
3
+ "version": "0.0.35",
4
4
  "description": "Experimental Backmatter SDK. APIs will break. Not ready for production.",
5
5
  "type": "module",
6
6
  "publishConfig": {
package/src/block.ts CHANGED
@@ -52,13 +52,15 @@ export const RESERVED_BLOCK_PROP = "data";
52
52
 
53
53
  type Simplify<T> = { -readonly [K in keyof T]: T[K] } & {};
54
54
 
55
- type ScalarOf<K extends FieldKind> = K extends "number"
55
+ type ValueOfField<F> = F extends { kind: "number" }
56
56
  ? number
57
- : K extends "boolean"
57
+ : F extends { kind: "boolean" }
58
58
  ? boolean
59
- : K extends "blocks"
59
+ : F extends { kind: "blocks" }
60
60
  ? BlockInstance[]
61
- : string;
61
+ : F extends { options: infer O extends readonly [string, ...string[]] }
62
+ ? O[number]
63
+ : string;
62
64
 
63
65
  type IsRequired<F> = F extends { required?: infer R }
64
66
  ? [Exclude<R, undefined>] extends [true]
@@ -72,12 +74,12 @@ type IsRequired<F> = F extends { required?: infer R }
72
74
  */
73
75
  export type InferBlockProps<Fields extends Record<string, FieldDef>, Data = undefined> = Simplify<
74
76
  {
75
- [N in keyof Fields as IsRequired<Fields[N]> extends true ? N : never]: ScalarOf<
76
- Fields[N]["kind"]
77
+ [N in keyof Fields as IsRequired<Fields[N]> extends true ? N : never]: ValueOfField<
78
+ Fields[N]
77
79
  >;
78
80
  } & {
79
- [N in keyof Fields as IsRequired<Fields[N]> extends true ? never : N]?: ScalarOf<
80
- Fields[N]["kind"]
81
+ [N in keyof Fields as IsRequired<Fields[N]> extends true ? never : N]?: ValueOfField<
82
+ Fields[N]
81
83
  >;
82
84
  } & ([Data] extends [undefined] ? {} : { data: Data })
83
85
  >;
package/src/color.ts ADDED
@@ -0,0 +1,40 @@
1
+ // packages/core/src/color.ts
2
+
3
+ function isHex(value: string): boolean {
4
+ const match = /^#([0-9a-fA-F]+)$/.exec(value);
5
+ if (!match) {
6
+ return false;
7
+ }
8
+ const digits = match[1];
9
+ const len = digits.length;
10
+ if (len !== 3 && len !== 4 && len !== 6 && len !== 8) {
11
+ return false;
12
+ }
13
+ return true;
14
+ }
15
+
16
+ const NUM = String.raw`(?:[+-]?(?:\d+\.?\d*|\.\d+))`;
17
+ const PCT = String.raw`${NUM}%`;
18
+ const CHANNEL = String.raw`(?:${NUM}|${PCT})`;
19
+ const HUE = String.raw`${NUM}(?:deg|rad|grad|turn)?`;
20
+
21
+ const FUNCTIONAL = [
22
+ String.raw`^rgb\(\s*${CHANNEL}\s*,\s*${CHANNEL}\s*,\s*${CHANNEL}\s*(?:,\s*${CHANNEL})?\s*\)$`,
23
+ String.raw`^rgba\(\s*${CHANNEL}\s*,\s*${CHANNEL}\s*,\s*${CHANNEL}\s*,\s*${CHANNEL}\s*\)$`,
24
+ String.raw`^rgb\(\s*${CHANNEL}\s+${CHANNEL}\s+${CHANNEL}(?:\s*/\s*${CHANNEL})?\s*\)$`,
25
+ String.raw`^rgba\(\s*${CHANNEL}\s+${CHANNEL}\s+${CHANNEL}(?:\s*/\s*${CHANNEL})?\s*\)$`,
26
+ String.raw`^hsl\(\s*${HUE}\s*,\s*${CHANNEL}\s*,\s*${CHANNEL}\s*(?:,\s*${CHANNEL})?\s*\)$`,
27
+ String.raw`^hsla\(\s*${HUE}\s*,\s*${CHANNEL}\s*,\s*${CHANNEL}\s*,\s*${CHANNEL}\s*\)$`,
28
+ String.raw`^hsl\(\s*${HUE}\s+${CHANNEL}\s+${CHANNEL}(?:\s*/\s*${CHANNEL})?\s*\)$`,
29
+ String.raw`^hsla\(\s*${HUE}\s+${CHANNEL}\s+${CHANNEL}(?:\s*/\s*${CHANNEL})?\s*\)$`,
30
+ String.raw`^hsla\(\s*${HUE}\s+${CHANNEL}\s+${CHANNEL}\s*/\s*${CHANNEL}\s*\)$`,
31
+ String.raw`^oklch\(\s*${CHANNEL}\s+${CHANNEL}\s+${HUE}(?:\s*/\s*${CHANNEL})?\s*\)$`,
32
+ String.raw`^oklch\(\s*${CHANNEL}\s*,\s*${CHANNEL}\s*,\s*${HUE}(?:\s*/\s*${CHANNEL})?\s*\)$`,
33
+ ].map((source) => new RegExp(source, "i"));
34
+
35
+ export function isColorString(value: string): boolean {
36
+ if (isHex(value)) {
37
+ return true;
38
+ }
39
+ return FUNCTIONAL.some((pattern) => pattern.test(value));
40
+ }
package/src/doc.ts CHANGED
@@ -5,6 +5,8 @@ export type BackmatterDocSummary = {
5
5
  slug: string;
6
6
  /** Card-level fields when the loader already parsed the file. */
7
7
  frontmatter?: Record<string, unknown>;
8
+ /** Markdown when the loader already read the file. */
9
+ body?: string;
8
10
  };
9
11
 
10
12
  export type BackmatterDoc = {
@@ -0,0 +1,24 @@
1
+ // packages/core/src/document-label.ts
2
+ const TITLE_KEYS = ["title", "name", "headline", "heading", "label"] as const;
3
+ const TITLE_KEY_SET = new Set<string>(TITLE_KEYS);
4
+
5
+ export function documentLabel(
6
+ frontmatter: Record<string, unknown>,
7
+ model: { fields?: Record<string, { kind: string }> } | null | undefined,
8
+ ): string | null {
9
+ const pick = (key: string) => {
10
+ const v = frontmatter[key];
11
+ return typeof v === "string" && v.trim() ? v.trim() : null;
12
+ };
13
+ for (const key of TITLE_KEYS) {
14
+ const v = pick(key);
15
+ if (v) return v;
16
+ }
17
+ for (const [key, field] of Object.entries(model?.fields ?? {})) {
18
+ if (TITLE_KEY_SET.has(key)) continue;
19
+ if (field.kind !== "text") continue;
20
+ const v = pick(key);
21
+ if (v) return v;
22
+ }
23
+ return null;
24
+ }
package/src/fields.ts CHANGED
@@ -5,12 +5,16 @@ export type FieldKind =
5
5
  | "image"
6
6
  | "media"
7
7
  | "relation"
8
+ | "link"
8
9
  | "number"
9
10
  | "boolean"
10
11
  | "blocks"
11
12
  | "object"
12
13
  | "array";
13
14
 
15
+ /** Equality filter on scalar frontmatter keys for a file-backed relation target. */
16
+ export type RelationWhere = Record<string, string | number | boolean>;
17
+
14
18
  /**
15
19
  * Field definition. Generic parameters only exist for type inference
16
20
  * (`InferFrontmatter`); the runtime object is `{ kind, required?, relationTo? }`.
@@ -34,6 +38,17 @@ export type FieldDef<
34
38
  * back to the default input for the kind.
35
39
  */
36
40
  input?: string;
41
+ /** Allowed values for a text field stored as a string. */
42
+ options?: readonly string[];
43
+ /** For link fields: internal document targets vs external URLs. */
44
+ to?: "internal" | "external";
45
+ /** For link fields: allowed target model types. */
46
+ models?: string[];
47
+ min?: number;
48
+ max?: number;
49
+ step?: number;
50
+ where?: RelationWhere;
51
+ multiple?: true;
37
52
  };
38
53
 
39
54
  export type FieldSnapshot = {
@@ -43,30 +58,250 @@ export type FieldSnapshot = {
43
58
  object?: string;
44
59
  fields?: Record<string, FieldSnapshot>;
45
60
  input?: string;
61
+ options?: string[];
62
+ to?: "internal" | "external";
63
+ models?: string[];
64
+ min?: number;
65
+ max?: number;
66
+ step?: number;
67
+ where?: RelationWhere;
68
+ multiple?: true;
46
69
  };
47
70
 
48
71
  type ObjectLike = { name: string; fields: Record<string, FieldDef> };
49
72
 
50
73
  type Opts<R extends boolean> = { required?: R; input?: string };
51
74
 
75
+ type TextOpts<R extends boolean> = Opts<R> & { options?: string[] };
76
+
77
+ type NumberOpts<R extends boolean> = Opts<R> & {
78
+ min?: number;
79
+ max?: number;
80
+ step?: number;
81
+ };
82
+
83
+ type RelationModelInput = {
84
+ type: string;
85
+ fields: Record<string, FieldDef>;
86
+ external?: boolean;
87
+ };
88
+
89
+ type LinkModelInput = RelationModelInput & { route?: string | false };
90
+
91
+ type LinkTo = "internal" | "external";
92
+
93
+ type LinkOptsBase<R extends boolean> = Opts<R> & { to?: LinkTo };
94
+
95
+ type LinkOptsNoModels<R extends boolean> = LinkOptsBase<R>;
96
+
97
+ type LinkOptsWithStringModels<R extends boolean> = LinkOptsBase<R> & {
98
+ to?: "internal";
99
+ models: readonly [string, ...string[]];
100
+ };
101
+
102
+ type LinkOptsWithModelModels<R extends boolean> = LinkOptsBase<R> & {
103
+ to?: "internal";
104
+ models: readonly [LinkModelInput, ...LinkModelInput[]];
105
+ };
106
+
107
+ type LinkOptsWithSingleModelWhere<M extends LinkModelInput, R extends boolean> = LinkOptsBase<R> & {
108
+ to?: "internal";
109
+ models: readonly [M];
110
+ where?: RelationWhereForModel<M>;
111
+ };
112
+
113
+ type ScalarFieldKind = "text" | "number" | "boolean";
114
+
115
+ type ScalarValueOfField<F extends FieldDef> = F extends { kind: "number" }
116
+ ? number
117
+ : F extends { kind: "boolean" }
118
+ ? boolean
119
+ : F extends { options: infer O extends readonly [string, ...string[]] }
120
+ ? O[number]
121
+ : F extends { kind: "text" }
122
+ ? string
123
+ : never;
124
+
125
+ /** Equality filters on slug and scalar fields of a file-backed relation target. */
126
+ export type RelationWhereForModel<M extends RelationModelInput> = Partial<
127
+ { slug: string } & {
128
+ [K in keyof M["fields"] as M["fields"][K]["kind"] extends ScalarFieldKind
129
+ ? K
130
+ : never]: ScalarValueOfField<M["fields"][K]>;
131
+ }
132
+ >;
133
+
134
+ type RelationModelOpts<M extends RelationModelInput, R extends boolean> = Opts<R> & {
135
+ where: RelationWhereForModel<M>;
136
+ };
137
+
138
+ function isBlankString(value: string): boolean {
139
+ return value.trim() === "";
140
+ }
141
+
142
+ function assertTextOptions(options: string[]): string[] {
143
+ if (options.length === 0) {
144
+ throw new Error("field.text options must be a non-empty array of distinct non-blank strings");
145
+ }
146
+ const seen = new Set<string>();
147
+ for (const option of options) {
148
+ if (typeof option !== "string" || isBlankString(option)) {
149
+ throw new Error("field.text options must be a non-empty array of distinct non-blank strings");
150
+ }
151
+ if (seen.has(option)) {
152
+ throw new Error("field.text options must be a non-empty array of distinct non-blank strings");
153
+ }
154
+ seen.add(option);
155
+ }
156
+ return options;
157
+ }
158
+
159
+ function assertNumberConstraints(
160
+ opts: NumberOpts<boolean>,
161
+ ): { min: number; max: number; step?: number } | undefined {
162
+ const { min, max, step } = opts;
163
+ if (min === undefined && max === undefined && step === undefined) {
164
+ return undefined;
165
+ }
166
+ if (min === undefined || max === undefined) {
167
+ throw new Error("field.number min and max must be provided together");
168
+ }
169
+ if (!Number.isFinite(min) || !Number.isFinite(max)) {
170
+ throw new Error("field.number min and max must be finite");
171
+ }
172
+ if (min >= max) {
173
+ throw new Error("field.number min must be less than max");
174
+ }
175
+ if (step !== undefined) {
176
+ if (!Number.isFinite(step) || step <= 0 || step > max - min) {
177
+ throw new Error("field.number step must be finite, greater than 0, and at most max - min");
178
+ }
179
+ return { min, max, step };
180
+ }
181
+ return { min, max };
182
+ }
183
+
184
+ function assertLinkModelTypes(models: readonly (string | LinkModelInput)[]): string[] {
185
+ if (models.length === 0) {
186
+ throw new Error("field.link models must include at least one model");
187
+ }
188
+ const seen = new Set<string>();
189
+ const types: string[] = [];
190
+ for (const entry of models) {
191
+ if (typeof entry === "string") {
192
+ if (isBlankString(entry)) {
193
+ throw new Error("field.link models must include at least one distinct model type");
194
+ }
195
+ if (seen.has(entry)) {
196
+ throw new Error("field.link models must be distinct model type names");
197
+ }
198
+ seen.add(entry);
199
+ types.push(entry);
200
+ continue;
201
+ }
202
+ if (entry.external) {
203
+ throw new Error("field.link cannot include an external model");
204
+ }
205
+ if (entry.route === false) {
206
+ throw new Error("field.link model must have a route");
207
+ }
208
+ if (seen.has(entry.type)) {
209
+ throw new Error("field.link models must be distinct model type names");
210
+ }
211
+ seen.add(entry.type);
212
+ types.push(entry.type);
213
+ }
214
+ return types;
215
+ }
216
+
217
+ function parseLinkModels(value: unknown): string[] | undefined {
218
+ if (value === undefined) {
219
+ return undefined;
220
+ }
221
+ if (!Array.isArray(value)) {
222
+ throw new Error("invalid field.models");
223
+ }
224
+ if (value.length === 0) {
225
+ throw new Error("invalid field.models");
226
+ }
227
+ const seen = new Set<string>();
228
+ const models: string[] = [];
229
+ for (const item of value) {
230
+ if (typeof item !== "string" || isBlankString(item)) {
231
+ throw new Error("invalid field.models");
232
+ }
233
+ if (seen.has(item)) {
234
+ throw new Error("invalid field.models");
235
+ }
236
+ seen.add(item);
237
+ models.push(item);
238
+ }
239
+ return models;
240
+ }
241
+
242
+ function parseRelationWhere(value: unknown): RelationWhere | undefined {
243
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
244
+ return undefined;
245
+ }
246
+ const where: RelationWhere = {};
247
+ for (const [key, entry] of Object.entries(value as Record<string, unknown>)) {
248
+ if (typeof entry === "string" || typeof entry === "number" || typeof entry === "boolean") {
249
+ where[key] = entry;
250
+ } else {
251
+ return undefined;
252
+ }
253
+ }
254
+ return where;
255
+ }
256
+
52
257
  function base<K extends FieldKind, R extends boolean, To extends string>(
53
258
  kind: K,
54
- opts?: { required?: R; relationTo?: To; input?: string },
259
+ opts?: {
260
+ required?: R;
261
+ relationTo?: To;
262
+ input?: string;
263
+ options?: readonly string[];
264
+ min?: number;
265
+ max?: number;
266
+ step?: number;
267
+ where?: RelationWhere;
268
+ multiple?: boolean;
269
+ to?: LinkTo;
270
+ models?: string[];
271
+ },
55
272
  ): FieldDef<K, R, To> {
56
273
  return {
57
274
  kind,
58
275
  ...(opts?.required ? { required: opts.required } : {}),
59
276
  ...(opts?.relationTo ? { relationTo: opts.relationTo } : {}),
60
277
  ...(opts?.input ? { input: opts.input } : {}),
278
+ ...(opts?.options ? { options: opts.options } : {}),
279
+ ...(opts?.to ? { to: opts.to } : {}),
280
+ ...(opts?.models ? { models: opts.models } : {}),
281
+ ...(opts?.min !== undefined ? { min: opts.min } : {}),
282
+ ...(opts?.max !== undefined ? { max: opts.max } : {}),
283
+ ...(opts?.step !== undefined ? { step: opts.step } : {}),
284
+ ...(opts?.where ? { where: opts.where } : {}),
285
+ ...(opts?.multiple === true ? { multiple: true as const } : {}),
61
286
  } as FieldDef<K, R, To>;
62
287
  }
63
288
 
64
- function composite<K extends "object" | "array", R extends boolean>(
65
- kind: K,
66
- def: string | ObjectLike,
67
- opts?: Opts<R>,
68
- ): FieldDef<K, R> {
69
- const required = opts?.required ? { required: opts.required as R } : {};
289
+ type InlineFields = Record<string, FieldDef>;
290
+
291
+ type NamedComposite<K extends "object" | "array", R extends boolean> = FieldDef<K, R> & {
292
+ object: string;
293
+ };
294
+
295
+ type InlineComposite<K extends "object" | "array", R extends boolean, F> = {
296
+ kind: K;
297
+ object: string;
298
+ required?: R;
299
+ input?: string;
300
+ fields: F;
301
+ };
302
+
303
+ function buildComposite(kind: "object" | "array", def: string | ObjectLike, opts?: Opts<boolean>) {
304
+ const required = opts?.required ? { required: opts.required } : {};
70
305
  const input = opts?.input ? { input: opts.input } : {};
71
306
  if (typeof def === "string") {
72
307
  return { kind, object: def, ...required, ...input };
@@ -74,19 +309,168 @@ function composite<K extends "object" | "array", R extends boolean>(
74
309
  return { kind, object: def.name, fields: def.fields, ...required, ...input };
75
310
  }
76
311
 
312
+ function objectField<const F extends InlineFields, const R extends boolean = false>(
313
+ def: { name: string; fields: F },
314
+ opts?: Opts<R>,
315
+ ): InlineComposite<"object", R, F>;
316
+ function objectField<const R extends boolean = false>(
317
+ def: string,
318
+ opts?: Opts<R>,
319
+ ): NamedComposite<"object", R>;
320
+ function objectField(def: string | ObjectLike, opts?: Opts<boolean>): InlineComposite<"object", boolean, InlineFields> {
321
+ return buildComposite("object", def, opts) as InlineComposite<"object", boolean, InlineFields>;
322
+ }
323
+
324
+ function arrayField<const F extends InlineFields, const R extends boolean = false>(
325
+ def: { name: string; fields: F },
326
+ opts?: Opts<R>,
327
+ ): InlineComposite<"array", R, F>;
328
+ function arrayField<const R extends boolean = false>(
329
+ def: string,
330
+ opts?: Opts<R>,
331
+ ): NamedComposite<"array", R>;
332
+ function arrayField(def: string | ObjectLike, opts?: Opts<boolean>): InlineComposite<"array", boolean, InlineFields> {
333
+ return buildComposite("array", def, opts) as InlineComposite<"array", boolean, InlineFields>;
334
+ }
335
+
336
+ function relation<To extends string, const R extends boolean = false>(
337
+ to: To,
338
+ opts?: Opts<R>,
339
+ ): FieldDef<"relation", R, To>;
340
+ function relation<M extends RelationModelInput, const R extends boolean = false>(
341
+ model: M,
342
+ opts: RelationModelOpts<M, R>,
343
+ ): FieldDef<"relation", R, M["type"]>;
344
+ function relation(
345
+ to: string | RelationModelInput,
346
+ opts?: Opts<boolean> | RelationModelOpts<RelationModelInput, boolean>,
347
+ ): FieldDef<"relation", boolean, string> {
348
+ if (typeof to === "string") {
349
+ if (opts && "where" in opts && opts.where !== undefined) {
350
+ throw new Error("field.relation where requires a model target");
351
+ }
352
+ return base("relation", { ...opts, relationTo: to });
353
+ }
354
+ if (to.external) {
355
+ throw new Error("field.relation where cannot target an external model");
356
+ }
357
+ const where = parseRelationWhere(
358
+ opts !== undefined && "where" in opts ? opts.where : undefined,
359
+ );
360
+ if (!where) {
361
+ throw new Error("field.relation where must be a plain object");
362
+ }
363
+ return base("relation", {
364
+ relationTo: to.type,
365
+ where,
366
+ required: opts?.required,
367
+ input: opts?.input,
368
+ });
369
+ }
370
+
371
+ function link<const R extends boolean = false>(opts?: LinkOptsNoModels<R>): FieldDef<"link", R>;
372
+ function link<const M extends LinkModelInput, const R extends boolean = false>(
373
+ opts: LinkOptsWithSingleModelWhere<M, R>,
374
+ ): FieldDef<"link", R>;
375
+ function link<const R extends boolean = false>(
376
+ opts: LinkOptsWithStringModels<R>,
377
+ ): FieldDef<"link", R>;
378
+ function link<const R extends boolean = false>(
379
+ opts: LinkOptsWithModelModels<R>,
380
+ ): FieldDef<"link", R>;
381
+ function link(opts?: LinkOptsBase<boolean> & {
382
+ models?: readonly (string | LinkModelInput)[];
383
+ where?: RelationWhere;
384
+ }): FieldDef<"link", boolean> {
385
+ if (opts?.to === "external") {
386
+ if (opts.models !== undefined || opts.where !== undefined) {
387
+ throw new Error("field.link external cannot include models or where");
388
+ }
389
+ return base("link", {
390
+ to: "external",
391
+ required: opts.required,
392
+ input: opts.input,
393
+ });
394
+ }
395
+
396
+ if (opts?.where !== undefined) {
397
+ const models = opts.models;
398
+ if (!models || models.length !== 1 || typeof models[0] === "string") {
399
+ throw new Error("field.link where requires a model");
400
+ }
401
+ const where = parseRelationWhere(opts.where);
402
+ if (!where) {
403
+ throw new Error("field.link where must be a plain object");
404
+ }
405
+ const modelTypes = assertLinkModelTypes(models);
406
+ return base("link", {
407
+ to: opts.to,
408
+ models: modelTypes,
409
+ where,
410
+ required: opts.required,
411
+ input: opts.input,
412
+ });
413
+ }
414
+
415
+ if (opts?.models !== undefined) {
416
+ const modelTypes = assertLinkModelTypes(opts.models);
417
+ return base("link", {
418
+ to: opts.to,
419
+ models: modelTypes,
420
+ required: opts.required,
421
+ input: opts.input,
422
+ });
423
+ }
424
+
425
+ return base("link", {
426
+ to: opts?.to,
427
+ required: opts?.required,
428
+ input: opts?.input,
429
+ });
430
+ }
431
+
432
+ type TextFieldDef<R extends boolean, O extends readonly string[] | undefined = undefined> =
433
+ FieldDef<"text", R> & (O extends readonly string[] ? { readonly options: O } : {});
434
+
435
+ type MediaFieldDef<R extends boolean, M extends boolean = false> = FieldDef<"media", R> &
436
+ (M extends true ? { readonly multiple: true } : {});
437
+
77
438
  export const field = {
78
- text: <const R extends boolean = false>(opts?: Opts<R>) => base("text", opts),
439
+ text: <
440
+ const R extends boolean = false,
441
+ const O extends readonly string[] | undefined = undefined,
442
+ >(
443
+ opts?: Opts<R> & { options?: O },
444
+ ): TextFieldDef<R, O> => {
445
+ if (opts?.options !== undefined) {
446
+ if (opts.input !== undefined) {
447
+ throw new Error("field.text options cannot be combined with input");
448
+ }
449
+ const options = assertTextOptions([...opts.options]);
450
+ return base("text", { required: opts.required, options }) as TextFieldDef<R, O>;
451
+ }
452
+ return base("text", {
453
+ required: opts?.required,
454
+ ...(opts?.input ? { input: opts.input } : {}),
455
+ }) as TextFieldDef<R, O>;
456
+ },
79
457
  image: <const R extends boolean = false>(opts?: Opts<R>) => base("image", opts),
80
- media: <const R extends boolean = false>(opts?: Opts<R>) => base("media", opts),
81
- number: <const R extends boolean = false>(opts?: Opts<R>) => base("number", opts),
458
+ media: <const R extends boolean = false, const M extends boolean = false>(
459
+ opts?: Opts<R> & { multiple?: M },
460
+ ) => base("media", opts) as MediaFieldDef<R, M>,
461
+ number: <const R extends boolean = false>(opts?: NumberOpts<R>) => {
462
+ const bounds = opts ? assertNumberConstraints(opts) : undefined;
463
+ if (bounds) {
464
+ return base("number", { ...opts, ...bounds });
465
+ }
466
+ return base("number", opts);
467
+ },
82
468
  boolean: <const R extends boolean = false>(opts?: Opts<R>) => base("boolean", opts),
83
- relation: <To extends string, const R extends boolean = false>(to: To, opts?: Opts<R>) =>
84
- base("relation", { ...opts, relationTo: to }),
469
+ relation,
470
+ link,
85
471
  blocks: <const R extends boolean = false>(opts?: Opts<R>) => base("blocks", opts),
86
- object: <const R extends boolean = false>(def: string | ObjectLike, opts?: Opts<R>) =>
87
- composite("object", def, opts),
88
- array: <const R extends boolean = false>(def: string | ObjectLike, opts?: Opts<R>) =>
89
- composite("array", def, opts),
472
+ object: objectField,
473
+ array: arrayField,
90
474
  };
91
475
 
92
476
  export function serializeField(def: FieldDef): FieldSnapshot {
@@ -96,6 +480,14 @@ export function serializeField(def: FieldDef): FieldSnapshot {
96
480
  ...(def.relationTo ? { relationTo: def.relationTo } : {}),
97
481
  ...(def.object ? { object: def.object } : {}),
98
482
  ...(def.input ? { input: def.input } : {}),
483
+ ...(def.options ? { options: [...def.options] } : {}),
484
+ ...(def.min !== undefined ? { min: def.min } : {}),
485
+ ...(def.max !== undefined ? { max: def.max } : {}),
486
+ ...(def.step !== undefined ? { step: def.step } : {}),
487
+ ...(def.to ? { to: def.to } : {}),
488
+ ...(def.models ? { models: [...def.models] } : {}),
489
+ ...(def.where ? { where: def.where } : {}),
490
+ ...(def.multiple ? { multiple: true as const } : {}),
99
491
  };
100
492
  if (def.fields) {
101
493
  const fields: Record<string, FieldSnapshot> = {};
@@ -118,6 +510,7 @@ export function parseFieldSnapshot(input: unknown): FieldSnapshot {
118
510
  "image",
119
511
  "media",
120
512
  "relation",
513
+ "link",
121
514
  "number",
122
515
  "boolean",
123
516
  "blocks",
@@ -129,6 +522,12 @@ export function parseFieldSnapshot(input: unknown): FieldSnapshot {
129
522
  }
130
523
  const snap: FieldSnapshot = { kind: kind as FieldKind };
131
524
  if (value.required === true) snap.required = true;
525
+ if (value.multiple !== undefined) {
526
+ if (value.multiple !== true && value.multiple !== false) {
527
+ throw new Error("invalid field.multiple");
528
+ }
529
+ if (value.multiple === true) snap.multiple = true;
530
+ }
132
531
  if (typeof value.relationTo === "string" && value.relationTo.length > 0) {
133
532
  snap.relationTo = value.relationTo;
134
533
  }
@@ -138,6 +537,39 @@ export function parseFieldSnapshot(input: unknown): FieldSnapshot {
138
537
  if (typeof value.input === "string" && value.input.length > 0) {
139
538
  snap.input = value.input;
140
539
  }
540
+ if (value.to !== undefined) {
541
+ if (value.to !== "internal" && value.to !== "external") {
542
+ throw new Error("invalid field.to");
543
+ }
544
+ snap.to = value.to;
545
+ }
546
+ if (value.models !== undefined) {
547
+ snap.models = parseLinkModels(value.models);
548
+ }
549
+ if (Array.isArray(value.options)) {
550
+ const options: string[] = [];
551
+ for (const item of value.options) {
552
+ if (typeof item === "string" && !isBlankString(item)) {
553
+ options.push(item);
554
+ }
555
+ }
556
+ if (options.length > 0) {
557
+ snap.options = options;
558
+ }
559
+ }
560
+ if (typeof value.min === "number" && Number.isFinite(value.min)) {
561
+ snap.min = value.min;
562
+ }
563
+ if (typeof value.max === "number" && Number.isFinite(value.max)) {
564
+ snap.max = value.max;
565
+ }
566
+ if (typeof value.step === "number" && Number.isFinite(value.step)) {
567
+ snap.step = value.step;
568
+ }
569
+ const where = parseRelationWhere(value.where);
570
+ if (where) {
571
+ snap.where = where;
572
+ }
141
573
  if (value.fields && typeof value.fields === "object" && !Array.isArray(value.fields)) {
142
574
  const fields: Record<string, FieldSnapshot> = {};
143
575
  for (const [name, nested] of Object.entries(value.fields as Record<string, unknown>)) {