@formspec/constraints 0.1.0-alpha.21 → 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.
@@ -35,8 +35,81 @@
35
35
  * @packageDocumentation
36
36
  */
37
37
 
38
- import type { FormElement } from '@formspec/core';
39
- import type { FormSpec } from '@formspec/core';
38
+ /**
39
+ * Union of all field types.
40
+ *
41
+ * @public
42
+ */
43
+ 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[]>;
44
+
45
+ /**
46
+ * An array field containing repeating items.
47
+ *
48
+ * Use this for lists of values (e.g., multiple addresses, line items).
49
+ *
50
+ * @typeParam N - The field name (string literal type)
51
+ * @typeParam Items - The form elements that define each array item
52
+ *
53
+ * @public
54
+ */
55
+ export declare interface ArrayField<N extends string, Items extends readonly FormElement[]> {
56
+ /** Type discriminator for form elements */
57
+ readonly _type: "field";
58
+ /** Field type discriminator - identifies this as an array field */
59
+ readonly _field: "array";
60
+ /** Unique field identifier used as the schema key */
61
+ readonly name: N;
62
+ /** Form elements that define the schema for each array item */
63
+ readonly items: Items;
64
+ /** Display label for the field */
65
+ readonly label?: string;
66
+ /** Whether this field is required for form submission */
67
+ readonly required?: boolean;
68
+ /** Minimum number of items required */
69
+ readonly minItems?: number;
70
+ /** Maximum number of items allowed */
71
+ readonly maxItems?: number;
72
+ }
73
+
74
+ /**
75
+ * A boolean checkbox field.
76
+ *
77
+ * @typeParam N - The field name (string literal type)
78
+ *
79
+ * @public
80
+ */
81
+ export declare interface BooleanField<N extends string> {
82
+ /** Type discriminator for form elements */
83
+ readonly _type: "field";
84
+ /** Field type discriminator - identifies this as a boolean field */
85
+ readonly _field: "boolean";
86
+ /** Unique field identifier used as the schema key */
87
+ readonly name: N;
88
+ /** Display label for the field */
89
+ readonly label?: string;
90
+ /** Whether this field is required for form submission */
91
+ readonly required?: boolean;
92
+ }
93
+
94
+ /**
95
+ * A conditional wrapper that shows/hides elements based on another field's value.
96
+ *
97
+ * @typeParam FieldName - The field to check
98
+ * @typeParam Value - The value that triggers the condition
99
+ * @typeParam Elements - Tuple of contained form elements
100
+ *
101
+ * @public
102
+ */
103
+ export declare interface Conditional<FieldName extends string, Value, Elements extends readonly FormElement[]> {
104
+ /** Type discriminator - identifies this as a conditional element */
105
+ readonly _type: "conditional";
106
+ /** Name of the field whose value determines visibility */
107
+ readonly field: FieldName;
108
+ /** Value that triggers the condition (shows nested elements) */
109
+ readonly value: Value;
110
+ /** Form elements shown when condition is met */
111
+ readonly elements: Elements;
112
+ }
40
113
 
41
114
  /**
42
115
  * Complete constraint configuration for a FormSpec project.
@@ -77,20 +150,9 @@ export declare interface ControlOptionConstraints {
77
150
  custom?: Record<string, Severity>;
78
151
  }
79
152
 
80
- /**
81
- * Default FormSpec configuration.
82
- *
83
- * @public
84
- */
85
- export declare const DEFAULT_CONFIG: FormSpecConfig;
153
+ /* Excluded from this release type: DEFAULT_CONFIG */
86
154
 
87
- /**
88
- * Default constraint configuration that allows all features.
89
- * All constraints default to "off" (allowed).
90
- *
91
- * @public
92
- */
93
- export declare const DEFAULT_CONSTRAINTS: ResolvedConstraintConfig;
155
+ /* Excluded from this release type: DEFAULT_CONSTRAINTS */
94
156
 
95
157
  /**
96
158
  * Creates a constraint configuration directly from an object.
@@ -117,22 +179,76 @@ export declare const DEFAULT_CONSTRAINTS: ResolvedConstraintConfig;
117
179
  export declare function defineConstraints(config: ConstraintConfig): ResolvedConstraintConfig;
118
180
 
119
181
  /**
120
- * Extracts which options are present on a field object.
121
- * Works with FormSpec field types.
182
+ * A field with dynamic enum options (fetched from a data source at runtime).
122
183
  *
123
- * @param field - A field object with potential options
124
- * @returns Array of present option names
184
+ * @typeParam N - The field name (string literal type)
185
+ * @typeParam Source - The data source key (from DataSourceRegistry)
125
186
  *
126
187
  * @public
127
188
  */
