@formspec/dsl 0.1.0-alpha.2 → 0.1.0-alpha.23

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.
@@ -0,0 +1,999 @@
1
+ /**
2
+ * `@formspec/dsl` - DSL functions for defining FormSpec forms
3
+ *
4
+ * This package provides the builder functions for creating form specifications:
5
+ * - `field.*` - Field builders (text, number, boolean, enum, dynamicEnum)
6
+ * - `group()` - Visual grouping
7
+ * - `when()` + `is()` - Conditional visibility
8
+ * - `formspec()` - Top-level form definition
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { formspec, field, group, when, is } from "@formspec/dsl";
13
+ *
14
+ * const InvoiceForm = formspec(
15
+ * group("Customer",
16
+ * field.text("customerName", { label: "Customer Name" }),
17
+ * field.dynamicEnum("country", "fetch_countries", { label: "Country" }),
18
+ * ),
19
+ * group("Invoice Details",
20
+ * field.number("amount", { label: "Amount", min: 0 }),
21
+ * field.enum("status", ["draft", "sent", "paid"] as const),
22
+ * when(is("status", "draft"),
23
+ * field.text("internalNotes", { label: "Internal Notes" }),
24
+ * ),
25
+ * ),
26
+ * );
27
+ * ```
28
+ *
29
+ * @packageDocumentation
30
+ */
31
+
32
+ /**
33
+ * Union of all field types.
34
+ *
35
+ * @public
36
+ */
37
+ export declare type AnyField = TextField<string> | NumberField<string> | BooleanField<string> | StaticEnumField<string, readonly EnumOptionValue[]> | DynamicEnumField<string, string> | DynamicSchemaField<string> | ArrayField<string, readonly FormElement[]> | ObjectField<string, readonly FormElement[]>;
38
+
39
+ /**
40
+ * An array field containing repeating items.
41
+ *
42
+ * Use this for lists of values (e.g., multiple addresses, line items).
43
+ *
44
+ * @typeParam N - The field name (string literal type)
45
+ * @typeParam Items - The form elements that define each array item
46
+ *
47
+ * @public
48
+ */
49
+ export declare interface ArrayField<N extends string, Items extends readonly FormElement[]> {
50
+ /** Type discriminator for form elements */
51
+ readonly _type: "field";
52
+ /** Field type discriminator - identifies this as an array field */
53
+ readonly _field: "array";
54
+ /** Unique field identifier used as the schema key */
55
+ readonly name: N;
56
+ /** Form elements that define the schema for each array item */
57
+ readonly items: Items;
58
+ /** Display label for the field */
59
+ readonly label?: string;
60
+ /** Whether this field is required for form submission */
61
+ readonly required?: boolean;
62
+ /** Minimum number of items required */
63
+ readonly minItems?: number;
64
+ /** Maximum number of items allowed */
65
+ readonly maxItems?: number;
66
+ }
67
+
68
+ /**
69
+ * A boolean checkbox field.
70
+ *
71
+ * @typeParam N - The field name (string literal type)
72
+ *
73
+ * @public
74
+ */
75
+ export declare interface BooleanField<N extends string> {
76
+ /** Type discriminator for form elements */
77
+ readonly _type: "field";
78
+ /** Field type discriminator - identifies this as a boolean field */
79
+ readonly _field: "boolean";
80
+ /** Unique field identifier used as the schema key */
81
+ readonly name: N;
82
+ /** Display label for the field */
83
+ readonly label?: string;
84
+ /** Whether this field is required for form submission */
85
+ readonly required?: boolean;
86
+ }
87
+
88
+ /**
89
+ * Builds a schema type from extracted fields.
90
+ *
91
+ * Maps field names to their inferred value types.
92
+ *
93
+ * @public
94
+ */
95
+ export declare type BuildSchema<Fields> = {
96
+ [F in Fields as F extends {
97
+ name: infer N extends string;
98
+ } ? N : never]: F extends AnyField ? InferFieldValue<F> : never;
99
+ };
100
+
101
+ /**
102
+ * A conditional wrapper that shows/hides elements based on another field's value.
103
+ *
104
+ * @typeParam FieldName - The field to check
105
+ * @typeParam Value - The value that triggers the condition
106
+ * @typeParam Elements - Tuple of contained form elements
107
+ *
108
+ * @public
109
+ */
110
+ export declare interface Conditional<FieldName extends string, Value, Elements extends readonly FormElement[]> {
111
+ /** Type discriminator - identifies this as a conditional element */
112
+ readonly _type: "conditional";
113
+ /** Name of the field whose value determines visibility */
114
+ readonly field: FieldName;
115
+ /** Value that triggers the condition (shows nested elements) */
116
+ readonly value: Value;
117
+ /** Form elements shown when condition is met */
118
+ readonly elements: Elements;
119
+ }
120
+
121
+ /**
122
+ * Registry for dynamic data sources.
123
+ *
124
+ * Extend this interface via module augmentation to register your data sources:
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * declare module "@formspec/core" {
129
+ * interface DataSourceRegistry {
130
+ * countries: { id: string; code: string; name: string };
131
+ * templates: { id: string; name: string; category: string };
132
+ * }
133
+ * }
134
+ * ```
135
+ *
136
+ * @public
137
+ */
138
+ export declare interface DataSourceRegistry {
139
+ }
140
+
141
+ /**
142
+ * Gets the value type for a registered data source.
143
+ *
144
+ * If the source has an `id` property, that becomes the value type.
145
+ * Otherwise, defaults to `string`.
146
+ *
147
+ * @public
148
+ */
149
+ export declare type DataSourceValueType<Source extends string> = Source extends keyof DataSourceRegistry ? DataSourceRegistry[Source] extends {
150
+ id: infer ID;
151
+ } ? ID : string : string;
152
+
153
+ /**
154
+ * A field with dynamic enum options (fetched from a data source at runtime).
155
+ *
156
+ * @typeParam N - The field name (string literal type)
157
+ * @typeParam Source - The data source key (from DataSourceRegistry)
158
+ *
159
+ * @public
160
+ */
161
+ export declare interface DynamicEnumField<N extends string, Source extends string> {
162
+ /** Type discriminator for form elements */
163
+ readonly _type: "field";
164
+ /** Field type discriminator - identifies this as a dynamic enum field */
165
+ readonly _field: "dynamic_enum";
166
+ /** Unique field identifier used as the schema key */
167
+ readonly name: N;
168
+ /** Data source key for fetching options at runtime */
169
+ readonly source: Source;
170
+ /** Display label for the field */
171
+ readonly label?: string;
172
+ /** Whether this field is required for form submission */
173
+ readonly required?: boolean;
174
+ /** Field names whose values are needed to fetch options */
175
+ readonly params?: readonly string[];
176
+ }
177
+
178
+ /**
179
+ * A field that loads its schema dynamically (e.g., from an extension).
180
+ *
181
+ * @typeParam N - The field name (string literal type)
182
+ *
183
+ * @public
184
+ */
185
+ export declare interface DynamicSchemaField<N extends string> {
186
+ /** Type discriminator for form elements */
187
+ readonly _type: "field";
188
+ /** Field type discriminator - identifies this as a dynamic schema field */
189
+ readonly _field: "dynamic_schema";
190
+ /** Unique field identifier used as the schema key */
191
+ readonly name: N;
192
+ /** Identifier for the schema source */
193
+ readonly schemaSource: string;
194
+ /** Display label for the field */
195
+ readonly label?: string;
196
+ /** Whether this field is required for form submission */
197
+ readonly required?: boolean;
198
+ /** Field names whose values are needed to configure the schema */
199
+ readonly params?: readonly string[];
200
+ }
201
+
202
+ /**
203
+ * An enum option with a separate ID and display label.
204
+ *
205
+ * Use this when the stored value (id) should differ from the display text (label).
206
+ *
207
+ * @public
208
+ */
209
+ export declare interface EnumOption {
210
+ readonly id: string;
211
+ readonly label: string;
212
+ }
213
+
214
+ /**
215
+ * Valid enum option types: either plain strings or objects with id/label.
216
+ *
217
+ * @public
218
+ */
219
+ export declare type EnumOptionValue = string | EnumOption;
220
+
221
+ /**
222
+ * Predicate types for conditional logic.
223
+ *
224
+ * Predicates are used with `when()` to define conditions in a readable way.
225
+ */
226
+ /**
227
+ * An equality predicate that checks if a field equals a specific value.
228
+ *
229
+ * @typeParam K - The field name to check
230
+ * @typeParam V - The value to compare against
231
+ *
232
+ * @public
233
+ */
234
+ export declare interface EqualsPredicate<K extends string, V> {
235
+ /** Predicate type discriminator */
236
+ readonly _predicate: "equals";
237
+ /** Name of the field to check */
238
+ readonly field: K;
239
+ /** Value that the field must equal */
240
+ readonly value: V;
241
+ }
242
+
243
+ /**
244
+ * Extracts fields that ARE inside conditionals.
245
+ * These fields may or may not be visible and should be optional in the inferred schema.
246
+ *
247
+ * @example
248
+ * ```typescript
249
+ * // Given a form with conditional fields:
250
+ * const form = formspec(
251
+ * field.text("name"), // non-conditional (skipped)
252
+ * when(is("type", "business"),
253
+ * field.text("company"), // conditional (extracted)
254
+ * field.text("taxId"), // conditional (extracted)
255
+ * ),
256
+ * );
257
+ *
258
+ * // ExtractConditionalFields extracts: TextField<"company"> | TextField<"taxId">
259
+ * ```
260
+ *
261
+ * @public
262
+ */
263
+ export declare type ExtractConditionalFields<E> = E extends AnyField ? never : E extends Group<infer Elements> ? ExtractConditionalFieldsFromArray<Elements> : E extends Conditional<string, unknown, infer Elements> ? ExtractFieldsFromArray<Elements> : never;
264
+
265
+ /**
266
+ * Extracts conditional fields from an array of elements.
267
+ *
268
+ * @example
269
+ * ```typescript
270
+ * type Elements = readonly [
271
+ * TextField<"name">,
272
+ * Conditional<"type", "business", readonly [TextField<"company">]>
273
+ * ];
274
+ * type Fields = ExtractConditionalFieldsFromArray<Elements>;
275
+ * // TextField<"company">
276
+ * ```
277
+ *
278
+ * @public
279
+ */
280
+ export declare type ExtractConditionalFieldsFromArray<Elements> = Elements extends readonly [
281
+ infer First,
282
+ ...infer Rest
283
+ ] ? ExtractConditionalFields<First> | ExtractConditionalFieldsFromArray<Rest> : never;
284
+
285
+ /**
286
+ * Extracts all fields from a single element (recursively).
287
+ *
288
+ * - Field elements return themselves
289
+ * - Groups extract fields from all child elements
290
+ * - Conditionals extract fields from all child elements
291
+ *
292
+ * @public
293
+ */
294
+ export declare type ExtractFields<E> = E extends AnyField ? E : E extends Group<infer Elements> ? ExtractFieldsFromArray<Elements> : E extends Conditional<string, unknown, infer Elements> ? ExtractFieldsFromArray<Elements> : never;
295
+
296
+ /**
297
+ * Extracts fields from an array of elements.
298
+ *
299
+ * Recursively processes each element and unions the results.
300
+ *
301
+ * @public
302
+ */
303
+ export declare type ExtractFieldsFromArray<Elements> = Elements extends readonly [
304
+ infer First,
305
+ ...infer Rest
306
+ ] ? ExtractFields<First> | ExtractFieldsFromArray<Rest> : never;
307
+
308
+ /**
309
+ * Extracts fields that are NOT inside conditionals.
310
+ * These fields are always visible and should be required in the inferred schema.
311
+ *
312
+ * @example
313
+ * ```typescript
314
+ * // Given a form with conditional and non-conditional fields:
315
+ * const form = formspec(
316
+ * field.text("name"), // non-conditional
317
+ * field.number("age"), // non-conditional
318
+ * when(is("type", "business"),
319
+ * field.text("company"), // conditional (skipped)
320
+ * ),
321
+ * );
322
+ *
323
+ * // ExtractNonConditionalFields extracts: TextField<"name"> | NumberField<"age">
324
+ * ```
325
+ *
326
+ * @public
327
+ */
328
+ export declare type ExtractNonConditionalFields<E> = E extends AnyField ? E : E extends Group<infer Elements> ? ExtractNonConditionalFieldsFromArray<Elements> : E extends Conditional<string, unknown, infer _Elements> ? never : never;
329
+
330
+ /**
331
+ * Extracts non-conditional fields from an array of elements.
332
+ *
333
+ * @example
334
+ * ```typescript
335
+ * type Elements = readonly [TextField<"name">, NumberField<"age">];
336
+ * type Fields = ExtractNonConditionalFieldsFromArray<Elements>;
337
+ * // TextField<"name"> | NumberField<"age">
338
+ * ```
339
+ *
340
+ * @public
341
+ */
342
+ export declare type ExtractNonConditionalFieldsFromArray<Elements> = Elements extends readonly [
343
+ infer First,
344
+ ...infer Rest
345
+ ] ? ExtractNonConditionalFields<First> | ExtractNonConditionalFieldsFromArray<Rest> : never;
346
+
347
+ /**
348
+ * Field builder namespace containing functions to create each field type.
349
+ *
350
+ * @example
351
+ * ```typescript
352
+ * import { field } from "@formspec/dsl";
353
+ *
354
+ * field.text("name", { label: "Full Name" });
355
+ * field.number("age", { min: 0, max: 150 });
356
+ * field.enum("status", ["draft", "sent", "paid"]);
357
+ * field.dynamicEnum("country", "countries", { label: "Country" });
358
+ * ```
359
+ *
360
+ * @public
361
+ */
362
+ export declare const field: {
363
+ /**
364
+ * Creates a text input field.
365
+ *
366
+ * @param name - The field name (used as the schema key)
367
+ * @param config - Optional configuration for label, placeholder, etc.
368
+ * @returns A TextField descriptor
369
+ */
370
+ text: <const N extends string>(name: N, config?: Omit<TextField<N>, "_type" | "_field" | "name">) => TextField<N>;
371
+ /**
372
+ * Creates a numeric input field.
373
+ *
374
+ * @param name - The field name (used as the schema key)
375
+ * @param config - Optional configuration for label, min, max, etc.
376
+ * @returns A NumberField descriptor
377
+ */
378
+ number: <const N extends string>(name: N, config?: Omit<NumberField<N>, "_type" | "_field" | "name">) => NumberField<N>;
379
+ /**
380
+ * Creates a boolean checkbox field.
381
+ *
382
+ * @param name - The field name (used as the schema key)
383
+ * @param config - Optional configuration for label, etc.
384
+ * @returns A BooleanField descriptor
385
+ */
386
+ boolean: <const N extends string>(name: N, config?: Omit<BooleanField<N>, "_type" | "_field" | "name">) => BooleanField<N>;
387
+ /**
388
+ * Creates a field with static enum options (known at compile time).
389
+ *
390
+ * Literal types are automatically inferred - no `as const` needed:
391
+ * ```typescript
392
+ * field.enum("status", ["draft", "sent", "paid"])
393
+ * // Schema type: "draft" | "sent" | "paid"
394
+ * ```
395
+ *
396
+ * Options can be strings or objects with `id` and `label`:
397
+ * ```typescript
398
+ * field.enum("priority", [
399
+ * { id: "low", label: "Low Priority" },
400
+ * { id: "high", label: "High Priority" },
401
+ * ])
402
+ * ```
403
+ *
404
+ * **Note:** All options must be of the same type (all strings OR all objects).
405
+ * Mixing strings and objects will throw a runtime error.
406
+ *
407
+ * @param name - The field name (used as the schema key)
408
+ * @param options - Array of allowed string values or objects with `id` and `label` properties
409
+ * @param config - Optional configuration for label, etc.
410
+ * @returns A StaticEnumField descriptor
411
+ * @throws Error if options array contains mixed types (strings and objects)
412
+ */
413
+ enum: <const N extends string, const O extends readonly EnumOptionValue[]>(name: N, options: O, config?: Omit<StaticEnumField<N, O>, "_type" | "_field" | "name" | "options">) => StaticEnumField<N, O>;
414
+ /**
415
+ * Creates a field with dynamic enum options (fetched from a data source at runtime).
416
+ *
417
+ * The data source must be registered in DataSourceRegistry via module augmentation:
418
+ * ```typescript
419
+ * declare module "@formspec/core" {
420
+ * interface DataSourceRegistry {
421
+ * countries: { id: string; code: string; name: string };
422
+ * }
423
+ * }
424
+ *
425
+ * field.dynamicEnum("country", "countries", { label: "Country" })
426
+ * ```
427
+ *
428
+ * @param name - The field name (used as the schema key)
429
+ * @param source - The data source key (must be in DataSourceRegistry)
430
+ * @param config - Optional configuration for label, params, etc.
431
+ * @returns A DynamicEnumField descriptor
432
+ */
433
+ dynamicEnum: <const N extends string, const Source extends string>(name: N, source: Source, config?: Omit<DynamicEnumField<N, Source>, "_type" | "_field" | "name" | "source">) => DynamicEnumField<N, Source>;
434
+ /**
435
+ * Creates a field that loads its schema dynamically (e.g., from an extension).
436
+ *
437
+ * @param name - The field name (used as the schema key)
438
+ * @param schemaSource - Identifier for the schema source
439
+ * @param config - Optional configuration for label, etc.
440
+ * @returns A DynamicSchemaField descriptor
441
+ */
442
+ dynamicSchema: <const N extends string>(name: N, schemaSource: string, config?: Omit<DynamicSchemaField<N>, "_type" | "_field" | "name" | "schemaSource">) => DynamicSchemaField<N>;
443
+ /**
444
+ * Creates an array field containing repeating items.
445
+ *
446
+ * Use this for lists of values (e.g., multiple addresses, line items).
447
+ *
448
+ * @example
449
+ * ```typescript
450
+ * field.array("addresses",
451
+ * field.text("street", { label: "Street" }),
452
+ * field.text("city", { label: "City" }),
453
+ * field.text("zip", { label: "ZIP Code" }),
454
+ * )
455
+ * ```
456
+ *
457
+ * @param name - The field name (used as the schema key)
458
+ * @param items - The form elements that define each array item
459
+ * @returns An ArrayField descriptor
460
+ */
461
+ array: <const N extends string, const Items extends readonly FormElement[]>(name: N, ...items: Items) => ArrayField<N, Items>;
462
+ /**
463
+ * Creates an array field with additional configuration options.
464
+ *
465
+ * @example
466
+ * ```typescript
467
+ * field.arrayWithConfig("lineItems", {
468
+ * label: "Line Items",
469
+ * minItems: 1,
470
+ * maxItems: 10,
471
+ * },
472
+ * field.text("description"),
473
+ * field.number("quantity"),
474
+ * )
475
+ * ```
476
+ *
477
+ * @param name - The field name (used as the schema key)
478
+ * @param config - Configuration for label, minItems, maxItems, etc.
479
+ * @param items - The form elements that define each array item
480
+ * @returns An ArrayField descriptor
481
+ */
482
+ arrayWithConfig: <const N extends string, const Items extends readonly FormElement[]>(name: N, config: Omit<ArrayField<N, Items>, "_type" | "_field" | "name" | "items">, ...items: Items) => ArrayField<N, Items>;
483
+ /**
484
+ * Creates an object field containing nested properties.
485
+ *
486
+ * Use this for grouping related fields under a single key in the schema.
487
+ *
488
+ * @example
489
+ * ```typescript
490
+ * field.object("address",
491
+ * field.text("street", { label: "Street" }),
492
+ * field.text("city", { label: "City" }),
493
+ * field.text("zip", { label: "ZIP Code" }),
494
+ * )
495
+ * ```
496
+ *
497
+ * @param name - The field name (used as the schema key)
498
+ * @param properties - The form elements that define the object's properties
499
+ * @returns An ObjectField descriptor
500
+ */
501
+ object: <const N extends string, const Properties extends readonly FormElement[]>(name: N, ...properties: Properties) => ObjectField<N, Properties>;
502
+ /**
503
+ * Creates an object field with additional configuration options.
504
+ *
505
+ * @example
506
+ * ```typescript
507
+ * field.objectWithConfig("billingAddress", { label: "Billing Address", required: true },
508
+ * field.text("street"),
509
+ * field.text("city"),
510
+ * )
511
+ * ```
512
+ *
513
+ * @param name - The field name (used as the schema key)
514
+ * @param config - Configuration for label, required, etc.
515
+ * @param properties - The form elements that define the object's properties
516
+ * @returns An ObjectField descriptor
517
+ */
518
+ objectWithConfig: <const N extends string, const Properties extends readonly FormElement[]>(name: N, config: Omit<ObjectField<N, Properties>, "_type" | "_field" | "name" | "properties">, ...properties: Properties) => ObjectField<N, Properties>;
519
+ };
520
+
521
+ /**
522
+ * Utility type that flattens intersection types into a single object type.
523
+ *
524
+ * This improves TypeScript's display of inferred types and ensures
525
+ * structural equality checks work correctly.
526
+ *
527
+ * @example
528
+ * ```typescript
529
+ * // Without FlattenIntersection:
530
+ * type Messy = { a: string } & { b: number };
531
+ * // Displays as: { a: string } & { b: number }
532
+ *
533
+ * // With FlattenIntersection:
534
+ * type Clean = FlattenIntersection<{ a: string } & { b: number }>;
535
+ * // Displays as: { a: string; b: number }
536
+ * ```
537
+ *
538
+ * @public
539
+ */
540
+ export declare type FlattenIntersection<T> = {
541
+ [K in keyof T]: T[K];
542
+ } & {};
543
+
544
+ /**
545
+ * Union of all form element types (fields and structural elements).
546
+ *
547
+ * @public
548
+ */
549
+ export declare type FormElement = AnyField | Group<readonly FormElement[]> | Conditional<string, unknown, readonly FormElement[]>;
550
+
551
+ /**
552
+ * A complete form specification.
553
+ *
554
+ * @typeParam Elements - Tuple of top-level form elements
555
+ *
556
+ * @public
557
+ */
558
+ export declare interface FormSpec<Elements extends readonly FormElement[]> {
559
+ /** Top-level form elements */
560
+ readonly elements: Elements;
561
+ }
562
+
563
+ /**
564
+ * Creates a complete form specification.
565
+ *
566
+ * The structure IS the definition:
567
+ * - Nesting with `group()` defines visual layout
568
+ * - Nesting with `when()` defines conditional visibility
569
+ * - Field type implies control type (text field → text input)
570
+ * - Array position implies field ordering
571
+ *
572
+ * Schema is automatically inferred from all fields in the structure.
573
+ *
574
+ * @example
575
+ * ```typescript
576
+ * const InvoiceForm = formspec(
577
+ * group("Customer",
578
+ * field.text("customerName", { label: "Customer Name" }),
579
+ * field.dynamicEnum("country", "fetch_countries", { label: "Country" }),
580
+ * ),
581
+ * group("Invoice Details",
582
+ * field.number("amount", { label: "Amount", min: 0 }),
583
+ * field.enum("status", ["draft", "sent", "paid"] as const),
584
+ * when(is("status", "draft"),
585
+ * field.text("internalNotes", { label: "Internal Notes" }),
586
+ * ),
587
+ * ),
588
+ * );
589
+ * ```
590
+ *
591
+ * @param elements - The top-level form elements
592
+ * @returns A FormSpec descriptor
593
+ *
594
+ * @public
595
+ */
596
+ export declare function formspec<const Elements extends readonly FormElement[]>(...elements: Elements): FormSpec<Elements>;
597
+
598
+ /**
599
+ * Options for creating a form specification.
600
+ *
601
+ * @public
602
+ */
603
+ export declare interface FormSpecOptions {
604
+ /**
605
+ * Whether to validate the form structure.
606
+ * - `true` or `"warn"`: Validate and log warnings/errors to console
607
+ * - `"throw"`: Validate and throw an error if validation fails
608
+ * - `false`: Skip validation (default in production for performance)
609
+ *
610
+ * @defaultValue false
611
+ */
612
+ validate?: boolean | "warn" | "throw";
613
+ /**
614
+ * Optional name for the form (used in validation messages).
615
+ */
616
+ name?: string;
617
+ }
618
+
619
+ /**
620
+ * Creates a complete form specification with validation options.
621
+ *
622
+ * @example
623
+ * ```typescript
624
+ * const form = formspecWithValidation(
625
+ * { validate: true, name: "MyForm" },
626
+ * field.text("name"),
627
+ * field.enum("status", ["draft", "sent"] as const),
628
+ * when(is("status", "draft"),
629
+ * field.text("notes"),
630
+ * ),
631
+ * );
632
+ * ```
633
+ *
634
+ * @param options - Validation options
635
+ * @param elements - The top-level form elements
636
+ * @returns A FormSpec descriptor
637
+ *
638
+ * @public
639
+ */
640
+ export declare function formspecWithValidation<const Elements extends readonly FormElement[]>(options: FormSpecOptions, ...elements: Elements): FormSpec<Elements>;
641
+
642
+ /**
643
+ * A visual grouping of form elements.
644
+ *
645
+ * Groups provide visual organization and can be rendered as fieldsets or sections.
646
+ *
647
+ * @typeParam Elements - Tuple of contained form elements
648
+ *
649
+ * @public
650
+ */
651
+ export declare interface Group<Elements extends readonly FormElement[]> {
652
+ /** Type discriminator - identifies this as a group element */
653
+ readonly _type: "group";
654
+ /** Display label for the group */
655
+ readonly label: string;
656
+ /** Form elements contained within this group */
657
+ readonly elements: Elements;
658
+ }
659
+
660
+ /**
661
+ * Creates a visual group of form elements.
662
+ *
663
+ * Groups provide visual organization and can be rendered as fieldsets or sections.
664
+ * The nesting of groups defines the visual hierarchy of the form.
665
+ *
666
+ * @example
667
+ * ```typescript
668
+ * group("Customer Information",
669
+ * field.text("name", { label: "Name" }),
670
+ * field.text("email", { label: "Email" }),
671
+ * )
672
+ * ```
673
+ *
674
+ * @param label - The group's display label
675
+ * @param elements - The form elements contained in this group
676
+ * @returns A Group descriptor
677
+ *
678
+ * @public
679
+ */
680
+ export declare function group<const Elements extends readonly FormElement[]>(label: string, ...elements: Elements): Group<Elements>;
681
+
682
+ /**
683
+ * Infers the value type from a single field.
684
+ *
685
+ * - TextField returns string
686
+ * - NumberField returns number
687
+ * - BooleanField returns boolean
688
+ * - StaticEnumField returns union of option literals
689
+ * - DynamicEnumField returns DataSourceValueType (usually string)
690
+ * - DynamicSchemaField returns Record of string to unknown
691
+ * - ArrayField returns array of inferred item schema
692
+ * - ObjectField returns object of inferred property schema
693
+ *
694
+ * @example
695
+ * ```typescript
696
+ * // Simple fields
697
+ * type T1 = InferFieldValue<TextField<"name">>; // string
698
+ * type T2 = InferFieldValue<NumberField<"age">>; // number
699
+ *
700
+ * // Enum fields
701
+ * type T3 = InferFieldValue<StaticEnumField<"status", ["draft", "sent"]>>; // "draft" | "sent"
702
+ *
703
+ * // Nested fields
704
+ * type T4 = InferFieldValue<ArrayField<"items", [TextField<"name">]>>; // { name: string }[]
705
+ * type T5 = InferFieldValue<ObjectField<"address", [TextField<"city">]>>; // { city: string }
706
+ * ```
707
+ *
708
+ * @public
709
+ */
710
+ export declare type InferFieldValue<F> = F extends TextField<string> ? string : F extends NumberField<string> ? number : F extends BooleanField<string> ? boolean : F extends StaticEnumField<string, infer O extends readonly EnumOptionValue[]> ? O extends readonly EnumOption[] ? O[number]["id"] : O extends readonly string[] ? O[number] : never : F extends DynamicEnumField<string, infer Source> ? DataSourceValueType<Source> : F extends DynamicSchemaField<string> ? Record<string, unknown> : F extends ArrayField<string, infer Items extends readonly FormElement[]> ? InferSchema<Items>[] : F extends ObjectField<string, infer Properties extends readonly FormElement[]> ? InferSchema<Properties> : never;
711
+
712
+ /**
713
+ * Infers the schema type from a FormSpec.
714
+ *
715
+ * Convenience type that extracts elements and infers the schema.
716
+ *
717
+ * @example
718
+ * ```typescript
719
+ * const form = formspec(...);
720
+ * type Schema = InferFormSchema<typeof form>;
721
+ * ```
722
+ *
723
+ * @public
724
+ */
725
+ export declare type InferFormSchema<F extends FormSpec<readonly FormElement[]>> = F extends FormSpec<infer Elements> ? InferSchema<Elements> : never;
726
+
727
+ /**
728
+ * Infers the complete schema type from a form's elements.
729
+ *
730
+ * This is the main inference type that converts a form structure
731
+ * into its corresponding TypeScript schema type.
732
+ *
733
+ * Non-conditional fields are required, conditional fields are optional.
734
+ *
735
+ * @example
736
+ * ```typescript
737
+ * const form = formspec(
738
+ * field.text("name"),
739
+ * field.number("age"),
740
+ * field.enum("status", ["active", "inactive"] as const),
741
+ * );
742
+ *
743
+ * type Schema = InferSchema<typeof form.elements>;
744
+ * // { name: string; age: number; status: "active" | "inactive" }
745
+ *
746
+ * // Conditional fields become optional:
747
+ * const formWithConditional = formspec(
748
+ * field.enum("type", ["a", "b"] as const),
749
+ * when(is("type", "a"), field.text("aField")),
750
+ * );
751
+ * type ConditionalSchema = InferSchema<typeof formWithConditional.elements>;
752
+ * // { type: "a" | "b"; aField?: string }
753
+ * ```
754
+ *
755
+ * @public
756
+ */
757
+ export declare type InferSchema<Elements extends readonly FormElement[]> = FlattenIntersection<BuildSchema<ExtractNonConditionalFieldsFromArray<Elements>> & Partial<BuildSchema<ExtractConditionalFieldsFromArray<Elements>>>>;
758
+
759
+ /**
760
+ * Creates an equality predicate that checks if a field equals a specific value.
761
+ *
762
+ * Use this with `when()` to create readable conditional expressions:
763
+ *
764
+ * @example
765
+ * ```typescript
766
+ * // Show cardNumber field when paymentMethod is "card"
767
+ * when(is("paymentMethod", "card"),
768
+ * field.text("cardNumber", { label: "Card Number" }),
769
+ * )
770
+ * ```
771
+ *
772
+ * @typeParam K - The field name (inferred as string literal)
773
+ * @typeParam V - The value type (inferred as literal)
774
+ * @param field - The name of the field to check
775
+ * @param value - The value the field must equal
776
+ * @returns An EqualsPredicate for use with `when()`
777
+ * @public
778
+ */
779
+ export declare function is<const K extends string, const V>(field: K, value: V): EqualsPredicate<K, V>;
780
+
781
+ /**
782
+ * Logs validation issues to the console.
783
+ *
784
+ * @param result - The validation result to log
785
+ * @param formName - Optional name for the form (for better error messages)
786
+ *
787
+ * @public
788
+ */
789
+ export declare function logValidationIssues(result: ValidationResult, formName?: string): void;
790
+
791
+ /**
792
+ * A numeric input field.
793
+ *
794
+ * @typeParam N - The field name (string literal type)
795
+ *
796
+ * @public
797
+ */
798
+ export declare interface NumberField<N extends string> {
799
+ /** Type discriminator for form elements */
800
+ readonly _type: "field";
801
+ /** Field type discriminator - identifies this as a number field */
802
+ readonly _field: "number";
803
+ /** Unique field identifier used as the schema key */
804
+ readonly name: N;
805
+ /** Display label for the field */
806
+ readonly label?: string;
807
+ /** Minimum allowed value */
808
+ readonly min?: number;
809
+ /** Maximum allowed value */
810
+ readonly max?: number;
811
+ /** Whether this field is required for form submission */
812
+ readonly required?: boolean;
813
+ /** Value must be a multiple of this number (use 1 for integer semantics) */
814
+ readonly multipleOf?: number;
815
+ }
816
+
817
+ /**
818
+ * An object field containing nested properties.
819
+ *
820
+ * Use this for grouping related fields under a single key in the schema.
821
+ *
822
+ * @typeParam N - The field name (string literal type)
823
+ * @typeParam Properties - The form elements that define the object's properties
824
+ *
825
+ * @public
826
+ */
827
+ export declare interface ObjectField<N extends string, Properties extends readonly FormElement[]> {
828
+ /** Type discriminator for form elements */
829
+ readonly _type: "field";
830
+ /** Field type discriminator - identifies this as an object field */
831
+ readonly _field: "object";
832
+ /** Unique field identifier used as the schema key */
833
+ readonly name: N;
834
+ /** Form elements that define the properties of this object */
835
+ readonly properties: Properties;
836
+ /** Display label for the field */
837
+ readonly label?: string;
838
+ /** Whether this field is required for form submission */
839
+ readonly required?: boolean;
840
+ }
841
+
842
+ /**
843
+ * Union of all predicate types.
844
+ *
845
+ * Currently only supports equality, but can be extended with:
846
+ * - `OneOfPredicate` - field value is one of several options
847
+ * - `NotPredicate` - negation of another predicate
848
+ * - `AndPredicate` / `OrPredicate` - logical combinations
849
+ *
850
+ * @public
851
+ */
852
+ export declare type Predicate<K extends string = string, V = unknown> = EqualsPredicate<K, V>;
853
+
854
+ /**
855
+ * A field with static enum options (known at compile time).
856
+ *
857
+ * Options can be plain strings or objects with `id` and `label` properties.
858
+ *
859
+ * @typeParam N - The field name (string literal type)
860
+ * @typeParam O - Tuple of option values (strings or EnumOption objects)
861
+ *
862
+ * @public
863
+ */
864
+ export declare interface StaticEnumField<N extends string, O extends readonly EnumOptionValue[]> {
865
+ /** Type discriminator for form elements */
866
+ readonly _type: "field";
867
+ /** Field type discriminator - identifies this as an enum field */
868
+ readonly _field: "enum";
869
+ /** Unique field identifier used as the schema key */
870
+ readonly name: N;
871
+ /** Array of allowed option values */
872
+ readonly options: O;
873
+ /** Display label for the field */
874
+ readonly label?: string;
875
+ /** Whether this field is required for form submission */
876
+ readonly required?: boolean;
877
+ }
878
+
879
+ /**
880
+ * Form element type definitions.
881
+ *
882
+ * These types define the structure of form specifications.
883
+ * The structure IS the definition - nesting implies layout and conditional logic.
884
+ */
885
+ /**
886
+ * A text input field.
887
+ *
888
+ * @typeParam N - The field name (string literal type)
889
+ *
890
+ * @public
891
+ */
892
+ export declare interface TextField<N extends string> {
893
+ /** Type discriminator for form elements */
894
+ readonly _type: "field";
895
+ /** Field type discriminator - identifies this as a text field */
896
+ readonly _field: "text";
897
+ /** Unique field identifier used as the schema key */
898
+ readonly name: N;
899
+ /** Display label for the field */
900
+ readonly label?: string;
901
+ /** Placeholder text shown when field is empty */
902
+ readonly placeholder?: string;
903
+ /** Whether this field is required for form submission */
904
+ readonly required?: boolean;
905
+ /** Minimum string length */
906
+ readonly minLength?: number;
907
+ /** Maximum string length */
908
+ readonly maxLength?: number;
909
+ /** Regular expression pattern the value must match */
910
+ readonly pattern?: string;
911
+ }
912
+
913
+ /**
914
+ * Validates a form specification for common issues.
915
+ *
916
+ * Checks for:
917
+ * - Duplicate field names at the root level (warning)
918
+ * - References to non-existent fields in conditionals (error)
919
+ *
920
+ * @example
921
+ * ```typescript
922
+ * const form = formspec(
923
+ * field.text("name"),
924
+ * field.text("name"), // Duplicate!
925
+ * when("nonExistent", "value", // Reference to non-existent field!
926
+ * field.text("extra"),
927
+ * ),
928
+ * );
929
+ *
930
+ * const result = validateForm(form.elements);
931
+ * // result.valid === false
932
+ * // result.issues contains duplicate and reference errors
933
+ * ```
934
+ *
935
+ * @param elements - The form elements to validate
936
+ * @returns Validation result with any issues found
937
+ *
938
+ * @public
939
+ */
940
+ export declare function validateForm(elements: readonly FormElement[]): ValidationResult;
941
+
942
+ /**
943
+ * A validation issue found in a form specification.
944
+ *
945
+ * @public
946
+ */
947
+ export declare interface ValidationIssue {
948
+ /** Severity of the issue */
949
+ severity: ValidationSeverity;
950
+ /** Human-readable message describing the issue */
951
+ message: string;
952
+ /** Path to the element with the issue (e.g., "group.fieldName") */
953
+ path: string;
954
+ }
955
+
956
+ /**
957
+ * Result of validating a form specification.
958
+ *
959
+ * @public
960
+ */
961
+ export declare interface ValidationResult {
962
+ /** Whether the form is valid (no errors, warnings are ok) */
963
+ valid: boolean;
964
+ /** List of validation issues found */
965
+ issues: ValidationIssue[];
966
+ }
967
+
968
+ /**
969
+ * Validation issue severity levels.
970
+ *
971
+ * @public
972
+ */
973
+ export declare type ValidationSeverity = "error" | "warning";
974
+
975
+ /**
976
+ * Creates a conditional wrapper that shows elements based on a predicate.
977
+ *
978
+ * When the predicate evaluates to true, the contained elements are shown.
979
+ * Otherwise, they are hidden (but still part of the schema).
980
+ *
981
+ * @example
982
+ * ```typescript
983
+ * import { is } from "@formspec/dsl";
984
+ *
985
+ * field.enum("status", ["draft", "sent", "paid"] as const),
986
+ * when(is("status", "draft"),
987
+ * field.text("internalNotes", { label: "Internal Notes" }),
988
+ * )
989
+ * ```
990
+ *
991
+ * @param predicate - The condition to evaluate (use `is()` to create)
992
+ * @param elements - The form elements to show when condition is met
993
+ * @returns A Conditional descriptor
994
+ *
995
+ * @public
996
+ */
997
+ export declare function when<const K extends string, const V, const Elements extends readonly FormElement[]>(predicate: Predicate<K, V>, ...elements: Elements): Conditional<K, V, Elements>;
998
+
999
+ export { }