128
- export declare function extractFieldOptions(field: Record<string, unknown>): FieldOption[];
189
+ export declare interface DynamicEnumField<N extends string, Source extends string> {
190
+ /** Type discriminator for form elements */
191
+ readonly _type: "field";
192
+ /** Field type discriminator - identifies this as a dynamic enum field */
193
+ readonly _field: "dynamic_enum";
194
+ /** Unique field identifier used as the schema key */
195
+ readonly name: N;
196
+ /** Data source key for fetching options at runtime */
197
+ readonly source: Source;
198
+ /** Display label for the field */
199
+ readonly label?: string;
200
+ /** Whether this field is required for form submission */
201
+ readonly required?: boolean;
202
+ /** Field names whose values are needed to fetch options */
203
+ readonly params?: readonly string[];
204
+ }
129
205
 
130
206
  /**
131
- * Known field options that can be validated.
207
+ * A field that loads its schema dynamically (e.g., from an extension).
208
+ *
209
+ * @typeParam N - The field name (string literal type)
132
210
  *
133
211
  * @public
134
212
  */
135
- export declare type FieldOption = "label" | "placeholder" | "required" | "minValue" | "maxValue" | "minItems" | "maxItems";
213
+ export declare interface DynamicSchemaField<N extends string> {
214
+ /** Type discriminator for form elements */
215
+ readonly _type: "field";
216
+ /** Field type discriminator - identifies this as a dynamic schema field */
217
+ readonly _field: "dynamic_schema";
218
+ /** Unique field identifier used as the schema key */
219
+ readonly name: N;
220
+ /** Identifier for the schema source */
221
+ readonly schemaSource: string;
222
+ /** Display label for the field */
223
+ readonly label?: string;
224
+ /** Whether this field is required for form submission */
225
+ readonly required?: boolean;
226
+ /** Field names whose values are needed to configure the schema */
227
+ readonly params?: readonly string[];
228
+ }
229
+
230
+ /**
231
+ * An enum option with a separate ID and display label.
232
+ *
233
+ * Use this when the stored value (id) should differ from the display text (label).
234
+ *
235
+ * @public
236
+ */
237
+ export declare interface EnumOption {
238
+ readonly id: string;
239
+ readonly label: string;
240
+ }
241
+
242
+ /**
243
+ * Valid enum option types: either plain strings or objects with id/label.
244
+ *
245
+ * @public
246
+ */
247
+ export declare type EnumOptionValue = string | EnumOption;
248
+
249
+ /* Excluded from this release type: extractFieldOptions */
250
+
251
+ /* Excluded from this release type: FieldOption */
136
252
 
137
253
  /**
138
254
  * Field configuration option constraints - control which field options are allowed.
@@ -156,19 +272,7 @@ export declare interface FieldOptionConstraints {
156
272
  maxItems?: Severity;
157
273
  }
158
274
 
159
- /**
160
- * Context for field option validation.
161
- *
162
- * @public
163
- */
164
- export declare interface FieldOptionsContext {
165
- /** The field name */
166
- fieldName: string;
167
- /** Which options are present on this field */
168
- presentOptions: FieldOption[];
169
- /** Path to this field */
170
- path?: string;
171
- }
275
+ /* Excluded from this release type: FieldOptionsContext */
172
276
 
173
277
  /**
174
278
  * Field type constraints - control which field types are allowed.
@@ -195,18 +299,25 @@ export declare interface FieldTypeConstraints {
195
299
  object?: Severity;
196
300
  }
197
301
 
302
+ /* Excluded from this release type: FieldTypeContext */
303
+
198
304
  /**
199
- * Context for field type validation.
305
+ * Union of all form element types (fields and structural elements).
200
306
  *
201
307
  * @public
202
308
  */
203
- export declare interface FieldTypeContext {
204
- /** The _field discriminator value (e.g., "text", "number", "enum") */
205
- fieldType: string;
206
- /** The field name */
207
- fieldName: string;
208
- /** Optional path for nested fields */
209
- path?: string;
309
+ export declare type FormElement = AnyField | Group<readonly FormElement[]> | Conditional<string, unknown, readonly FormElement[]>;
310
+
311
+ /**
312
+ * A complete form specification.
313
+ *
314
+ * @typeParam Elements - Tuple of top-level form elements
315
+ *
316
+ * @public
317
+ */
318
+ export declare interface FormSpec<Elements extends readonly FormElement[]> {
319
+ /** Top-level form elements */
320
+ readonly elements: Elements;
210
321
  }
211
322
 
212
323
  /**
@@ -230,72 +341,35 @@ export declare interface FormSpecValidationOptions {
230
341
  constraints?: ConstraintConfig;
231
342
  }
232
343
 
233
- /**
234
- * Gets the severity level for a field option.
235
- *
236
- * @param option - The option to check
237
- * @param constraints - Field option constraints
238
- * @returns Severity level, or "off" if not constrained
239
- *
240
- * @public
241
- */
242
- export declare function getFieldOptionSeverity(option: FieldOption, constraints: FieldOptionConstraints): Severity;
344
+ /* Excluded from this release type: getFieldOptionSeverity */
243
345
 
244
- /**
245
- * Gets the severity level for a field type.
246
- *
247
- * @param fieldType - The _field discriminator value
248
- * @param constraints - Field type constraints
249
- * @returns Severity level, or "off" if not constrained
250
- *
251
- * @public
252
- */
253
- export declare function getFieldTypeSeverity(fieldType: string, constraints: FieldTypeConstraints): Severity;
346
+ /* Excluded from this release type: getFieldTypeSeverity */
254
347
 
255
348
  /**
256
- * Checks if a specific field option is allowed.
349
+ * A visual grouping of form elements.
257
350
  *
258
- * @param option - The option to check
259
- * @param constraints - Field option constraints
260
- * @returns true if allowed, false if disallowed
351
+ * Groups provide visual organization and can be rendered as fieldsets or sections.
261
352
  *
262
- * @public
263
- */
264
- export declare function isFieldOptionAllowed(option: FieldOption, constraints: FieldOptionConstraints): boolean;
265
-
266
- /**
267
- * Checks if a field type is allowed by the constraints.
268
- * Useful for quick checks without generating issues.
269
- *
270
- * @param fieldType - The _field discriminator value
271
- * @param constraints - Field type constraints
272
- * @returns true if allowed, false if disallowed
353
+ * @typeParam Elements - Tuple of contained form elements
273
354
  *
274
355
  * @public
275
356
  */
276
- export declare function isFieldTypeAllowed(fieldType: string, constraints: FieldTypeConstraints): boolean;
357
+ export declare interface Group<Elements extends readonly FormElement[]> {
358
+ /** Type discriminator - identifies this as a group element */
359
+ readonly _type: "group";
360
+ /** Display label for the group */
361
+ readonly label: string;
362
+ /** Form elements contained within this group */
363
+ readonly elements: Elements;
364
+ }
277
365
 
278
- /**
279
- * Checks if a layout type is allowed by the constraints.
280
- *
281
- * @param layoutType - The type of layout element
282
- * @param constraints - Layout constraints
283
- * @returns true if allowed, false if disallowed
284
- *
285
- * @public
286
- */
287
- export declare function isLayoutTypeAllowed(layoutType: "group" | "conditional", constraints: LayoutConstraints): boolean;
366
+ /* Excluded from this release type: isFieldOptionAllowed */
288
367
 
289
- /**
290
- * Checks if a nesting depth is allowed.
291
- *
292
- * @param depth - Current nesting depth
293
- * @param constraints - Layout constraints
294
- * @returns true if allowed, false if exceeds limit
295
- *
296
- * @public
297
- */
298
- export declare function isNestingDepthAllowed(depth: number, constraints: LayoutConstraints): boolean;
368
+ /* Excluded from this release type: isFieldTypeAllowed */
369
+
370
+ /* Excluded from this release type: isLayoutTypeAllowed */
371
+
372
+ /* Excluded from this release type: isNestingDepthAllowed */
299
373
 
300
374
  /**
301
375
  * Layout and structure constraints - control grouping, conditionals, nesting.
@@ -311,21 +385,7 @@ export declare interface LayoutConstraints {
311
385
  maxNestingDepth?: number;
312
386
  }
313
387
 
314
- /**
315
- * Context for layout validation.
316
- *
317
- * @public
318
- */
319
- export declare interface LayoutContext {
320
- /** The type of layout element ("group" | "conditional") */
321
- layoutType: "group" | "conditional";
322
- /** Optional label for the element (for groups) */
323
- label?: string;
324
- /** Current nesting depth */
325
- depth: number;
326
- /** Path to this element */
327
- path?: string;
328
- }
388
+ /* Excluded from this release type: LayoutContext */
329
389
 
330
390
  /**
331
391
  * JSONForms layout type constraints.
@@ -415,12 +475,58 @@ export declare interface LoadConfigResult {
415
475
  found: boolean;
416
476
  }
417
477
 
478
+ /* Excluded from this release type: mergeWithDefaults */
479
+
480
+ /**
481
+ * A numeric input field.
482
+ *
483
+ * @typeParam N - The field name (string literal type)
484
+ *
485
+ * @public
486
+ */
487
+ export declare interface NumberField<N extends string> {
488
+ /** Type discriminator for form elements */
489
+ readonly _type: "field";
490
+ /** Field type discriminator - identifies this as a number field */
491
+ readonly _field: "number";
492
+ /** Unique field identifier used as the schema key */
493
+ readonly name: N;
494
+ /** Display label for the field */
495
+ readonly label?: string;
496
+ /** Minimum allowed value */
497
+ readonly min?: number;
498
+ /** Maximum allowed value */
499
+ readonly max?: number;
500
+ /** Whether this field is required for form submission */
501
+ readonly required?: boolean;
502
+ /** Value must be a multiple of this number (use 1 for integer semantics) */
503
+ readonly multipleOf?: number;
504
+ }
505
+
418
506
  /**
419
- * Merges user constraints with defaults, filling in any missing values.
507
+ * An object field containing nested properties.
508
+ *
509
+ * Use this for grouping related fields under a single key in the schema.
510
+ *
511
+ * @typeParam N - The field name (string literal type)
512
+ * @typeParam Properties - The form elements that define the object's properties
420
513
  *
421
514
  * @public
422
515
  */
423
- export declare function mergeWithDefaults(config: ConstraintConfig | undefined): ResolvedConstraintConfig;
516
+ export declare interface ObjectField<N extends string, Properties extends readonly FormElement[]> {
517
+ /** Type discriminator for form elements */
518
+ readonly _type: "field";
519
+ /** Field type discriminator - identifies this as an object field */
520
+ readonly _field: "object";
521
+ /** Unique field identifier used as the schema key */
522
+ readonly name: N;
523
+ /** Form elements that define the properties of this object */
524
+ readonly properties: Properties;
525
+ /** Display label for the field */
526
+ readonly label?: string;
527
+ /** Whether this field is required for form submission */
528
+ readonly required?: boolean;
529
+ }
424
530
 
425
531
  /**
426
532
  * Fully resolved constraint configuration with all properties required.
@@ -495,38 +601,79 @@ export declare interface RuleEffectConstraints {
495
601
  export declare type Severity = "error" | "warn" | "off";
496
602
 
497
603
  /**
498
- * UI Schema feature constraints - control JSONForms-specific features.
604
+ * A field with static enum options (known at compile time).
605
+ *
606
+ * Options can be plain strings or objects with `id` and `label` properties.
607
+ *
608
+ * @typeParam N - The field name (string literal type)
609
+ * @typeParam O - Tuple of option values (strings or EnumOption objects)
499
610
  *
500
611
  * @public
501
612
  */
502
- export declare interface UISchemaConstraints {
503
- /** Layout type constraints */
504
- layouts?: LayoutTypeConstraints;
505
- /** Rule (conditional) constraints */
506
- rules?: RuleConstraints;
613
+ export declare interface StaticEnumField<N extends string, O extends readonly EnumOptionValue[]> {
614
+ /** Type discriminator for form elements */
615
+ readonly _type: "field";
616
+ /** Field type discriminator - identifies this as an enum field */
617
+ readonly _field: "enum";
618
+ /** Unique field identifier used as the schema key */
619
+ readonly name: N;
620
+ /** Array of allowed option values */
621
+ readonly options: O;
622
+ /** Display label for the field */
623
+ readonly label?: string;
624
+ /** Whether this field is required for form submission */
625
+ readonly required?: boolean;
507
626
  }
508
627
 
509
628
  /**
510
- * Validates field options against constraints.
629
+ * Form element type definitions.
630
+ *
631
+ * These types define the structure of form specifications.
632
+ * The structure IS the definition - nesting implies layout and conditional logic.
633
+ */
634
+ /**
635
+ * A text input field.
511
636
  *
512
- * @param context - Information about the field and its options
513
- * @param constraints - Field option constraints
514
- * @returns Array of validation issues (empty if valid)
637
+ * @typeParam N - The field name (string literal type)
515
638
  *
516
639
  * @public
517
640
  */
518
- export declare function validateFieldOptions(context: FieldOptionsContext, constraints: FieldOptionConstraints): ValidationIssue[];
641
+ export declare interface TextField<N extends string> {
642
+ /** Type discriminator for form elements */
643
+ readonly _type: "field";
644
+ /** Field type discriminator - identifies this as a text field */
645
+ readonly _field: "text";
646
+ /** Unique field identifier used as the schema key */
647
+ readonly name: N;
648
+ /** Display label for the field */
649
+ readonly label?: string;
650
+ /** Placeholder text shown when field is empty */
651
+ readonly placeholder?: string;
652
+ /** Whether this field is required for form submission */
653
+ readonly required?: boolean;
654
+ /** Minimum string length */
655
+ readonly minLength?: number;
656
+ /** Maximum string length */
657
+ readonly maxLength?: number;
658
+ /** Regular expression pattern the value must match */
659
+ readonly pattern?: string;
660
+ }
519
661
 
520
662
  /**
521
- * Validates a field type against constraints.
522
- *
523
- * @param context - Information about the field being validated
524
- * @param constraints - Field type constraints
525
- * @returns Array of validation issues (empty if valid)
663
+ * UI Schema feature constraints - control JSONForms-specific features.
526
664
  *
527
665
  * @public
528
666
  */
529
- export declare function validateFieldTypes(context: FieldTypeContext, constraints: FieldTypeConstraints): ValidationIssue[];
667
+ export declare interface UISchemaConstraints {
668
+ /** Layout type constraints */
669
+ layouts?: LayoutTypeConstraints;
670
+ /** Rule (conditional) constraints */
671
+ rules?: RuleConstraints;
672
+ }
673
+
674
+ /* Excluded from this release type: validateFieldOptions */
675
+
676
+ /* Excluded from this release type: validateFieldTypes */
530
677
 
531
678
  /**
532
679
  * Validates a complete FormSpec against constraints.
@@ -578,16 +725,7 @@ export declare function validateFormSpec(formSpec: FormSpec<readonly FormElement
578
725
  */
579
726
  export declare function validateFormSpecElements(elements: readonly FormElement[], options?: FormSpecValidationOptions): ValidationResult;
580
727
 
581
- /**
582
- * Validates a layout element against constraints.
583
- *
584
- * @param context - Information about the layout element
585
- * @param constraints - Layout constraints
586
- * @returns Array of validation issues (empty if valid)
587
- *
588
- * @public
589
- */
590
- export declare function validateLayout(context: LayoutContext, constraints: LayoutConstraints): ValidationIssue[];
728
+ /* Excluded from this release type: validateLayout */
591
729
 
592
730
  /**
593
731
  * A single validation issue found during constraint checking.
@@ -3,19 +3,19 @@ import type { ConstraintConfig, FormSpecConfig, ResolvedConstraintConfig } from
3
3
  * Default constraint configuration that allows all features.
4
4
  * All constraints default to "off" (allowed).
5
5
  *
6
- * @public
6
+ * @beta
7
7
  */
8
8
  export declare const DEFAULT_CONSTRAINTS: ResolvedConstraintConfig;
9
9
  /**
10
10
  * Default FormSpec configuration.
11
11
  *
12
- * @public
12
+ * @beta
13
13
  */
14
14
  export declare const DEFAULT_CONFIG: FormSpecConfig;
15
15
  /**
16
16
  * Merges user constraints with defaults, filling in any missing values.
17
17
  *
18
- * @public
18
+ * @beta
19
19
  */
20
20
  export declare function mergeWithDefaults(config: ConstraintConfig | undefined): ResolvedConstraintConfig;
21
21
  //# sourceMappingURL=defaults.d.ts.map