@formspec/build 0.1.0-alpha.12 → 0.1.0-alpha.13

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.
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/browser.ts","../src/canonicalize/chain-dsl-canonicalizer.ts","../src/canonicalize/tsdoc-canonicalizer.ts","../src/json-schema/ir-generator.ts","../src/json-schema/generator.ts","../src/ui-schema/schema.ts","../src/ui-schema/ir-generator.ts","../src/ui-schema/generator.ts","../src/json-schema/types.ts","../src/json-schema/schema.ts","../src/validate/constraint-validator.ts"],"sourcesContent":["/**\n * Browser-safe exports for `@formspec/build`.\n *\n * This entry point excludes Node.js-specific functions like `writeSchemas`\n * that use `node:fs` and `node:path`, making it suitable for browser environments.\n *\n * @example\n * ```typescript\n * // In browser code (e.g., playground)\n * import { buildFormSchemas, generateJsonSchema, generateUiSchema } from \"@formspec/build/browser\";\n *\n * const form = formspec(field.text(\"name\"));\n * const { jsonSchema, uiSchema } = buildFormSchemas(form);\n * ```\n *\n * @packageDocumentation\n */\n\nimport type { FormElement, FormSpec } from \"@formspec/core\";\nimport { generateJsonSchema } from \"./json-schema/generator.js\";\nimport { generateUiSchema } from \"./ui-schema/generator.js\";\nimport type { JsonSchema2020 } from \"./json-schema/ir-generator.js\";\nimport type { UISchema } from \"./ui-schema/types.js\";\n\n// Re-export types\nexport type { JsonSchema2020 } from \"./json-schema/ir-generator.js\";\n\nexport type {\n JSONSchema7,\n JSONSchemaType,\n ExtendedJSONSchema7,\n FormSpecSchemaExtensions,\n} from \"./json-schema/types.js\";\n\nexport { setSchemaExtension, getSchemaExtension } from \"./json-schema/types.js\";\n\nexport type {\n UISchema,\n UISchemaElement,\n UISchemaElementBase,\n UISchemaElementType,\n ControlElement,\n VerticalLayout,\n HorizontalLayout,\n GroupLayout,\n Categorization,\n Category,\n LabelElement,\n Rule,\n RuleEffect,\n RuleConditionSchema,\n SchemaBasedCondition,\n} from \"./ui-schema/types.js\";\n\n// Zod validation schemas\nexport {\n ruleEffectSchema,\n uiSchemaElementTypeSchema,\n ruleConditionSchema,\n schemaBasedConditionSchema,\n ruleSchema,\n controlSchema,\n verticalLayoutSchema,\n horizontalLayoutSchema,\n groupLayoutSchema,\n categorizationSchema,\n categorySchema,\n labelElementSchema,\n uiSchemaElementSchema,\n uiSchema as uiSchemaSchema,\n} from \"./ui-schema/schema.js\";\n\nexport { jsonSchemaTypeSchema, jsonSchema7Schema } from \"./json-schema/schema.js\";\n\n// Re-export individual generators\nexport { generateJsonSchema } from \"./json-schema/generator.js\";\nexport { generateUiSchema } from \"./ui-schema/generator.js\";\n\n// IR canonicalization (browser-safe: no Node.js dependencies)\nexport { canonicalizeChainDSL } from \"./canonicalize/chain-dsl-canonicalizer.js\";\n\n// IR validation (browser-safe: no Node.js dependencies)\nexport { validateIR } from \"./validate/constraint-validator.js\";\nexport type {\n ValidationDiagnostic,\n ValidationResult,\n ValidateIROptions,\n} from \"./validate/constraint-validator.js\";\n\n/**\n * Result of building form schemas.\n */\nexport interface BuildResult {\n /** JSON Schema 2020-12 for validation */\n readonly jsonSchema: JsonSchema2020;\n /** JSON Forms UI Schema for rendering */\n readonly uiSchema: UISchema;\n}\n\n/**\n * Builds both JSON Schema and UI Schema from a FormSpec.\n *\n * This is a browser-safe version that does not include file system operations.\n *\n * @example\n * ```typescript\n * const form = formspec(\n * field.text(\"name\", { required: true }),\n * field.number(\"age\", { min: 0 }),\n * );\n *\n * const { jsonSchema, uiSchema } = buildFormSchemas(form);\n * ```\n *\n * @param form - The FormSpec to build schemas from\n * @returns Object containing both jsonSchema and uiSchema\n */\nexport function buildFormSchemas<E extends readonly FormElement[]>(form: FormSpec<E>): BuildResult {\n return {\n jsonSchema: generateJsonSchema(form),\n uiSchema: generateUiSchema(form),\n };\n}\n","/**\n * Canonicalizer that translates chain DSL `FormSpec` objects into the\n * canonical FormIR intermediate representation.\n *\n * This module maps the runtime objects produced by `@formspec/dsl` builder\n * functions (`field.*`, `group`, `when`, `formspec`) into the IR that all\n * downstream phases (validation, JSON Schema generation, UI Schema generation)\n * consume.\n */\n\nimport type {\n // Source types (chain DSL)\n AnyField,\n ArrayField,\n BooleanField,\n Conditional,\n DynamicEnumField,\n DynamicSchemaField,\n EnumOptionValue,\n FormElement,\n FormSpec,\n Group,\n NumberField,\n ObjectField,\n StaticEnumField,\n TextField,\n // IR types\n JsonValue,\n AnnotationNode,\n ArrayTypeNode,\n ConstraintNode,\n ConditionalLayoutNode,\n DisplayNameAnnotationNode,\n DynamicTypeNode,\n EnumMember,\n EnumTypeNode,\n FieldNode,\n FormIR,\n FormIRElement,\n GroupLayoutNode,\n LengthConstraintNode,\n NumericConstraintNode,\n ObjectProperty,\n ObjectTypeNode,\n PlaceholderAnnotationNode,\n PrimitiveTypeNode,\n Provenance,\n TypeNode,\n} from \"@formspec/core\";\nimport { IR_VERSION } from \"@formspec/core\";\n\n// =============================================================================\n// CONSTANTS\n// =============================================================================\n\n/** Default provenance for chain DSL nodes (no source location available). */\nconst CHAIN_DSL_PROVENANCE: Provenance = {\n surface: \"chain-dsl\",\n file: \"\",\n line: 0,\n column: 0,\n} as const;\n\n// =============================================================================\n// TYPE GUARDS\n// =============================================================================\n\nfunction isGroup(el: FormElement): el is Group<readonly FormElement[]> {\n return el._type === \"group\";\n}\n\nfunction isConditional(\n el: FormElement\n): el is Conditional<string, unknown, readonly FormElement[]> {\n return el._type === \"conditional\";\n}\n\nfunction isField(el: FormElement): el is AnyField {\n return el._type === \"field\";\n}\n\n// =============================================================================\n// PUBLIC API\n// =============================================================================\n\n/**\n * Translates a chain DSL `FormSpec` into the canonical `FormIR`.\n *\n * @param form - A form specification created via `formspec(...)` from `@formspec/dsl`\n * @returns The canonical intermediate representation\n */\nexport function canonicalizeChainDSL(form: FormSpec<readonly FormElement[]>): FormIR {\n return {\n kind: \"form-ir\",\n irVersion: IR_VERSION,\n elements: canonicalizeElements(form.elements),\n typeRegistry: {},\n provenance: CHAIN_DSL_PROVENANCE,\n };\n}\n\n// =============================================================================\n// ELEMENT CANONICALIZATION\n// =============================================================================\n\n/**\n * Canonicalizes an array of chain DSL form elements into IR elements.\n */\nfunction canonicalizeElements(elements: readonly FormElement[]): FormIRElement[] {\n return elements.map(canonicalizeElement);\n}\n\n/**\n * Dispatches a single form element to its specific canonicalization function.\n */\nfunction canonicalizeElement(element: FormElement): FormIRElement {\n if (isField(element)) {\n return canonicalizeField(element);\n }\n if (isGroup(element)) {\n return canonicalizeGroup(element);\n }\n if (isConditional(element)) {\n return canonicalizeConditional(element);\n }\n const _exhaustive: never = element;\n throw new Error(`Unknown element type: ${JSON.stringify(_exhaustive)}`);\n}\n\n// =============================================================================\n// FIELD CANONICALIZATION\n// =============================================================================\n\n/**\n * Dispatches a field element to its type-specific canonicalization function.\n */\nfunction canonicalizeField(field: AnyField): FieldNode {\n switch (field._field) {\n case \"text\":\n return canonicalizeTextField(field);\n case \"number\":\n return canonicalizeNumberField(field);\n case \"boolean\":\n return canonicalizeBooleanField(field);\n case \"enum\":\n return canonicalizeStaticEnumField(field);\n case \"dynamic_enum\":\n return canonicalizeDynamicEnumField(field);\n case \"dynamic_schema\":\n return canonicalizeDynamicSchemaField(field);\n case \"array\":\n return canonicalizeArrayField(field);\n case \"object\":\n return canonicalizeObjectField(field);\n default: {\n const _exhaustive: never = field;\n throw new Error(`Unknown field type: ${JSON.stringify(_exhaustive)}`);\n }\n }\n}\n\n// =============================================================================\n// SPECIFIC FIELD TYPE CANONICALIZERS\n// =============================================================================\n\nfunction canonicalizeTextField(field: TextField<string>): FieldNode {\n const type: PrimitiveTypeNode = { kind: \"primitive\", primitiveKind: \"string\" };\n return buildFieldNode(\n field.name,\n type,\n field.required,\n buildAnnotations(field.label, field.placeholder)\n );\n}\n\nfunction canonicalizeNumberField(field: NumberField<string>): FieldNode {\n const type: PrimitiveTypeNode = { kind: \"primitive\", primitiveKind: \"number\" };\n const constraints: ConstraintNode[] = [];\n\n if (field.min !== undefined) {\n const c: NumericConstraintNode = {\n kind: \"constraint\",\n constraintKind: \"minimum\",\n value: field.min,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n constraints.push(c);\n }\n\n if (field.max !== undefined) {\n const c: NumericConstraintNode = {\n kind: \"constraint\",\n constraintKind: \"maximum\",\n value: field.max,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n constraints.push(c);\n }\n\n return buildFieldNode(\n field.name,\n type,\n field.required,\n buildAnnotations(field.label),\n constraints\n );\n}\n\nfunction canonicalizeBooleanField(field: BooleanField<string>): FieldNode {\n const type: PrimitiveTypeNode = { kind: \"primitive\", primitiveKind: \"boolean\" };\n return buildFieldNode(field.name, type, field.required, buildAnnotations(field.label));\n}\n\nfunction canonicalizeStaticEnumField(\n field: StaticEnumField<string, readonly EnumOptionValue[]>\n): FieldNode {\n const members: EnumMember[] = field.options.map((opt) => {\n if (typeof opt === \"string\") {\n return { value: opt } satisfies EnumMember;\n }\n // Object option with id/label\n return { value: opt.id, displayName: opt.label } satisfies EnumMember;\n });\n\n const type: EnumTypeNode = { kind: \"enum\", members };\n return buildFieldNode(field.name, type, field.required, buildAnnotations(field.label));\n}\n\nfunction canonicalizeDynamicEnumField(field: DynamicEnumField<string, string>): FieldNode {\n const type: DynamicTypeNode = {\n kind: \"dynamic\",\n dynamicKind: \"enum\",\n sourceKey: field.source,\n parameterFields: field.params ? [...field.params] : [],\n };\n return buildFieldNode(field.name, type, field.required, buildAnnotations(field.label));\n}\n\nfunction canonicalizeDynamicSchemaField(field: DynamicSchemaField<string>): FieldNode {\n const type: DynamicTypeNode = {\n kind: \"dynamic\",\n dynamicKind: \"schema\",\n sourceKey: field.schemaSource,\n parameterFields: [],\n };\n return buildFieldNode(field.name, type, field.required, buildAnnotations(field.label));\n}\n\nfunction canonicalizeArrayField(field: ArrayField<string, readonly FormElement[]>): FieldNode {\n // Array items form an object type from the sub-elements\n const itemProperties = buildObjectProperties(field.items);\n const itemsType: ObjectTypeNode = {\n kind: \"object\",\n properties: itemProperties,\n additionalProperties: false,\n };\n const type: ArrayTypeNode = { kind: \"array\", items: itemsType };\n\n const constraints: ConstraintNode[] = [];\n if (field.minItems !== undefined) {\n const c: LengthConstraintNode = {\n kind: \"constraint\",\n constraintKind: \"minItems\",\n value: field.minItems,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n constraints.push(c);\n }\n if (field.maxItems !== undefined) {\n const c: LengthConstraintNode = {\n kind: \"constraint\",\n constraintKind: \"maxItems\",\n value: field.maxItems,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n constraints.push(c);\n }\n\n return buildFieldNode(\n field.name,\n type,\n field.required,\n buildAnnotations(field.label),\n constraints\n );\n}\n\nfunction canonicalizeObjectField(field: ObjectField<string, readonly FormElement[]>): FieldNode {\n const properties = buildObjectProperties(field.properties);\n const type: ObjectTypeNode = {\n kind: \"object\",\n properties,\n additionalProperties: false,\n };\n return buildFieldNode(field.name, type, field.required, buildAnnotations(field.label));\n}\n\n// =============================================================================\n// LAYOUT CANONICALIZATION\n// =============================================================================\n\nfunction canonicalizeGroup(g: Group<readonly FormElement[]>): GroupLayoutNode {\n return {\n kind: \"group\",\n label: g.label,\n elements: canonicalizeElements(g.elements),\n provenance: CHAIN_DSL_PROVENANCE,\n };\n}\n\nfunction canonicalizeConditional(\n c: Conditional<string, unknown, readonly FormElement[]>\n): ConditionalLayoutNode {\n return {\n kind: \"conditional\",\n fieldName: c.field,\n // Conditional values from the chain DSL are JSON-serializable primitives\n // (strings, numbers, booleans) produced by the `is()` predicate helper.\n value: assertJsonValue(c.value),\n elements: canonicalizeElements(c.elements),\n provenance: CHAIN_DSL_PROVENANCE,\n };\n}\n\n// =============================================================================\n// HELPERS\n// =============================================================================\n\n/**\n * Validates that a value is JSON-serializable (`JsonValue`).\n * The chain DSL's `is()` helper constrains conditional values to\n * JSON-compatible primitives, but the TypeScript type is `unknown`.\n * This runtime guard replaces an `as` cast with a validated assertion.\n */\nfunction assertJsonValue(v: unknown): JsonValue {\n if (v === null || typeof v === \"string\" || typeof v === \"number\" || typeof v === \"boolean\") {\n return v;\n }\n if (Array.isArray(v)) {\n return v.map(assertJsonValue);\n }\n if (typeof v === \"object\") {\n const result: Record<string, JsonValue> = {};\n for (const [key, val] of Object.entries(v)) {\n result[key] = assertJsonValue(val);\n }\n return result;\n }\n // Remaining types (function, symbol, bigint, undefined) are not JSON-serializable\n throw new TypeError(`Conditional value is not a valid JsonValue: ${typeof v}`);\n}\n\n/**\n * Builds a FieldNode from common field properties.\n */\nfunction buildFieldNode(\n name: string,\n type: TypeNode,\n required: boolean | undefined,\n annotations: AnnotationNode[],\n constraints: ConstraintNode[] = []\n): FieldNode {\n return {\n kind: \"field\",\n name,\n type,\n required: required === true,\n constraints,\n annotations,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n}\n\n/**\n * Builds annotation nodes from optional label and placeholder values.\n */\nfunction buildAnnotations(label?: string, placeholder?: string): AnnotationNode[] {\n const annotations: AnnotationNode[] = [];\n\n if (label !== undefined) {\n const a: DisplayNameAnnotationNode = {\n kind: \"annotation\",\n annotationKind: \"displayName\",\n value: label,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n annotations.push(a);\n }\n\n if (placeholder !== undefined) {\n const a: PlaceholderAnnotationNode = {\n kind: \"annotation\",\n annotationKind: \"placeholder\",\n value: placeholder,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n annotations.push(a);\n }\n\n return annotations;\n}\n\n/**\n * Converts an array of form elements into ObjectProperty nodes.\n * Used for ObjectField properties and ArrayField items.\n *\n * Only field elements produce properties; groups and conditionals within\n * an object/array context are recursively flattened to extract their fields.\n *\n * Fields inside conditional branches are always marked `optional: true`\n * because their presence in the data depends on the condition being met.\n * This matches the DSL's type inference behavior where conditional fields\n * produce optional properties in `InferFormSchema`.\n *\n * @param elements - The form elements to convert\n * @param insideConditional - Whether these elements are inside a conditional branch\n */\nfunction buildObjectProperties(\n elements: readonly FormElement[],\n insideConditional = false\n): ObjectProperty[] {\n const properties: ObjectProperty[] = [];\n\n for (const el of elements) {\n if (isField(el)) {\n const fieldNode = canonicalizeField(el);\n properties.push({\n name: fieldNode.name,\n type: fieldNode.type,\n // Fields inside a conditional branch are always optional in the\n // data schema, regardless of their `required` flag — the condition\n // may not be met, so the field may be absent.\n optional: insideConditional || !fieldNode.required,\n constraints: fieldNode.constraints,\n annotations: fieldNode.annotations,\n provenance: CHAIN_DSL_PROVENANCE,\n });\n } else if (isGroup(el)) {\n // Groups inside object/array items contribute their fields by flattening.\n // Groups do not affect optionality — pass through the current state.\n properties.push(...buildObjectProperties(el.elements, insideConditional));\n } else if (isConditional(el)) {\n // Conditionals inside object/array items contribute their fields by\n // flattening, but all fields inside are forced optional.\n properties.push(...buildObjectProperties(el.elements, true));\n }\n }\n\n return properties;\n}\n","/**\n * TSDoc canonicalizer — assembles an {@link IRClassAnalysis} into a canonical\n * {@link FormIR}, applying layout metadata from `@Group` and `@ShowWhen`\n * decorators.\n *\n * The analysis functions in `class-analyzer.ts` produce `FieldNode[]`,\n * `fieldLayouts`, and `typeRegistry` directly. This canonicalizer uses\n * the layout metadata to wrap fields in `GroupLayoutNode` and\n * `ConditionalLayoutNode` elements.\n */\n\nimport type {\n FormIR,\n FormIRElement,\n FieldNode,\n GroupLayoutNode,\n ConditionalLayoutNode,\n Provenance,\n} from \"@formspec/core\";\nimport { IR_VERSION } from \"@formspec/core\";\nimport type { IRClassAnalysis, FieldLayoutMetadata } from \"../analyzer/class-analyzer.js\";\n\n/**\n * Source-level metadata for provenance tracking.\n */\nexport interface TSDocSource {\n /** Absolute path to the source file. */\n readonly file: string;\n}\n\n/**\n * Wraps an {@link IRClassAnalysis} (from `analyzeClassToIR`,\n * `analyzeInterfaceToIR`, or `analyzeTypeAliasToIR`) into a canonical\n * {@link FormIR}.\n *\n * Fields with `@Group` decorators are grouped into `GroupLayoutNode` elements.\n * Fields with `@ShowWhen` decorators are wrapped in `ConditionalLayoutNode` elements.\n * When both are present, the conditional wraps the field inside the group.\n *\n * @param analysis - IR analysis result (fields are already FieldNode[])\n * @param source - Optional source file metadata for provenance\n * @returns The canonical FormIR\n */\nexport function canonicalizeTSDoc(analysis: IRClassAnalysis, source?: TSDocSource): FormIR {\n const file = source?.file ?? \"\";\n\n const provenance: Provenance = {\n surface: \"tsdoc\",\n file,\n line: 1,\n column: 0,\n };\n\n const elements = assembleElements(analysis.fields, analysis.fieldLayouts, provenance);\n\n return {\n kind: \"form-ir\",\n irVersion: IR_VERSION,\n elements,\n typeRegistry: analysis.typeRegistry,\n provenance,\n };\n}\n\n/**\n * Assembles flat fields and their layout metadata into a tree of\n * `FormIRElement[]` with groups and conditionals.\n *\n * Fields are processed in order. Consecutive fields with the same\n * `@Group` label are collected into a single `GroupLayoutNode`.\n * Fields with `@ShowWhen` are wrapped in `ConditionalLayoutNode`.\n */\nfunction assembleElements(\n fields: readonly FieldNode[],\n layouts: readonly FieldLayoutMetadata[],\n provenance: Provenance\n): readonly FormIRElement[] {\n const elements: FormIRElement[] = [];\n\n // Group consecutive fields with the same group label together.\n // We use an ordered map to preserve insertion order of groups.\n const groupMap = new Map<string, FormIRElement[]>();\n const topLevelOrder: (\n | { type: \"group\"; label: string }\n | { type: \"element\"; element: FormIRElement }\n )[] = [];\n\n for (let i = 0; i < fields.length; i++) {\n const field = fields[i];\n const layout = layouts[i];\n if (!field || !layout) continue;\n\n // Wrap in conditional if @ShowWhen is present\n const element = wrapInConditional(field, layout, provenance);\n\n if (layout.groupLabel !== undefined) {\n const label = layout.groupLabel;\n let groupElements = groupMap.get(label);\n if (!groupElements) {\n groupElements = [];\n groupMap.set(label, groupElements);\n topLevelOrder.push({ type: \"group\", label });\n }\n groupElements.push(element);\n } else {\n topLevelOrder.push({ type: \"element\", element });\n }\n }\n\n // Assemble the final element array in order\n for (const entry of topLevelOrder) {\n if (entry.type === \"group\") {\n const groupElements = groupMap.get(entry.label);\n if (groupElements) {\n const groupNode: GroupLayoutNode = {\n kind: \"group\",\n label: entry.label,\n elements: groupElements,\n provenance,\n };\n elements.push(groupNode);\n // Clear so duplicate group labels in topLevelOrder don't re-emit\n groupMap.delete(entry.label);\n }\n } else {\n elements.push(entry.element);\n }\n }\n\n return elements;\n}\n\n/**\n * Wraps a field in a `ConditionalLayoutNode` if the layout has `showWhen` metadata.\n */\nfunction wrapInConditional(\n field: FieldNode,\n layout: FieldLayoutMetadata,\n provenance: Provenance\n): FormIRElement {\n if (layout.showWhen === undefined) {\n return field;\n }\n\n const conditional: ConditionalLayoutNode = {\n kind: \"conditional\",\n fieldName: layout.showWhen.field,\n value: layout.showWhen.value,\n elements: [field],\n provenance,\n };\n\n return conditional;\n}\n","/**\n * JSON Schema 2020-12 generator that consumes the canonical FormIR.\n *\n * This generator is a pure function of the IR. It never consults the TypeScript\n * AST or surface syntax directly — only the IR (per the JSON Schema vocabulary spec §1.2).\n *\n * @see https://json-schema.org/draft/2020-12/schema\n * @see https://json-schema.org/draft/2020-12/schema\n */\n\nimport type {\n FormIR,\n FormIRElement,\n FieldNode,\n TypeNode,\n PrimitiveTypeNode,\n EnumTypeNode,\n ArrayTypeNode,\n ObjectTypeNode,\n UnionTypeNode,\n ReferenceTypeNode,\n DynamicTypeNode,\n CustomTypeNode,\n ConstraintNode,\n AnnotationNode,\n ObjectProperty,\n} from \"@formspec/core\";\n\n// =============================================================================\n// OUTPUT TYPE\n// =============================================================================\n\n/**\n * A JSON Schema 2020-12 document, sub-schema, or keyword collection.\n *\n * This interface covers the subset of JSON Schema 2020-12 that this generator\n * emits, plus an index signature for custom `x-formspec-*` extension keywords.\n */\nexport interface JsonSchema2020 {\n $schema?: string;\n $ref?: string;\n $defs?: Record<string, JsonSchema2020>;\n type?: string;\n properties?: Record<string, JsonSchema2020>;\n required?: string[];\n items?: JsonSchema2020;\n additionalProperties?: boolean;\n enum?: readonly (string | number)[];\n const?: string | number | boolean | null;\n oneOf?: readonly JsonSchema2020[];\n anyOf?: readonly JsonSchema2020[];\n // Constraints\n minimum?: number;\n maximum?: number;\n exclusiveMinimum?: number;\n exclusiveMaximum?: number;\n multipleOf?: number;\n minLength?: number;\n maxLength?: number;\n minItems?: number;\n maxItems?: number;\n pattern?: string;\n uniqueItems?: boolean;\n // Annotations\n title?: string;\n description?: string;\n default?: unknown;\n deprecated?: boolean;\n // Extensions (open for vendor-prefixed keywords, e.g., x-formspec-*, x-stripe-*)\n // The vendor prefix is configurable (white-labelable).\n [key: `x-${string}`]: unknown;\n}\n\n// =============================================================================\n// CONTEXT\n// =============================================================================\n\n/**\n * Mutable accumulator passed through the generation traversal.\n *\n * Using a context object rather than return-value threading keeps the\n * recursive generators simple and avoids repeated object spreading.\n */\ninterface GeneratorContext {\n /** Named type schemas collected during traversal, keyed by reference name. */\n readonly defs: Record<string, JsonSchema2020>;\n}\n\nfunction makeContext(): GeneratorContext {\n return { defs: {} };\n}\n\n// =============================================================================\n// PUBLIC API\n// =============================================================================\n\n/**\n * Generates a JSON Schema 2020-12 object from a canonical FormIR.\n *\n * Groups and conditionals are flattened — they influence UI layout but do not\n * affect the data schema. All fields appear at the level they would occupy in\n * the output data.\n *\n * Named types in the `typeRegistry` are emitted as `$defs` entries and\n * referenced via `$ref` (per PP7 — high-fidelity output).\n *\n * @example\n * ```typescript\n * import { canonicalizeDSL } from \"./canonicalize/index.js\";\n * import { generateJsonSchemaFromIR } from \"./json-schema/ir-generator.js\";\n * import { formspec, field } from \"@formspec/dsl\";\n *\n * const form = formspec(\n * field.text(\"name\", { label: \"Name\", required: true }),\n * field.number(\"age\", { min: 0 }),\n * );\n * const ir = canonicalizeDSL(form);\n * const schema = generateJsonSchemaFromIR(ir);\n * // {\n * // $schema: \"https://json-schema.org/draft/2020-12/schema\",\n * // type: \"object\",\n * // properties: {\n * // name: { type: \"string\", title: \"Name\" },\n * // age: { type: \"number\", minimum: 0 }\n * // },\n * // required: [\"name\"]\n * // }\n * ```\n *\n * @param ir - The canonical FormIR produced by a canonicalizer\n * @returns A plain JSON-serializable JSON Schema 2020-12 object\n */\nexport function generateJsonSchemaFromIR(ir: FormIR): JsonSchema2020 {\n const ctx = makeContext();\n\n // Seed $defs from the type registry so referenced types are available even if\n // the field tree traversal never visits them (e.g., unreferenced types added\n // by a TSDoc canonicalizer pass).\n for (const [name, typeDef] of Object.entries(ir.typeRegistry)) {\n ctx.defs[name] = generateTypeNode(typeDef.type, ctx);\n }\n\n const properties: Record<string, JsonSchema2020> = {};\n const required: string[] = [];\n\n collectFields(ir.elements, properties, required, ctx);\n\n // Deduplicate required (same field can appear across conditional branches).\n const uniqueRequired = [...new Set(required)];\n\n const result: JsonSchema2020 = {\n $schema: \"https://json-schema.org/draft/2020-12/schema\",\n type: \"object\",\n properties,\n ...(uniqueRequired.length > 0 && { required: uniqueRequired }),\n };\n\n if (Object.keys(ctx.defs).length > 0) {\n result.$defs = ctx.defs;\n }\n\n return result;\n}\n\n// =============================================================================\n// ELEMENT TRAVERSAL\n// =============================================================================\n\n/**\n * Recursively visits all IR elements, collecting field schemas and required names.\n *\n * Groups and conditionals are transparent to the schema — their children are\n * lifted to the enclosing level (per the JSON Schema vocabulary spec §1.2).\n */\nfunction collectFields(\n elements: readonly FormIRElement[],\n properties: Record<string, JsonSchema2020>,\n required: string[],\n ctx: GeneratorContext\n): void {\n for (const element of elements) {\n switch (element.kind) {\n case \"field\":\n properties[element.name] = generateFieldSchema(element, ctx);\n if (element.required) {\n required.push(element.name);\n }\n break;\n\n case \"group\":\n // Groups are UI-only; flatten children into the enclosing schema.\n collectFields(element.elements, properties, required, ctx);\n break;\n\n case \"conditional\":\n // Conditional visibility is UI-only; all fields remain in the schema.\n collectFields(element.elements, properties, required, ctx);\n break;\n\n default: {\n const _exhaustive: never = element;\n void _exhaustive;\n }\n }\n }\n}\n\n// =============================================================================\n// FIELD SCHEMA GENERATION\n// =============================================================================\n\n/**\n * Generates the JSON Schema sub-schema for a single FieldNode.\n */\nfunction generateFieldSchema(field: FieldNode, ctx: GeneratorContext): JsonSchema2020 {\n const schema = generateTypeNode(field.type, ctx);\n\n // Apply constraints. multipleOf:1 on a number type is a special case: it\n // promotes the type to \"integer\" and removes the multipleOf keyword.\n applyConstraints(schema, field.constraints);\n\n // Apply annotations (title, description, default, deprecated, etc.).\n applyAnnotations(schema, field.annotations);\n\n return schema;\n}\n\n// =============================================================================\n// TYPE NODE GENERATION\n// =============================================================================\n\n/**\n * Converts a TypeNode to a JSON Schema sub-schema.\n *\n * This function is intentionally exhaustive — all TypeNode variants are handled.\n * TypeScript's exhaustiveness check via the default branch ensures new variants\n * added to the IR are caught at compile time.\n */\nfunction generateTypeNode(type: TypeNode, ctx: GeneratorContext): JsonSchema2020 {\n switch (type.kind) {\n case \"primitive\":\n return generatePrimitiveType(type);\n\n case \"enum\":\n return generateEnumType(type);\n\n case \"array\":\n return generateArrayType(type, ctx);\n\n case \"object\":\n return generateObjectType(type, ctx);\n\n case \"union\":\n return generateUnionType(type, ctx);\n\n case \"reference\":\n return generateReferenceType(type);\n\n case \"dynamic\":\n return generateDynamicType(type);\n\n case \"custom\":\n return generateCustomType(type);\n\n default: {\n // TypeScript exhaustiveness guard.\n const _exhaustive: never = type;\n return _exhaustive;\n }\n }\n}\n\n/**\n * Maps primitive IR types to JSON Schema type keywords.\n *\n * Note: `integer` is NOT a primitive kind in the IR. Integer semantics are\n * expressed via a `multipleOf: 1` constraint on a number type; `applyConstraints`\n * handles the promotion (per the JSON Schema vocabulary spec §2.1).\n */\nfunction generatePrimitiveType(type: PrimitiveTypeNode): JsonSchema2020 {\n return { type: type.primitiveKind };\n}\n\n/**\n * Generates JSON Schema for a static enum type.\n *\n * When any member has a displayName, the output uses the `oneOf` form with\n * per-member `const`/`title` entries (per the JSON Schema vocabulary spec §2.3). Otherwise the\n * flat `enum` keyword is used (simpler, equally valid).\n */\nfunction generateEnumType(type: EnumTypeNode): JsonSchema2020 {\n const hasDisplayNames = type.members.some((m) => m.displayName !== undefined);\n\n if (hasDisplayNames) {\n return {\n oneOf: type.members.map((m) => {\n const entry: JsonSchema2020 = { const: m.value };\n if (m.displayName !== undefined) {\n entry.title = m.displayName;\n }\n return entry;\n }),\n };\n }\n\n return { enum: type.members.map((m) => m.value) };\n}\n\n/**\n * Generates JSON Schema for an array type.\n * Per 2020-12, `items` is a single schema (not an array); tuple types use\n * `prefixItems` + `items: false`.\n */\nfunction generateArrayType(type: ArrayTypeNode, ctx: GeneratorContext): JsonSchema2020 {\n return {\n type: \"array\",\n items: generateTypeNode(type.items, ctx),\n };\n}\n\n/**\n * Generates JSON Schema for an object type.\n *\n * `additionalProperties` is only emitted when the IR explicitly disallows extra\n * properties. The default per the JSON Schema vocabulary spec §2.5 is to omit it (allow policy).\n */\nfunction generateObjectType(type: ObjectTypeNode, ctx: GeneratorContext): JsonSchema2020 {\n const properties: Record<string, JsonSchema2020> = {};\n const required: string[] = [];\n\n for (const prop of type.properties) {\n properties[prop.name] = generatePropertySchema(prop, ctx);\n if (!prop.optional) {\n required.push(prop.name);\n }\n }\n\n const schema: JsonSchema2020 = { type: \"object\", properties };\n\n if (required.length > 0) {\n schema.required = required;\n }\n\n if (!type.additionalProperties) {\n // IR default is false (closed objects). Emit explicitly when disallowed.\n schema.additionalProperties = false;\n }\n\n return schema;\n}\n\n/**\n * Generates a schema for an ObjectProperty, applying its use-site constraints\n * and annotations (per the JSON Schema vocabulary spec §5.4 — inline allOf at use site).\n */\nfunction generatePropertySchema(prop: ObjectProperty, ctx: GeneratorContext): JsonSchema2020 {\n const schema = generateTypeNode(prop.type, ctx);\n applyConstraints(schema, prop.constraints);\n applyAnnotations(schema, prop.annotations);\n return schema;\n}\n\n/**\n * Generates JSON Schema for a union type.\n *\n * Union handling strategy:\n * - Boolean shorthand: `true | false` → `{ type: \"boolean\" }` (not anyOf)\n * - All other unions → `anyOf` (members may overlap; discriminated union\n * detection is deferred to a future phase per design doc 003 §7.4)\n */\nfunction generateUnionType(type: UnionTypeNode, ctx: GeneratorContext): JsonSchema2020 {\n // Boolean shorthand: union of true-literal and false-literal → type: \"boolean\"\n if (isBooleanUnion(type)) {\n return { type: \"boolean\" };\n }\n\n // Default: anyOf for all non-boolean unions.\n // Discriminated union detection (shared required property with distinct consts)\n // is deferred to a future phase.\n return {\n anyOf: type.members.map((m) => generateTypeNode(m, ctx)),\n };\n}\n\n/**\n * Returns true if the union is `true | false` (boolean shorthand).\n */\nfunction isBooleanUnion(type: UnionTypeNode): boolean {\n if (type.members.length !== 2) return false;\n const kinds = type.members.map((m) => m.kind);\n // Both must be primitives; check if both are \"boolean\" primitives.\n // The IR currently does not have a boolean literal node, so boolean union\n // is represented as two primitive boolean members.\n return (\n kinds.every((k) => k === \"primitive\") &&\n type.members.every((m) => m.kind === \"primitive\" && m.primitiveKind === \"boolean\")\n );\n}\n\n/**\n * Generates JSON Schema for a reference type.\n *\n * The referenced type's schema is stored in `$defs` (seeded from the type\n * registry before traversal begins). The reference simply emits a `$ref`.\n */\nfunction generateReferenceType(type: ReferenceTypeNode): JsonSchema2020 {\n return { $ref: `#/$defs/${type.name}` };\n}\n\n/**\n * Generates JSON Schema for a dynamic type (runtime-resolved enum or schema).\n *\n * Dynamic enums emit `x-formspec-source` and optionally `x-formspec-params`.\n * Dynamic schemas emit `x-formspec-schemaSource` with `additionalProperties: true`\n * since the actual schema is determined at runtime (per the JSON Schema vocabulary spec §3.2).\n */\nfunction generateDynamicType(type: DynamicTypeNode): JsonSchema2020 {\n if (type.dynamicKind === \"enum\") {\n const schema: JsonSchema2020 = {\n type: \"string\",\n \"x-formspec-source\": type.sourceKey,\n };\n if (type.parameterFields.length > 0) {\n schema[\"x-formspec-params\"] = [...type.parameterFields];\n }\n return schema;\n }\n\n // dynamicKind === \"schema\"\n return {\n type: \"object\",\n additionalProperties: true,\n \"x-formspec-schemaSource\": type.sourceKey,\n };\n}\n\n/**\n * CustomTypeNode is a placeholder for Phase 8 extensions.\n * Emits a minimal passthrough object type until the extension API is implemented.\n */\nfunction generateCustomType(_type: CustomTypeNode): JsonSchema2020 {\n return { type: \"object\" };\n}\n\n// =============================================================================\n// CONSTRAINT APPLICATION\n// =============================================================================\n\n/**\n * Applies constraint nodes onto an existing JSON Schema object (mutates in place).\n *\n * All callers pass freshly-created objects so there is no aliasing risk.\n *\n * Special rule (per the JSON Schema vocabulary spec §2.1): `multipleOf: 1` on a `\"number\"` type\n * promotes to `\"integer\"` and suppresses the `multipleOf` keyword (integer is a\n * subtype of number; expressing it via multipleOf:1 is redundant).\n *\n * Path-targeted constraints (e.g., `@minimum :value 0`) are emitted at the field\n * level here; full sub-field targeting via allOf composition is a Phase 4 concern.\n */\nfunction applyConstraints(schema: JsonSchema2020, constraints: readonly ConstraintNode[]): void {\n for (const constraint of constraints) {\n switch (constraint.constraintKind) {\n case \"minimum\":\n schema.minimum = constraint.value;\n break;\n\n case \"maximum\":\n schema.maximum = constraint.value;\n break;\n\n case \"exclusiveMinimum\":\n schema.exclusiveMinimum = constraint.value;\n break;\n\n case \"exclusiveMaximum\":\n schema.exclusiveMaximum = constraint.value;\n break;\n\n case \"multipleOf\": {\n const { value } = constraint;\n if (value === 1 && schema.type === \"number\") {\n // Promote number → integer; omit the multipleOf keyword (redundant).\n schema.type = \"integer\";\n } else {\n schema.multipleOf = value;\n }\n break;\n }\n\n case \"minLength\":\n schema.minLength = constraint.value;\n break;\n\n case \"maxLength\":\n schema.maxLength = constraint.value;\n break;\n\n case \"minItems\":\n schema.minItems = constraint.value;\n break;\n\n case \"maxItems\":\n schema.maxItems = constraint.value;\n break;\n\n case \"pattern\":\n schema.pattern = constraint.pattern;\n break;\n\n case \"uniqueItems\":\n schema.uniqueItems = constraint.value;\n break;\n\n case \"allowedMembers\":\n // EnumMemberConstraintNode — not yet emitted to JSON Schema (Phase 6 validation).\n break;\n\n case \"custom\":\n // CustomConstraintNode — handled by Phase 8 extensions.\n break;\n\n default: {\n // TypeScript exhaustiveness guard.\n const _exhaustive: never = constraint;\n void _exhaustive;\n }\n }\n }\n}\n\n// =============================================================================\n// ANNOTATION APPLICATION\n// =============================================================================\n\n/**\n * Applies annotation nodes onto an existing JSON Schema object (mutates in place).\n *\n * Mapping per the JSON Schema vocabulary spec §2.8:\n * - `displayName` → `title`\n * - `description` → `description`\n * - `defaultValue` → `default`\n * - `deprecated` → `deprecated: true` (2020-12 standard annotation)\n *\n * UI-only annotations (`placeholder`, `formatHint`) are silently ignored here —\n * they belong in the UI Schema, not the data schema.\n */\nfunction applyAnnotations(schema: JsonSchema2020, annotations: readonly AnnotationNode[]): void {\n for (const annotation of annotations) {\n switch (annotation.annotationKind) {\n case \"displayName\":\n schema.title = annotation.value;\n break;\n\n case \"description\":\n schema.description = annotation.value;\n break;\n\n case \"defaultValue\":\n schema.default = annotation.value;\n break;\n\n case \"deprecated\":\n schema.deprecated = true;\n break;\n\n case \"placeholder\":\n // UI-only — belongs in UI Schema, not emitted here.\n break;\n\n case \"formatHint\":\n // UI-only — belongs in UI Schema, not emitted here.\n break;\n\n case \"custom\":\n // CustomAnnotationNode — handled by Phase 8 extensions.\n break;\n\n default: {\n // TypeScript exhaustiveness guard.\n const _exhaustive: never = annotation;\n void _exhaustive;\n }\n }\n }\n}\n","/**\n * JSON Schema generator for FormSpec forms.\n *\n * Routes through the canonical IR pipeline: Chain DSL → FormIR → JSON Schema 2020-12.\n */\n\nimport type { FormElement, FormSpec } from \"@formspec/core\";\nimport { canonicalizeChainDSL } from \"../canonicalize/index.js\";\nimport { generateJsonSchemaFromIR, type JsonSchema2020 } from \"./ir-generator.js\";\n\n/**\n * Generates a JSON Schema 2020-12 from a FormSpec.\n *\n * All generation routes through the canonical IR. The chain DSL is first\n * canonicalized to a FormIR, then the IR-based generator produces the schema.\n *\n * @example\n * ```typescript\n * const form = formspec(\n * field.text(\"name\", { label: \"Name\", required: true }),\n * field.number(\"age\", { min: 0 }),\n * );\n *\n * const schema = generateJsonSchema(form);\n * // {\n * // $schema: \"https://json-schema.org/draft/2020-12/schema\",\n * // type: \"object\",\n * // properties: {\n * // name: { type: \"string\", title: \"Name\" },\n * // age: { type: \"number\", minimum: 0 }\n * // },\n * // required: [\"name\"]\n * // }\n * ```\n *\n * @param form - The FormSpec to convert\n * @returns A JSON Schema 2020-12 object\n */\nexport function generateJsonSchema<E extends readonly FormElement[]>(\n form: FormSpec<E>\n): JsonSchema2020 {\n const ir = canonicalizeChainDSL(form);\n return generateJsonSchemaFromIR(ir);\n}\n","/**\n * Zod schemas for JSON Forms UI Schema.\n *\n * These schemas are the source of truth for UI Schema validation.\n * TypeScript types are derived from these schemas via `z.infer<>`.\n *\n * @see https://jsonforms.io/docs/uischema/\n */\n\nimport { z } from \"zod\";\n\n// =============================================================================\n// Primitive helpers\n// =============================================================================\n\n/** JSON Pointer string (e.g., \"#/properties/fieldName\") */\nconst jsonPointerSchema = z.string();\n\n// =============================================================================\n// Rule Effect and Element Type enums\n// =============================================================================\n\n/**\n * Zod schema for rule effect values.\n */\nexport const ruleEffectSchema = z.enum([\"SHOW\", \"HIDE\", \"ENABLE\", \"DISABLE\"]);\n\n/**\n * Rule effect types for conditional visibility.\n */\nexport type RuleEffect = z.infer<typeof ruleEffectSchema>;\n\n/**\n * Zod schema for UI Schema element type strings.\n */\nexport const uiSchemaElementTypeSchema = z.enum([\n \"Control\",\n \"VerticalLayout\",\n \"HorizontalLayout\",\n \"Group\",\n \"Categorization\",\n \"Category\",\n \"Label\",\n]);\n\n/**\n * UI Schema element types.\n */\nexport type UISchemaElementType = z.infer<typeof uiSchemaElementTypeSchema>;\n\n// =============================================================================\n// Rule Condition Schema (recursive)\n// =============================================================================\n\n// Forward-declare the recursive TypeScript type.\n// We use an interface here (rather than z.infer<>) because the recursive\n// z.lazy() type annotation requires us to pre-declare the shape.\n/**\n * JSON Schema subset used in rule conditions.\n */\nexport interface RuleConditionSchema {\n const?: unknown;\n enum?: readonly unknown[];\n type?: string;\n not?: RuleConditionSchema;\n minimum?: number;\n maximum?: number;\n exclusiveMinimum?: number;\n exclusiveMaximum?: number;\n minLength?: number;\n properties?: Record<string, RuleConditionSchema>;\n required?: string[];\n allOf?: RuleConditionSchema[];\n}\n\n// Build the Zod schema referencing the pre-declared interface.\n// We use z.ZodType<RuleConditionSchema> so the recursive reference works.\n// The interface uses `?` (exact optional), and z.ZodType checks output only,\n// so the optional fields (which Zod infers as `T | undefined`) are compatible\n// because `T | undefined` is assignable to the optional field slot.\n//\n// @ts-expect-error -- exactOptionalPropertyTypes: the Zod output type for optional\n// fields is `T | undefined`, but our interface uses `?` (exact optional, key may\n// be absent). This is a known mismatch when using z.ZodType<T> with\n// exactOptionalPropertyTypes:true; the runtime behavior is correct.\nexport const ruleConditionSchema: z.ZodType<RuleConditionSchema> = z.lazy(() =>\n z\n .object({\n const: z.unknown().optional(),\n enum: z.array(z.unknown()).readonly().optional(),\n type: z.string().optional(),\n not: ruleConditionSchema.optional(),\n minimum: z.number().optional(),\n maximum: z.number().optional(),\n exclusiveMinimum: z.number().optional(),\n exclusiveMaximum: z.number().optional(),\n minLength: z.number().optional(),\n properties: z.record(z.string(), ruleConditionSchema).optional(),\n required: z.array(z.string()).optional(),\n allOf: z.array(ruleConditionSchema).optional(),\n })\n .strict()\n);\n\n// =============================================================================\n// Schema-Based Condition and Rule\n// =============================================================================\n\n/**\n * Zod schema for a schema-based rule condition.\n */\nexport const schemaBasedConditionSchema = z\n .object({\n scope: jsonPointerSchema,\n schema: ruleConditionSchema,\n })\n .strict();\n\n/**\n * Condition for a rule.\n */\nexport type SchemaBasedCondition = z.infer<typeof schemaBasedConditionSchema>;\n\n/**\n * Zod schema for a UI Schema rule.\n */\nexport const ruleSchema = z\n .object({\n effect: ruleEffectSchema,\n condition: schemaBasedConditionSchema,\n })\n .strict();\n\n/**\n * Rule for conditional element visibility/enablement.\n */\nexport type Rule = z.infer<typeof ruleSchema>;\n\n// =============================================================================\n// UI Schema Element Schemas (recursive via z.lazy)\n// =============================================================================\n\n// Forward-declare UISchemaElement so layout schemas can reference it.\n// We declare the type up-front and wire the Zod schema below.\n/**\n * Union of all UI Schema element types.\n */\nexport type UISchemaElement =\n | ControlElement\n | VerticalLayout\n | HorizontalLayout\n | GroupLayout\n | Categorization\n | Category\n | LabelElement;\n\n// The Zod schema for UISchemaElement is defined as a const using z.lazy(),\n// which defers evaluation until first use. This allows all element schemas\n// below to be referenced even though they are declared after this line.\nexport const uiSchemaElementSchema: z.ZodType<UISchemaElement> = z.lazy(() =>\n z.union([\n controlSchema,\n verticalLayoutSchema,\n horizontalLayoutSchema,\n groupLayoutSchema,\n categorizationSchema,\n categorySchema,\n labelElementSchema,\n ])\n) as z.ZodType<UISchemaElement>;\n\n// -----------------------------------------------------------------------------\n// Control\n// -----------------------------------------------------------------------------\n\n/**\n * Zod schema for a Control element.\n */\nexport const controlSchema = z\n .object({\n type: z.literal(\"Control\"),\n scope: jsonPointerSchema,\n label: z.union([z.string(), z.literal(false)]).optional(),\n rule: ruleSchema.optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n })\n .passthrough();\n\n/**\n * A Control element that binds to a JSON Schema property.\n */\nexport type ControlElement = z.infer<typeof controlSchema>;\n\n// -----------------------------------------------------------------------------\n// VerticalLayout\n// -----------------------------------------------------------------------------\n\n// Pre-declare the interface so the Zod schema can reference UISchemaElement.\n/**\n * A vertical layout element.\n */\nexport interface VerticalLayout {\n type: \"VerticalLayout\";\n elements: UISchemaElement[];\n rule?: Rule | undefined;\n options?: Record<string, unknown> | undefined;\n [k: string]: unknown;\n}\n\nexport const verticalLayoutSchema: z.ZodType<VerticalLayout> = z.lazy(() =>\n z\n .object({\n type: z.literal(\"VerticalLayout\"),\n elements: z.array(uiSchemaElementSchema),\n rule: ruleSchema.optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n })\n .passthrough()\n);\n\n// -----------------------------------------------------------------------------\n// HorizontalLayout\n// -----------------------------------------------------------------------------\n\n/**\n * A horizontal layout element.\n */\nexport interface HorizontalLayout {\n type: \"HorizontalLayout\";\n elements: UISchemaElement[];\n rule?: Rule | undefined;\n options?: Record<string, unknown> | undefined;\n [k: string]: unknown;\n}\n\nexport const horizontalLayoutSchema: z.ZodType<HorizontalLayout> = z.lazy(() =>\n z\n .object({\n type: z.literal(\"HorizontalLayout\"),\n elements: z.array(uiSchemaElementSchema),\n rule: ruleSchema.optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n })\n .passthrough()\n);\n\n// -----------------------------------------------------------------------------\n// GroupLayout\n// -----------------------------------------------------------------------------\n\n/**\n * A group element with a label.\n */\nexport interface GroupLayout {\n type: \"Group\";\n label: string;\n elements: UISchemaElement[];\n rule?: Rule | undefined;\n options?: Record<string, unknown> | undefined;\n [k: string]: unknown;\n}\n\nexport const groupLayoutSchema: z.ZodType<GroupLayout> = z.lazy(() =>\n z\n .object({\n type: z.literal(\"Group\"),\n label: z.string(),\n elements: z.array(uiSchemaElementSchema),\n rule: ruleSchema.optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n })\n .passthrough()\n);\n\n// -----------------------------------------------------------------------------\n// Category\n// -----------------------------------------------------------------------------\n\n/**\n * A Category element, used inside a Categorization layout.\n */\nexport interface Category {\n type: \"Category\";\n label: string;\n elements: UISchemaElement[];\n rule?: Rule | undefined;\n options?: Record<string, unknown> | undefined;\n [k: string]: unknown;\n}\n\nexport const categorySchema: z.ZodType<Category> = z.lazy(() =>\n z\n .object({\n type: z.literal(\"Category\"),\n label: z.string(),\n elements: z.array(uiSchemaElementSchema),\n rule: ruleSchema.optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n })\n .passthrough()\n);\n\n// -----------------------------------------------------------------------------\n// Categorization\n// -----------------------------------------------------------------------------\n\n/**\n * A Categorization element (tab-based layout).\n */\nexport interface Categorization {\n type: \"Categorization\";\n elements: Category[];\n label?: string | undefined;\n rule?: Rule | undefined;\n options?: Record<string, unknown> | undefined;\n [k: string]: unknown;\n}\n\nexport const categorizationSchema: z.ZodType<Categorization> = z.lazy(() =>\n z\n .object({\n type: z.literal(\"Categorization\"),\n elements: z.array(categorySchema),\n label: z.string().optional(),\n rule: ruleSchema.optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n })\n .passthrough()\n);\n\n// -----------------------------------------------------------------------------\n// LabelElement\n// -----------------------------------------------------------------------------\n\n/**\n * Zod schema for a Label element.\n */\nexport const labelElementSchema = z\n .object({\n type: z.literal(\"Label\"),\n text: z.string(),\n rule: ruleSchema.optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n })\n .passthrough();\n\n/**\n * A Label element for displaying static text.\n */\nexport type LabelElement = z.infer<typeof labelElementSchema>;\n\n// =============================================================================\n// Root UISchema\n// =============================================================================\n\n/**\n * Root UI Schema (always a layout — not a Control, Category, or Label).\n */\nexport type UISchema = VerticalLayout | HorizontalLayout | GroupLayout | Categorization;\n\n/**\n * Zod schema for the root UI Schema (layout types only).\n */\nexport const uiSchema: z.ZodType<UISchema> = z.lazy(() =>\n z.union([verticalLayoutSchema, horizontalLayoutSchema, groupLayoutSchema, categorizationSchema])\n) as z.ZodType<UISchema>;\n","/**\n * JSON Forms UI Schema generator that operates on the canonical FormIR.\n *\n * This generator consumes the IR produced by the Canonicalize phase and\n * produces a JSON Forms UI Schema. All downstream UI Schema generation\n * should use this module for UI Schema generation.\n */\n\nimport type { FormIR, FormIRElement, FieldNode, GroupLayoutNode } from \"@formspec/core\";\nimport type { UISchema, UISchemaElement, ControlElement, GroupLayout, Rule } from \"./types.js\";\nimport { uiSchema as uiSchemaValidator } from \"./schema.js\";\nimport { z } from \"zod\";\n\n// =============================================================================\n// HELPERS\n// =============================================================================\n\n/**\n * Parses a value through a Zod schema, converting validation errors to a\n * descriptive Error.\n */\nfunction parseOrThrow<T>(schema: z.ZodType<T>, value: unknown, label: string): T {\n try {\n return schema.parse(value);\n } catch (error) {\n if (error instanceof z.ZodError) {\n throw new Error(\n `Generated ${label} failed validation:\\n${error.issues.map((i) => ` ${i.path.join(\".\")}: ${i.message}`).join(\"\\n\")}`\n );\n }\n throw error;\n }\n}\n\n/**\n * Converts a field name to a JSON Pointer scope string.\n */\nfunction fieldToScope(fieldName: string): string {\n return `#/properties/${fieldName}`;\n}\n\n/**\n * Creates a SHOW rule for a single conditional field/value pair.\n */\nfunction createShowRule(fieldName: string, value: unknown): Rule {\n return {\n effect: \"SHOW\",\n condition: {\n scope: fieldToScope(fieldName),\n schema: { const: value },\n },\n };\n}\n\n/**\n * Combines two SHOW rules into a single rule using an allOf condition.\n *\n * When elements are nested inside multiple conditionals, all parent conditions\n * must be met for the element to be visible. This function merges the two\n * conditions into a single rule using allOf so that JSON Forms evaluates\n * both predicates simultaneously.\n */\nfunction combineRules(parentRule: Rule, childRule: Rule): Rule {\n const parentCondition = parentRule.condition;\n const childCondition = childRule.condition;\n\n return {\n effect: \"SHOW\",\n condition: {\n scope: \"#\",\n schema: {\n allOf: [\n {\n properties: {\n [parentCondition.scope.replace(\"#/properties/\", \"\")]: parentCondition.schema,\n },\n },\n {\n properties: {\n [childCondition.scope.replace(\"#/properties/\", \"\")]: childCondition.schema,\n },\n },\n ],\n },\n },\n };\n}\n\n// =============================================================================\n// ELEMENT CONVERSION\n// =============================================================================\n\n/**\n * Converts a FieldNode from the IR to a ControlElement.\n *\n * The label is sourced from the first `displayName` annotation on the field,\n * matching how the chain DSL propagates the `label` option through the\n * canonicalization phase.\n */\nfunction fieldNodeToControl(field: FieldNode, parentRule?: Rule): ControlElement {\n const displayNameAnnotation = field.annotations.find((a) => a.annotationKind === \"displayName\");\n\n const control: ControlElement = {\n type: \"Control\",\n scope: fieldToScope(field.name),\n ...(displayNameAnnotation !== undefined && { label: displayNameAnnotation.value }),\n ...(parentRule !== undefined && { rule: parentRule }),\n };\n\n return control;\n}\n\n/**\n * Converts a GroupLayoutNode from the IR to a GroupLayout element.\n *\n * The group's children are recursively converted; the optional parent rule is\n * forwarded to nested elements so that a group inside a conditional inherits\n * the visibility rule.\n */\nfunction groupNodeToLayout(group: GroupLayoutNode, parentRule?: Rule): GroupLayout {\n return {\n type: \"Group\",\n label: group.label,\n elements: irElementsToUiSchema(group.elements, parentRule),\n ...(parentRule !== undefined && { rule: parentRule }),\n };\n}\n\n/**\n * Converts an array of IR elements to UI Schema elements.\n *\n * @param elements - The IR elements to convert\n * @param parentRule - Optional rule inherited from a parent ConditionalLayoutNode\n * @returns Array of UI Schema elements\n */\nfunction irElementsToUiSchema(\n elements: readonly FormIRElement[],\n parentRule?: Rule\n): UISchemaElement[] {\n const result: UISchemaElement[] = [];\n\n for (const element of elements) {\n switch (element.kind) {\n case \"field\": {\n result.push(fieldNodeToControl(element, parentRule));\n break;\n }\n\n case \"group\": {\n result.push(groupNodeToLayout(element, parentRule));\n break;\n }\n\n case \"conditional\": {\n // Build the rule for this conditional level.\n const newRule = createShowRule(element.fieldName, element.value);\n // Combine with the inherited parent rule for nested conditionals.\n const combinedRule = parentRule !== undefined ? combineRules(parentRule, newRule) : newRule;\n // Children are flattened into the parent container with the combined\n // rule attached.\n const childElements = irElementsToUiSchema(element.elements, combinedRule);\n result.push(...childElements);\n break;\n }\n\n default: {\n const _exhaustive: never = element;\n void _exhaustive;\n throw new Error(\"Unhandled IR element kind\");\n }\n }\n }\n\n return result;\n}\n\n// =============================================================================\n// PUBLIC API\n// =============================================================================\n\n/**\n * Generates a JSON Forms UI Schema from a canonical `FormIR`.\n *\n * Mapping rules:\n * - `FieldNode` → `ControlElement` with `scope: \"#/properties/<name>\"`\n * - `displayName` annotation → `label` on the `ControlElement`\n * - `GroupLayoutNode` → `GroupLayout` with recursively converted `elements`\n * - `ConditionalLayoutNode` → children flattened with a `SHOW` rule\n * - Nested conditionals → combined `allOf` rule\n * - Root wrapper is always `{ type: \"VerticalLayout\", elements: [...] }`\n *\n * @example\n * ```typescript\n * const ir = canonicalizeDSL(\n * formspec(\n * group(\"Customer\", field.text(\"name\", { label: \"Name\" })),\n * when(is(\"status\", \"draft\"), field.text(\"notes\", { label: \"Notes\" })),\n * )\n * );\n *\n * const uiSchema = generateUiSchemaFromIR(ir);\n * // {\n * // type: \"VerticalLayout\",\n * // elements: [\n * // {\n * // type: \"Group\",\n * // label: \"Customer\",\n * // elements: [{ type: \"Control\", scope: \"#/properties/name\", label: \"Name\" }]\n * // },\n * // {\n * // type: \"Control\",\n * // scope: \"#/properties/notes\",\n * // label: \"Notes\",\n * // rule: { effect: \"SHOW\", condition: { scope: \"#/properties/status\", schema: { const: \"draft\" } } }\n * // }\n * // ]\n * // }\n * ```\n *\n * @param ir - The canonical FormIR produced by the Canonicalize phase\n * @returns A validated JSON Forms UI Schema\n */\nexport function generateUiSchemaFromIR(ir: FormIR): UISchema {\n const result: UISchema = {\n type: \"VerticalLayout\",\n elements: irElementsToUiSchema(ir.elements),\n };\n\n return parseOrThrow(uiSchemaValidator, result, \"UI Schema\");\n}\n","/**\n * JSON Forms UI Schema generator for FormSpec forms.\n *\n * Routes through the canonical IR pipeline: Chain DSL → FormIR → UI Schema.\n */\n\nimport type { FormElement, FormSpec } from \"@formspec/core\";\nimport { canonicalizeChainDSL } from \"../canonicalize/index.js\";\nimport { generateUiSchemaFromIR } from \"./ir-generator.js\";\nimport type { UISchema } from \"./types.js\";\n\n/**\n * Generates a JSON Forms UI Schema from a FormSpec.\n *\n * All generation routes through the canonical IR. The chain DSL is first\n * canonicalized to a FormIR, then the IR-based generator produces the schema.\n *\n * @example\n * ```typescript\n * const form = formspec(\n * group(\"Customer\",\n * field.text(\"name\", { label: \"Name\" }),\n * ),\n * when(\"status\", \"draft\",\n * field.text(\"notes\", { label: \"Notes\" }),\n * ),\n * );\n *\n * const uiSchema = generateUiSchema(form);\n * // {\n * // type: \"VerticalLayout\",\n * // elements: [\n * // {\n * // type: \"Group\",\n * // label: \"Customer\",\n * // elements: [\n * // { type: \"Control\", scope: \"#/properties/name\", label: \"Name\" }\n * // ]\n * // },\n * // {\n * // type: \"Control\",\n * // scope: \"#/properties/notes\",\n * // label: \"Notes\",\n * // rule: {\n * // effect: \"SHOW\",\n * // condition: { scope: \"#/properties/status\", schema: { const: \"draft\" } }\n * // }\n * // }\n * // ]\n * // }\n * ```\n *\n * @param form - The FormSpec to convert\n * @returns A JSON Forms UI Schema\n */\nexport function generateUiSchema<E extends readonly FormElement[]>(form: FormSpec<E>): UISchema {\n const ir = canonicalizeChainDSL(form);\n return generateUiSchemaFromIR(ir);\n}\n","/**\n * JSON Schema type definitions.\n *\n * These types are a subset of JSON Schema sufficient for form generation.\n */\n\n/**\n * JSON Schema primitive types.\n */\nexport type JSONSchemaType =\n | \"string\"\n | \"number\"\n | \"integer\"\n | \"boolean\"\n | \"object\"\n | \"array\"\n | \"null\";\n\n/**\n * A JSON Schema definition (legacy subset used by Zod validator and types.ts).\n */\nexport interface JSONSchema7 {\n $schema?: string;\n $id?: string;\n $ref?: string;\n\n // Metadata\n title?: string;\n description?: string;\n deprecated?: boolean;\n\n // Type\n type?: JSONSchemaType | JSONSchemaType[];\n\n // String validation\n minLength?: number;\n maxLength?: number;\n pattern?: string;\n\n // Number validation\n minimum?: number;\n maximum?: number;\n exclusiveMinimum?: number;\n exclusiveMaximum?: number;\n\n // Enum\n enum?: readonly (string | number | boolean | null)[];\n const?: string | number | boolean | null;\n\n // Object\n properties?: Record<string, JSONSchema7>;\n required?: string[];\n additionalProperties?: boolean | JSONSchema7;\n\n // Array\n items?: JSONSchema7 | JSONSchema7[];\n minItems?: number;\n maxItems?: number;\n\n // Composition\n allOf?: JSONSchema7[];\n anyOf?: JSONSchema7[];\n oneOf?: JSONSchema7[];\n not?: JSONSchema7;\n\n // Conditional\n if?: JSONSchema7;\n then?: JSONSchema7;\n else?: JSONSchema7;\n\n // Format\n format?: string;\n\n // Default\n default?: unknown;\n\n // =============================================================================\n // FormSpec Extensions (x- prefixed)\n // =============================================================================\n\n /**\n * Data source key for dynamic enum fields.\n * Indicates that options should be fetched from a registered resolver.\n */\n \"x-formspec-source\"?: string;\n\n /**\n * Field names whose values are needed to fetch dynamic enum options.\n * Used for dependent/cascading dropdowns.\n */\n \"x-formspec-params\"?: readonly string[];\n\n /**\n * Schema source identifier for dynamic schema fields.\n * Indicates that the schema should be loaded dynamically at runtime.\n */\n \"x-formspec-schemaSource\"?: string;\n}\n\n/** Extension properties for custom FormSpec decorators. */\nexport type FormSpecSchemaExtensions = Record<`x-formspec-${string}`, unknown>;\n\n/** JSON Schema with FormSpec extension properties for arbitrary x-formspec-* keys. */\nexport type ExtendedJSONSchema7 = JSONSchema7 & FormSpecSchemaExtensions;\n\n/**\n * Sets a FormSpec extension property on a JSON Schema node.\n *\n * Use this to safely add `x-formspec-*` properties to any schema,\n * including nested schemas typed as `JSONSchema7` (which don't carry\n * the extension index signature).\n *\n * @param schema - Any JSON Schema node\n * @param key - Extension key (must start with `x-formspec-`)\n * @param value - Extension value\n */\nexport function setSchemaExtension(\n schema: object,\n key: `x-formspec-${string}`,\n value: unknown\n): void {\n (schema as Record<string, unknown>)[key] = value;\n}\n\n/**\n * Reads a FormSpec extension property from a JSON Schema node.\n *\n * Accepts any schema object — `JSONSchema7`, `JsonSchema2020`, `ExtendedJSONSchema7`, etc.\n *\n * @param schema - Any JSON Schema node\n * @param key - Extension key (must start with `x-formspec-`)\n * @returns The extension value, or `undefined` if not present\n */\nexport function getSchemaExtension(schema: object, key: `x-formspec-${string}`): unknown {\n return (schema as Record<string, unknown>)[key];\n}\n","/**\n * Zod schemas for JSON Schema output validation.\n *\n * These schemas cover the subset of JSON Schema that FormSpec generates,\n * plus the FormSpec-specific `x-formspec-*` extension properties.\n *\n * @see https://json-schema.org/draft/2020-12/schema\n */\n\nimport { z } from \"zod\";\nimport type { JSONSchema7 } from \"./types.js\";\n\n// =============================================================================\n// JSON Schema type enum\n// =============================================================================\n\n/**\n * Zod schema for JSON Schema primitive type strings.\n */\nexport const jsonSchemaTypeSchema = z.enum([\n \"string\",\n \"number\",\n \"integer\",\n \"boolean\",\n \"object\",\n \"array\",\n \"null\",\n]);\n\n// =============================================================================\n// JSON Schema validator schema (recursive)\n// =============================================================================\n\n// We annotate with z.ZodType<JSONSchema7> for the recursive self-reference.\n// The @ts-expect-error is required because exactOptionalPropertyTypes:true causes\n// Zod's inferred output type for optional fields (`T | undefined`) to be\n// incompatible with the JSONSchema7 interface's exact optional fields (`T?`).\n// The runtime behavior is correct: z.optional() will strip `undefined` values\n// during parsing and correctly handle absent keys.\n//\n// @ts-expect-error -- exactOptionalPropertyTypes: Zod optional infers `T | undefined`\n// but JSONSchema7 uses exact optional `?:` which disallows explicit undefined.\nexport const jsonSchema7Schema: z.ZodType<JSONSchema7> = z.lazy(() =>\n z\n .object({\n $schema: z.string().optional(),\n $id: z.string().optional(),\n $ref: z.string().optional(),\n\n // Metadata\n title: z.string().optional(),\n description: z.string().optional(),\n deprecated: z.boolean().optional(),\n\n // Type\n type: z.union([jsonSchemaTypeSchema, z.array(jsonSchemaTypeSchema)]).optional(),\n\n // String validation\n minLength: z.number().optional(),\n maxLength: z.number().optional(),\n pattern: z.string().optional(),\n\n // Number validation\n minimum: z.number().optional(),\n maximum: z.number().optional(),\n exclusiveMinimum: z.number().optional(),\n exclusiveMaximum: z.number().optional(),\n\n // Enum\n enum: z\n .array(z.union([z.string(), z.number(), z.boolean(), z.null()]))\n .readonly()\n .optional(),\n const: z.union([z.string(), z.number(), z.boolean(), z.null()]).optional(),\n\n // Object\n properties: z.record(z.string(), jsonSchema7Schema).optional(),\n required: z.array(z.string()).optional(),\n additionalProperties: z.union([z.boolean(), jsonSchema7Schema]).optional(),\n\n // Array\n items: z.union([jsonSchema7Schema, z.array(jsonSchema7Schema)]).optional(),\n minItems: z.number().optional(),\n maxItems: z.number().optional(),\n\n // Composition\n allOf: z.array(jsonSchema7Schema).optional(),\n anyOf: z.array(jsonSchema7Schema).optional(),\n oneOf: z.array(jsonSchema7Schema).optional(),\n not: jsonSchema7Schema.optional(),\n\n // Conditional\n if: jsonSchema7Schema.optional(),\n then: jsonSchema7Schema.optional(),\n else: jsonSchema7Schema.optional(),\n\n // Format\n format: z.string().optional(),\n\n // Default\n default: z.unknown().optional(),\n\n // FormSpec extensions\n \"x-formspec-source\": z.string().optional(),\n \"x-formspec-params\": z.array(z.string()).readonly().optional(),\n \"x-formspec-schemaSource\": z.string().optional(),\n })\n // passthrough preserves arbitrary x-formspec-* extension properties\n // added by custom decorators without causing validation failures\n .passthrough()\n);\n","/**\n * Constraint validator for the FormSpec IR.\n *\n * Performs the Validate pipeline phase:\n * - Contradiction detection between paired constraints\n * - Type applicability checks (e.g. numeric constraints on string fields)\n * - Custom constraint type applicability (when extension registry is provided)\n * - Unknown extension warnings (when a registry is provided)\n *\n * @packageDocumentation\n */\n\nimport type {\n FormIR,\n FormIRElement,\n FieldNode,\n TypeNode,\n ConstraintNode,\n NumericConstraintNode,\n LengthConstraintNode,\n EnumMemberConstraintNode,\n Provenance,\n ObjectProperty,\n} from \"@formspec/core\";\nimport type { ExtensionRegistry } from \"../extensions/index.js\";\n\n// =============================================================================\n// PUBLIC API TYPES\n// =============================================================================\n\n/**\n * A structured diagnostic produced by constraint validation.\n *\n * The `code` follows the format: `{VENDOR}-{CATEGORY}-{NNN}`.\n * - VENDOR defaults to \"FORMSPEC\" (configurable via `vendorPrefix`).\n * - Categories: CONTRADICTION, TYPE_MISMATCH, UNKNOWN_EXTENSION\n */\nexport interface ValidationDiagnostic {\n readonly code: string;\n readonly message: string;\n readonly severity: \"error\" | \"warning\";\n /** Location of the primary constraint involved in the violation. */\n readonly primaryLocation: Provenance;\n /** Related locations (e.g., the other side of a contradiction pair). */\n readonly relatedLocations: readonly Provenance[];\n}\n\n/** Result of validating a {@link FormIR}. */\nexport interface ValidationResult {\n readonly diagnostics: readonly ValidationDiagnostic[];\n /** `true` if there are no error-severity diagnostics (warnings are OK). */\n readonly valid: boolean;\n}\n\n/** Options for constraint validation. */\nexport interface ValidateIROptions {\n /**\n * Vendor prefix used when constructing diagnostic codes.\n * @defaultValue \"FORMSPEC\"\n */\n readonly vendorPrefix?: string;\n /**\n * Extension registry for resolving custom constraint type applicability.\n * When provided, custom constraints with `applicableTypes` will be\n * validated against the field's type node kind. Custom constraints\n * whose IDs are absent from this registry emit a WARNING (UNKNOWN_EXTENSION).\n * When omitted, custom constraints are silently skipped.\n */\n readonly extensionRegistry?: ExtensionRegistry;\n}\n\n\n// =============================================================================\n// CONTEXT\n// =============================================================================\n\n/** Mutable accumulator threaded through the validation walk. */\ninterface ValidationContext {\n readonly diagnostics: ValidationDiagnostic[];\n readonly vendorPrefix: string;\n readonly extensionRegistry: ExtensionRegistry | undefined;\n}\n\n// =============================================================================\n// DIAGNOSTIC FACTORIES\n// =============================================================================\n\ntype DiagnosticCategory = \"CONTRADICTION\" | \"TYPE_MISMATCH\" | \"UNKNOWN_EXTENSION\";\n\nfunction makeCode(ctx: ValidationContext, category: DiagnosticCategory, number: number): string {\n return `${ctx.vendorPrefix}-${category}-${String(number).padStart(3, \"0\")}`;\n}\n\nfunction addContradiction(\n ctx: ValidationContext,\n message: string,\n primary: Provenance,\n related: Provenance\n): void {\n ctx.diagnostics.push({\n code: makeCode(ctx, \"CONTRADICTION\", 1),\n message,\n severity: \"error\",\n primaryLocation: primary,\n relatedLocations: [related],\n });\n}\n\nfunction addTypeMismatch(\n ctx: ValidationContext,\n message: string,\n primary: Provenance\n): void {\n ctx.diagnostics.push({\n code: makeCode(ctx, \"TYPE_MISMATCH\", 1),\n message,\n severity: \"error\",\n primaryLocation: primary,\n relatedLocations: [],\n });\n}\n\nfunction addUnknownExtension(\n ctx: ValidationContext,\n message: string,\n primary: Provenance\n): void {\n ctx.diagnostics.push({\n code: makeCode(ctx, \"UNKNOWN_EXTENSION\", 1),\n message,\n severity: \"warning\",\n primaryLocation: primary,\n relatedLocations: [],\n });\n}\n\n// =============================================================================\n// CONSTRAINT NARROWING HELPERS\n// =============================================================================\n\n/** Extract the first numeric constraint with the given kind, if present. */\nfunction findNumeric(\n constraints: readonly ConstraintNode[],\n constraintKind: NumericConstraintNode[\"constraintKind\"]\n): NumericConstraintNode | undefined {\n return constraints.find(\n (c): c is NumericConstraintNode => c.constraintKind === constraintKind\n );\n}\n\n/** Extract the first length constraint with the given kind, if present. */\nfunction findLength(\n constraints: readonly ConstraintNode[],\n constraintKind: LengthConstraintNode[\"constraintKind\"]\n): LengthConstraintNode | undefined {\n return constraints.find(\n (c): c is LengthConstraintNode => c.constraintKind === constraintKind\n );\n}\n\n/** Extract all allowedMembers constraints. */\nfunction findAllowedMembers(\n constraints: readonly ConstraintNode[]\n): readonly EnumMemberConstraintNode[] {\n return constraints.filter(\n (c): c is EnumMemberConstraintNode => c.constraintKind === \"allowedMembers\"\n );\n}\n\n// =============================================================================\n// CONTRADICTION DETECTION\n// =============================================================================\n\nfunction checkNumericContradictions(\n ctx: ValidationContext,\n fieldName: string,\n constraints: readonly ConstraintNode[]\n): void {\n const min = findNumeric(constraints, \"minimum\");\n const max = findNumeric(constraints, \"maximum\");\n const exMin = findNumeric(constraints, \"exclusiveMinimum\");\n const exMax = findNumeric(constraints, \"exclusiveMaximum\");\n\n // minimum > maximum\n if (min !== undefined && max !== undefined && min.value > max.value) {\n addContradiction(\n ctx,\n `Field \"${fieldName}\": minimum (${String(min.value)}) is greater than maximum (${String(max.value)})`,\n min.provenance,\n max.provenance\n );\n }\n\n // exclusiveMinimum >= maximum\n if (exMin !== undefined && max !== undefined && exMin.value >= max.value) {\n addContradiction(\n ctx,\n `Field \"${fieldName}\": exclusiveMinimum (${String(exMin.value)}) is greater than or equal to maximum (${String(max.value)})`,\n exMin.provenance,\n max.provenance\n );\n }\n\n // minimum >= exclusiveMaximum\n if (min !== undefined && exMax !== undefined && min.value >= exMax.value) {\n addContradiction(\n ctx,\n `Field \"${fieldName}\": minimum (${String(min.value)}) is greater than or equal to exclusiveMaximum (${String(exMax.value)})`,\n min.provenance,\n exMax.provenance\n );\n }\n\n // exclusiveMinimum >= exclusiveMaximum\n if (exMin !== undefined && exMax !== undefined && exMin.value >= exMax.value) {\n addContradiction(\n ctx,\n `Field \"${fieldName}\": exclusiveMinimum (${String(exMin.value)}) is greater than or equal to exclusiveMaximum (${String(exMax.value)})`,\n exMin.provenance,\n exMax.provenance\n );\n }\n}\n\nfunction checkLengthContradictions(\n ctx: ValidationContext,\n fieldName: string,\n constraints: readonly ConstraintNode[]\n): void {\n const minLen = findLength(constraints, \"minLength\");\n const maxLen = findLength(constraints, \"maxLength\");\n\n if (minLen !== undefined && maxLen !== undefined && minLen.value > maxLen.value) {\n addContradiction(\n ctx,\n `Field \"${fieldName}\": minLength (${String(minLen.value)}) is greater than maxLength (${String(maxLen.value)})`,\n minLen.provenance,\n maxLen.provenance\n );\n }\n\n const minItems = findLength(constraints, \"minItems\");\n const maxItems = findLength(constraints, \"maxItems\");\n\n if (minItems !== undefined && maxItems !== undefined && minItems.value > maxItems.value) {\n addContradiction(\n ctx,\n `Field \"${fieldName}\": minItems (${String(minItems.value)}) is greater than maxItems (${String(maxItems.value)})`,\n minItems.provenance,\n maxItems.provenance\n );\n }\n}\n\nfunction checkAllowedMembersContradiction(\n ctx: ValidationContext,\n fieldName: string,\n constraints: readonly ConstraintNode[]\n): void {\n const members = findAllowedMembers(constraints);\n if (members.length < 2) return;\n\n // Intersect all allowedMembers sets; if empty — contradiction\n const firstSet = new Set(members[0]?.members ?? []);\n for (let i = 1; i < members.length; i++) {\n const current = members[i];\n if (current === undefined) continue;\n for (const m of firstSet) {\n if (!current.members.includes(m)) {\n firstSet.delete(m);\n }\n }\n }\n\n if (firstSet.size === 0) {\n const first = members[0];\n const second = members[1];\n if (first !== undefined && second !== undefined) {\n addContradiction(\n ctx,\n `Field \"${fieldName}\": allowedMembers constraints have an empty intersection (no valid values remain)`,\n first.provenance,\n second.provenance\n );\n }\n }\n}\n\n// =============================================================================\n// TYPE APPLICABILITY CHECKS\n// =============================================================================\n\n/** Return a readable label for a type node for use in diagnostics. */\nfunction typeLabel(type: TypeNode): string {\n switch (type.kind) {\n case \"primitive\":\n return type.primitiveKind;\n case \"enum\":\n return \"enum\";\n case \"array\":\n return \"array\";\n case \"object\":\n return \"object\";\n case \"union\":\n return \"union\";\n case \"reference\":\n return `reference(${type.name})`;\n case \"dynamic\":\n return `dynamic(${type.dynamicKind})`;\n case \"custom\":\n return `custom(${type.typeId})`;\n default: {\n const _exhaustive: never = type;\n return String(_exhaustive);\n }\n }\n}\n\nfunction checkTypeApplicability(\n ctx: ValidationContext,\n fieldName: string,\n type: TypeNode,\n constraints: readonly ConstraintNode[]\n): void {\n const isNumber = type.kind === \"primitive\" && type.primitiveKind === \"number\";\n const isString = type.kind === \"primitive\" && type.primitiveKind === \"string\";\n const isArray = type.kind === \"array\";\n const isEnum = type.kind === \"enum\";\n\n const label = typeLabel(type);\n\n for (const constraint of constraints) {\n const ck = constraint.constraintKind;\n\n switch (ck) {\n case \"minimum\":\n case \"maximum\":\n case \"exclusiveMinimum\":\n case \"exclusiveMaximum\":\n case \"multipleOf\": {\n if (!isNumber) {\n addTypeMismatch(\n ctx,\n `Field \"${fieldName}\": constraint \"${ck}\" is only valid on number fields, but field type is \"${label}\"`,\n constraint.provenance\n );\n }\n break;\n }\n case \"minLength\":\n case \"maxLength\":\n case \"pattern\": {\n if (!isString) {\n addTypeMismatch(\n ctx,\n `Field \"${fieldName}\": constraint \"${ck}\" is only valid on string fields, but field type is \"${label}\"`,\n constraint.provenance\n );\n }\n break;\n }\n case \"minItems\":\n case \"maxItems\":\n case \"uniqueItems\": {\n if (!isArray) {\n addTypeMismatch(\n ctx,\n `Field \"${fieldName}\": constraint \"${ck}\" is only valid on array fields, but field type is \"${label}\"`,\n constraint.provenance\n );\n }\n break;\n }\n case \"allowedMembers\": {\n if (!isEnum) {\n addTypeMismatch(\n ctx,\n `Field \"${fieldName}\": constraint \"allowedMembers\" is only valid on enum fields, but field type is \"${label}\"`,\n constraint.provenance\n );\n }\n break;\n }\n case \"custom\": {\n checkCustomConstraint(ctx, fieldName, type, constraint);\n break;\n }\n default: {\n const _exhaustive: never = constraint;\n throw new Error(\n `Unhandled constraint kind: ${(_exhaustive as ConstraintNode).constraintKind}`\n );\n }\n }\n }\n}\n\n/**\n * Check a custom constraint against the extension registry.\n *\n * When the registry is available:\n * - If the constraint ID is not found, emit UNKNOWN_EXTENSION warning\n * - If found and the registration has `applicableTypes`, verify the field's\n * type kind is in that list (emit TYPE_MISMATCH if not)\n * - If `applicableTypes` is null, the constraint applies to any type\n *\n * When no registry is available, custom constraints are silently skipped.\n */\nfunction checkCustomConstraint(\n ctx: ValidationContext,\n fieldName: string,\n type: TypeNode,\n constraint: ConstraintNode & { readonly constraintKind: \"custom\" }\n): void {\n if (ctx.extensionRegistry === undefined) return;\n\n const registration = ctx.extensionRegistry.findConstraint(constraint.constraintId);\n\n if (registration === undefined) {\n addUnknownExtension(\n ctx,\n `Field \"${fieldName}\": custom constraint \"${constraint.constraintId}\" is not registered in the extension registry`,\n constraint.provenance\n );\n return;\n }\n\n // If applicableTypes is null, the constraint applies to any type\n if (registration.applicableTypes === null) return;\n\n if (!registration.applicableTypes.includes(type.kind)) {\n addTypeMismatch(\n ctx,\n `Field \"${fieldName}\": custom constraint \"${constraint.constraintId}\" is not applicable to type \"${typeLabel(type)}\"`,\n constraint.provenance\n );\n }\n}\n\n// =============================================================================\n// FIELD VALIDATION\n// =============================================================================\n\nfunction validateFieldNode(ctx: ValidationContext, field: FieldNode): void {\n validateConstraints(ctx, field.name, field.type, field.constraints);\n\n // Recurse into object type properties\n if (field.type.kind === \"object\") {\n for (const prop of field.type.properties) {\n validateObjectProperty(ctx, field.name, prop);\n }\n }\n}\n\nfunction validateObjectProperty(\n ctx: ValidationContext,\n parentName: string,\n prop: ObjectProperty\n): void {\n const qualifiedName = `${parentName}.${prop.name}`;\n validateConstraints(ctx, qualifiedName, prop.type, prop.constraints);\n\n // Recurse further if this property is also an object\n if (prop.type.kind === \"object\") {\n for (const nestedProp of prop.type.properties) {\n validateObjectProperty(ctx, qualifiedName, nestedProp);\n }\n }\n}\n\nfunction validateConstraints(\n ctx: ValidationContext,\n name: string,\n type: TypeNode,\n constraints: readonly ConstraintNode[]\n): void {\n checkNumericContradictions(ctx, name, constraints);\n checkLengthContradictions(ctx, name, constraints);\n checkAllowedMembersContradiction(ctx, name, constraints);\n checkTypeApplicability(ctx, name, type, constraints);\n}\n\n// =============================================================================\n// RECURSIVE ELEMENT WALK\n// =============================================================================\n\nfunction validateElement(ctx: ValidationContext, element: FormIRElement): void {\n switch (element.kind) {\n case \"field\":\n validateFieldNode(ctx, element);\n break;\n case \"group\":\n for (const child of element.elements) {\n validateElement(ctx, child);\n }\n break;\n case \"conditional\":\n for (const child of element.elements) {\n validateElement(ctx, child);\n }\n break;\n default: {\n const _exhaustive: never = element;\n throw new Error(`Unhandled element kind: ${(_exhaustive as FormIRElement).kind}`);\n }\n }\n}\n\n// =============================================================================\n// MAIN EXPORT\n// =============================================================================\n\n/**\n * Validate all constraints in a {@link FormIR}.\n *\n * Checks for:\n * - Contradictions between paired constraints (e.g. `minimum > maximum`)\n * - Type applicability violations (e.g. `minLength` on a number field)\n * - Custom constraint type applicability (via extension registry)\n * - Unknown extension constraints (when `extensionRegistry` is provided)\n *\n * @param ir - The form IR to validate.\n * @param options - Optional configuration.\n * @returns A {@link ValidationResult} with diagnostics and a `valid` flag.\n */\nexport function validateIR(ir: FormIR, options?: ValidateIROptions): ValidationResult {\n const ctx: ValidationContext = {\n diagnostics: [],\n vendorPrefix: options?.vendorPrefix ?? \"FORMSPEC\",\n extensionRegistry: options?.extensionRegistry,\n };\n\n for (const element of ir.elements) {\n validateElement(ctx, element);\n }\n\n return {\n diagnostics: ctx.diagnostics,\n valid: ctx.diagnostics.every((d) => d.severity !== \"error\"),\n };\n}\n\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACiDA,kBAA2B;AAO3B,IAAM,uBAAmC;AAAA,EACvC,SAAS;AAAA,EACT,MAAM;AAAA,EACN,MAAM;AAAA,EACN,QAAQ;AACV;AAMA,SAAS,QAAQ,IAAsD;AACrE,SAAO,GAAG,UAAU;AACtB;AAEA,SAAS,cACP,IAC4D;AAC5D,SAAO,GAAG,UAAU;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QAAQ,MAAM;AAAA,IACpB,KAAK;AACH,wBAAkB,KAAK,OAAO;AAC9B;AAAA,IACF,KAAK;AACH,iBAAW,SAAS,QAAQ,UAAU;AACpC,wBAAgB,KAAK,KAAK;AAAA,MAC5B;AACA;AAAA,IACF,KAAK;AACH,iBAAW,SAAS,QAAQ,UAAU;AACpC,wBAAgB,KAAK,KAAK;AAAA,MAC5B;AACA;AAAA,IACF,SAAS;AACP,YAAM,cAAqB;AAC3B,YAAM,IAAI,MAAM,2BAA4B,YAA8B,IAAI,EAAE;AAAA,IAClF;AAAA,EACF;AACF;AAmBO,SAAS,WAAW,IAAY,SAA+C;AACpF,QAAM,MAAyB;AAAA,IAC7B,aAAa,CAAC;AAAA,IACd,cAAc,SAAS,gBAAgB;AAAA,IACvC,mBAAmB,SAAS;AAAA,EAC9B;AAEA,aAAW,WAAW,GAAG,UAAU;AACjC,oBAAgB,KAAK,OAAO;AAAA,EAC9B;AAEA,SAAO;AAAA,IACL,aAAa,IAAI;AAAA,IACjB,OAAO,IAAI,YAAY,MAAM,CAAC,MAAM,EAAE,aAAa,OAAO;AAAA,EAC5D;AACF;;;AVvaO,SAAS,iBAAmD,MAAgC;AACjG,SAAO;AAAA,IACL,YAAY,mBAAmB,IAAI;AAAA,IACnC,UAAU,iBAAiB,IAAI;AAAA,EACjC;AACF;","names":["import_core","import_zod","import_zod"]}
1
+ {"version":3,"sources":["../src/browser.ts","../src/canonicalize/chain-dsl-canonicalizer.ts","../src/canonicalize/tsdoc-canonicalizer.ts","../src/json-schema/ir-generator.ts","../src/json-schema/generator.ts","../src/ui-schema/schema.ts","../src/ui-schema/ir-generator.ts","../src/ui-schema/generator.ts","../src/json-schema/types.ts","../src/json-schema/schema.ts","../src/validate/constraint-validator.ts"],"sourcesContent":["/**\n * Browser-safe exports for `@formspec/build`.\n *\n * This entry point excludes Node.js-specific functions like `writeSchemas`\n * that use `node:fs` and `node:path`, making it suitable for browser environments.\n *\n * @example\n * ```typescript\n * // In browser code (e.g., playground)\n * import { buildFormSchemas, generateJsonSchema, generateUiSchema } from \"@formspec/build/browser\";\n *\n * const form = formspec(field.text(\"name\"));\n * const { jsonSchema, uiSchema } = buildFormSchemas(form);\n * ```\n *\n * @packageDocumentation\n */\n\nimport type { FormElement, FormSpec } from \"@formspec/core\";\nimport { generateJsonSchema } from \"./json-schema/generator.js\";\nimport { generateUiSchema } from \"./ui-schema/generator.js\";\nimport type { JsonSchema2020 } from \"./json-schema/ir-generator.js\";\nimport type { UISchema } from \"./ui-schema/types.js\";\n\n// Re-export types\nexport type { JsonSchema2020 } from \"./json-schema/ir-generator.js\";\n\nexport type {\n JSONSchema7,\n JSONSchemaType,\n ExtendedJSONSchema7,\n FormSpecSchemaExtensions,\n} from \"./json-schema/types.js\";\n\nexport { setSchemaExtension, getSchemaExtension } from \"./json-schema/types.js\";\n\nexport type {\n UISchema,\n UISchemaElement,\n UISchemaElementBase,\n UISchemaElementType,\n ControlElement,\n VerticalLayout,\n HorizontalLayout,\n GroupLayout,\n Categorization,\n Category,\n LabelElement,\n Rule,\n RuleEffect,\n RuleConditionSchema,\n SchemaBasedCondition,\n} from \"./ui-schema/types.js\";\n\n// Zod validation schemas\nexport {\n ruleEffectSchema,\n uiSchemaElementTypeSchema,\n ruleConditionSchema,\n schemaBasedConditionSchema,\n ruleSchema,\n controlSchema,\n verticalLayoutSchema,\n horizontalLayoutSchema,\n groupLayoutSchema,\n categorizationSchema,\n categorySchema,\n labelElementSchema,\n uiSchemaElementSchema,\n uiSchema as uiSchemaSchema,\n} from \"./ui-schema/schema.js\";\n\nexport { jsonSchemaTypeSchema, jsonSchema7Schema } from \"./json-schema/schema.js\";\n\n// Re-export individual generators\nexport { generateJsonSchema } from \"./json-schema/generator.js\";\nexport { generateUiSchema } from \"./ui-schema/generator.js\";\n\n// IR canonicalization (browser-safe: no Node.js dependencies)\nexport { canonicalizeChainDSL } from \"./canonicalize/chain-dsl-canonicalizer.js\";\n\n// IR validation (browser-safe: no Node.js dependencies)\nexport { validateIR } from \"./validate/constraint-validator.js\";\nexport type {\n ValidationDiagnostic,\n ValidationResult,\n ValidateIROptions,\n} from \"./validate/constraint-validator.js\";\n\n/**\n * Result of building form schemas.\n */\nexport interface BuildResult {\n /** JSON Schema 2020-12 for validation */\n readonly jsonSchema: JsonSchema2020;\n /** JSON Forms UI Schema for rendering */\n readonly uiSchema: UISchema;\n}\n\n/**\n * Builds both JSON Schema and UI Schema from a FormSpec.\n *\n * This is a browser-safe version that does not include file system operations.\n *\n * @example\n * ```typescript\n * const form = formspec(\n * field.text(\"name\", { required: true }),\n * field.number(\"age\", { min: 0 }),\n * );\n *\n * const { jsonSchema, uiSchema } = buildFormSchemas(form);\n * ```\n *\n * @param form - The FormSpec to build schemas from\n * @returns Object containing both jsonSchema and uiSchema\n */\nexport function buildFormSchemas<E extends readonly FormElement[]>(form: FormSpec<E>): BuildResult {\n return {\n jsonSchema: generateJsonSchema(form),\n uiSchema: generateUiSchema(form),\n };\n}\n","/**\n * Canonicalizer that translates chain DSL `FormSpec` objects into the\n * canonical FormIR intermediate representation.\n *\n * This module maps the runtime objects produced by `@formspec/dsl` builder\n * functions (`field.*`, `group`, `when`, `formspec`) into the IR that all\n * downstream phases (validation, JSON Schema generation, UI Schema generation)\n * consume.\n */\n\nimport type {\n // Source types (chain DSL)\n AnyField,\n ArrayField,\n BooleanField,\n Conditional,\n DynamicEnumField,\n DynamicSchemaField,\n EnumOptionValue,\n FormElement,\n FormSpec,\n Group,\n NumberField,\n ObjectField,\n StaticEnumField,\n TextField,\n // IR types\n JsonValue,\n AnnotationNode,\n ArrayTypeNode,\n ConstraintNode,\n ConditionalLayoutNode,\n DisplayNameAnnotationNode,\n DynamicTypeNode,\n EnumMember,\n EnumTypeNode,\n FieldNode,\n FormIR,\n FormIRElement,\n GroupLayoutNode,\n LengthConstraintNode,\n NumericConstraintNode,\n ObjectProperty,\n PatternConstraintNode,\n ObjectTypeNode,\n PlaceholderAnnotationNode,\n PrimitiveTypeNode,\n Provenance,\n TypeNode,\n} from \"@formspec/core\";\nimport { IR_VERSION } from \"@formspec/core\";\n\n// =============================================================================\n// CONSTANTS\n// =============================================================================\n\n/** Default provenance for chain DSL nodes (no source location available). */\nconst CHAIN_DSL_PROVENANCE: Provenance = {\n surface: \"chain-dsl\",\n file: \"\",\n line: 0,\n column: 0,\n} as const;\n\n// =============================================================================\n// TYPE GUARDS\n// =============================================================================\n\nfunction isGroup(el: FormElement): el is Group<readonly FormElement[]> {\n return el._type === \"group\";\n}\n\nfunction isConditional(\n el: FormElement\n): el is Conditional<string, unknown, readonly FormElement[]> {\n return el._type === \"conditional\";\n}\n\nfunction isField(el: FormElement): el is AnyField {\n return el._type === \"field\";\n}\n\n// =============================================================================\n// PUBLIC API\n// =============================================================================\n\n/**\n * Translates a chain DSL `FormSpec` into the canonical `FormIR`.\n *\n * @param form - A form specification created via `formspec(...)` from `@formspec/dsl`\n * @returns The canonical intermediate representation\n */\nexport function canonicalizeChainDSL(form: FormSpec<readonly FormElement[]>): FormIR {\n return {\n kind: \"form-ir\",\n irVersion: IR_VERSION,\n elements: canonicalizeElements(form.elements),\n typeRegistry: {},\n provenance: CHAIN_DSL_PROVENANCE,\n };\n}\n\n// =============================================================================\n// ELEMENT CANONICALIZATION\n// =============================================================================\n\n/**\n * Canonicalizes an array of chain DSL form elements into IR elements.\n */\nfunction canonicalizeElements(elements: readonly FormElement[]): FormIRElement[] {\n return elements.map(canonicalizeElement);\n}\n\n/**\n * Dispatches a single form element to its specific canonicalization function.\n */\nfunction canonicalizeElement(element: FormElement): FormIRElement {\n if (isField(element)) {\n return canonicalizeField(element);\n }\n if (isGroup(element)) {\n return canonicalizeGroup(element);\n }\n if (isConditional(element)) {\n return canonicalizeConditional(element);\n }\n const _exhaustive: never = element;\n throw new Error(`Unknown element type: ${JSON.stringify(_exhaustive)}`);\n}\n\n// =============================================================================\n// FIELD CANONICALIZATION\n// =============================================================================\n\n/**\n * Dispatches a field element to its type-specific canonicalization function.\n */\nfunction canonicalizeField(field: AnyField): FieldNode {\n switch (field._field) {\n case \"text\":\n return canonicalizeTextField(field);\n case \"number\":\n return canonicalizeNumberField(field);\n case \"boolean\":\n return canonicalizeBooleanField(field);\n case \"enum\":\n return canonicalizeStaticEnumField(field);\n case \"dynamic_enum\":\n return canonicalizeDynamicEnumField(field);\n case \"dynamic_schema\":\n return canonicalizeDynamicSchemaField(field);\n case \"array\":\n return canonicalizeArrayField(field);\n case \"object\":\n return canonicalizeObjectField(field);\n default: {\n const _exhaustive: never = field;\n throw new Error(`Unknown field type: ${JSON.stringify(_exhaustive)}`);\n }\n }\n}\n\n// =============================================================================\n// SPECIFIC FIELD TYPE CANONICALIZERS\n// =============================================================================\n\nfunction canonicalizeTextField(field: TextField<string>): FieldNode {\n const type: PrimitiveTypeNode = { kind: \"primitive\", primitiveKind: \"string\" };\n const constraints: ConstraintNode[] = [];\n\n if (field.minLength !== undefined) {\n const c: LengthConstraintNode = {\n kind: \"constraint\",\n constraintKind: \"minLength\",\n value: field.minLength,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n constraints.push(c);\n }\n\n if (field.maxLength !== undefined) {\n const c: LengthConstraintNode = {\n kind: \"constraint\",\n constraintKind: \"maxLength\",\n value: field.maxLength,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n constraints.push(c);\n }\n\n if (field.pattern !== undefined) {\n const c: PatternConstraintNode = {\n kind: \"constraint\",\n constraintKind: \"pattern\",\n pattern: field.pattern,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n constraints.push(c);\n }\n\n return buildFieldNode(\n field.name,\n type,\n field.required,\n buildAnnotations(field.label, field.placeholder),\n constraints\n );\n}\n\nfunction canonicalizeNumberField(field: NumberField<string>): FieldNode {\n const type: PrimitiveTypeNode = { kind: \"primitive\", primitiveKind: \"number\" };\n const constraints: ConstraintNode[] = [];\n\n if (field.min !== undefined) {\n const c: NumericConstraintNode = {\n kind: \"constraint\",\n constraintKind: \"minimum\",\n value: field.min,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n constraints.push(c);\n }\n\n if (field.max !== undefined) {\n const c: NumericConstraintNode = {\n kind: \"constraint\",\n constraintKind: \"maximum\",\n value: field.max,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n constraints.push(c);\n }\n\n if (field.multipleOf !== undefined) {\n const c: NumericConstraintNode = {\n kind: \"constraint\",\n constraintKind: \"multipleOf\",\n value: field.multipleOf,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n constraints.push(c);\n }\n\n return buildFieldNode(\n field.name,\n type,\n field.required,\n buildAnnotations(field.label),\n constraints\n );\n}\n\nfunction canonicalizeBooleanField(field: BooleanField<string>): FieldNode {\n const type: PrimitiveTypeNode = { kind: \"primitive\", primitiveKind: \"boolean\" };\n return buildFieldNode(field.name, type, field.required, buildAnnotations(field.label));\n}\n\nfunction canonicalizeStaticEnumField(\n field: StaticEnumField<string, readonly EnumOptionValue[]>\n): FieldNode {\n const members: EnumMember[] = field.options.map((opt) => {\n if (typeof opt === \"string\") {\n return { value: opt } satisfies EnumMember;\n }\n // Object option with id/label\n return { value: opt.id, displayName: opt.label } satisfies EnumMember;\n });\n\n const type: EnumTypeNode = { kind: \"enum\", members };\n return buildFieldNode(field.name, type, field.required, buildAnnotations(field.label));\n}\n\nfunction canonicalizeDynamicEnumField(field: DynamicEnumField<string, string>): FieldNode {\n const type: DynamicTypeNode = {\n kind: \"dynamic\",\n dynamicKind: \"enum\",\n sourceKey: field.source,\n parameterFields: field.params ? [...field.params] : [],\n };\n return buildFieldNode(field.name, type, field.required, buildAnnotations(field.label));\n}\n\nfunction canonicalizeDynamicSchemaField(field: DynamicSchemaField<string>): FieldNode {\n const type: DynamicTypeNode = {\n kind: \"dynamic\",\n dynamicKind: \"schema\",\n sourceKey: field.schemaSource,\n parameterFields: [],\n };\n return buildFieldNode(field.name, type, field.required, buildAnnotations(field.label));\n}\n\nfunction canonicalizeArrayField(field: ArrayField<string, readonly FormElement[]>): FieldNode {\n // Array items form an object type from the sub-elements\n const itemProperties = buildObjectProperties(field.items);\n const itemsType: ObjectTypeNode = {\n kind: \"object\",\n properties: itemProperties,\n additionalProperties: false,\n };\n const type: ArrayTypeNode = { kind: \"array\", items: itemsType };\n\n const constraints: ConstraintNode[] = [];\n if (field.minItems !== undefined) {\n const c: LengthConstraintNode = {\n kind: \"constraint\",\n constraintKind: \"minItems\",\n value: field.minItems,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n constraints.push(c);\n }\n if (field.maxItems !== undefined) {\n const c: LengthConstraintNode = {\n kind: \"constraint\",\n constraintKind: \"maxItems\",\n value: field.maxItems,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n constraints.push(c);\n }\n\n return buildFieldNode(\n field.name,\n type,\n field.required,\n buildAnnotations(field.label),\n constraints\n );\n}\n\nfunction canonicalizeObjectField(field: ObjectField<string, readonly FormElement[]>): FieldNode {\n const properties = buildObjectProperties(field.properties);\n const type: ObjectTypeNode = {\n kind: \"object\",\n properties,\n additionalProperties: false,\n };\n return buildFieldNode(field.name, type, field.required, buildAnnotations(field.label));\n}\n\n// =============================================================================\n// LAYOUT CANONICALIZATION\n// =============================================================================\n\nfunction canonicalizeGroup(g: Group<readonly FormElement[]>): GroupLayoutNode {\n return {\n kind: \"group\",\n label: g.label,\n elements: canonicalizeElements(g.elements),\n provenance: CHAIN_DSL_PROVENANCE,\n };\n}\n\nfunction canonicalizeConditional(\n c: Conditional<string, unknown, readonly FormElement[]>\n): ConditionalLayoutNode {\n return {\n kind: \"conditional\",\n fieldName: c.field,\n // Conditional values from the chain DSL are JSON-serializable primitives\n // (strings, numbers, booleans) produced by the `is()` predicate helper.\n value: assertJsonValue(c.value),\n elements: canonicalizeElements(c.elements),\n provenance: CHAIN_DSL_PROVENANCE,\n };\n}\n\n// =============================================================================\n// HELPERS\n// =============================================================================\n\n/**\n * Validates that a value is JSON-serializable (`JsonValue`).\n * The chain DSL's `is()` helper constrains conditional values to\n * JSON-compatible primitives, but the TypeScript type is `unknown`.\n * This runtime guard replaces an `as` cast with a validated assertion.\n */\nfunction assertJsonValue(v: unknown): JsonValue {\n if (v === null || typeof v === \"string\" || typeof v === \"number\" || typeof v === \"boolean\") {\n return v;\n }\n if (Array.isArray(v)) {\n return v.map(assertJsonValue);\n }\n if (typeof v === \"object\") {\n const result: Record<string, JsonValue> = {};\n for (const [key, val] of Object.entries(v)) {\n result[key] = assertJsonValue(val);\n }\n return result;\n }\n // Remaining types (function, symbol, bigint, undefined) are not JSON-serializable\n throw new TypeError(`Conditional value is not a valid JsonValue: ${typeof v}`);\n}\n\n/**\n * Builds a FieldNode from common field properties.\n */\nfunction buildFieldNode(\n name: string,\n type: TypeNode,\n required: boolean | undefined,\n annotations: AnnotationNode[],\n constraints: ConstraintNode[] = []\n): FieldNode {\n return {\n kind: \"field\",\n name,\n type,\n required: required === true,\n constraints,\n annotations,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n}\n\n/**\n * Builds annotation nodes from optional label and placeholder values.\n */\nfunction buildAnnotations(label?: string, placeholder?: string): AnnotationNode[] {\n const annotations: AnnotationNode[] = [];\n\n if (label !== undefined) {\n const a: DisplayNameAnnotationNode = {\n kind: \"annotation\",\n annotationKind: \"displayName\",\n value: label,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n annotations.push(a);\n }\n\n if (placeholder !== undefined) {\n const a: PlaceholderAnnotationNode = {\n kind: \"annotation\",\n annotationKind: \"placeholder\",\n value: placeholder,\n provenance: CHAIN_DSL_PROVENANCE,\n };\n annotations.push(a);\n }\n\n return annotations;\n}\n\n/**\n * Converts an array of form elements into ObjectProperty nodes.\n * Used for ObjectField properties and ArrayField items.\n *\n * Only field elements produce properties; groups and conditionals within\n * an object/array context are recursively flattened to extract their fields.\n *\n * Fields inside conditional branches are always marked `optional: true`\n * because their presence in the data depends on the condition being met.\n * This matches the DSL's type inference behavior where conditional fields\n * produce optional properties in `InferFormSchema`.\n *\n * @param elements - The form elements to convert\n * @param insideConditional - Whether these elements are inside a conditional branch\n */\nfunction buildObjectProperties(\n elements: readonly FormElement[],\n insideConditional = false\n): ObjectProperty[] {\n const properties: ObjectProperty[] = [];\n\n for (const el of elements) {\n if (isField(el)) {\n const fieldNode = canonicalizeField(el);\n properties.push({\n name: fieldNode.name,\n type: fieldNode.type,\n // Fields inside a conditional branch are always optional in the\n // data schema, regardless of their `required` flag — the condition\n // may not be met, so the field may be absent.\n optional: insideConditional || !fieldNode.required,\n constraints: fieldNode.constraints,\n annotations: fieldNode.annotations,\n provenance: CHAIN_DSL_PROVENANCE,\n });\n } else if (isGroup(el)) {\n // Groups inside object/array items contribute their fields by flattening.\n // Groups do not affect optionality — pass through the current state.\n properties.push(...buildObjectProperties(el.elements, insideConditional));\n } else if (isConditional(el)) {\n // Conditionals inside object/array items contribute their fields by\n // flattening, but all fields inside are forced optional.\n properties.push(...buildObjectProperties(el.elements, true));\n }\n }\n\n return properties;\n}\n","/**\n * TSDoc canonicalizer — assembles an {@link IRClassAnalysis} into a canonical\n * {@link FormIR}, applying layout metadata from `@Group` and `@ShowWhen`\n * decorators.\n *\n * The analysis functions in `class-analyzer.ts` produce `FieldNode[]`,\n * `fieldLayouts`, and `typeRegistry` directly. This canonicalizer uses\n * the layout metadata to wrap fields in `GroupLayoutNode` and\n * `ConditionalLayoutNode` elements.\n */\n\nimport type {\n FormIR,\n FormIRElement,\n FieldNode,\n GroupLayoutNode,\n ConditionalLayoutNode,\n Provenance,\n} from \"@formspec/core\";\nimport { IR_VERSION } from \"@formspec/core\";\nimport type { IRClassAnalysis, FieldLayoutMetadata } from \"../analyzer/class-analyzer.js\";\n\n/**\n * Source-level metadata for provenance tracking.\n */\nexport interface TSDocSource {\n /** Absolute path to the source file. */\n readonly file: string;\n}\n\n/**\n * Wraps an {@link IRClassAnalysis} (from `analyzeClassToIR`,\n * `analyzeInterfaceToIR`, or `analyzeTypeAliasToIR`) into a canonical\n * {@link FormIR}.\n *\n * Fields with `@Group` decorators are grouped into `GroupLayoutNode` elements.\n * Fields with `@ShowWhen` decorators are wrapped in `ConditionalLayoutNode` elements.\n * When both are present, the conditional wraps the field inside the group.\n *\n * @param analysis - IR analysis result (fields are already FieldNode[])\n * @param source - Optional source file metadata for provenance\n * @returns The canonical FormIR\n */\nexport function canonicalizeTSDoc(analysis: IRClassAnalysis, source?: TSDocSource): FormIR {\n const file = source?.file ?? \"\";\n\n const provenance: Provenance = {\n surface: \"tsdoc\",\n file,\n line: 1,\n column: 0,\n };\n\n const elements = assembleElements(analysis.fields, analysis.fieldLayouts, provenance);\n\n return {\n kind: \"form-ir\",\n irVersion: IR_VERSION,\n elements,\n typeRegistry: analysis.typeRegistry,\n provenance,\n };\n}\n\n/**\n * Assembles flat fields and their layout metadata into a tree of\n * `FormIRElement[]` with groups and conditionals.\n *\n * Fields are processed in order. Consecutive fields with the same\n * `@Group` label are collected into a single `GroupLayoutNode`.\n * Fields with `@ShowWhen` are wrapped in `ConditionalLayoutNode`.\n */\nfunction assembleElements(\n fields: readonly FieldNode[],\n layouts: readonly FieldLayoutMetadata[],\n provenance: Provenance\n): readonly FormIRElement[] {\n const elements: FormIRElement[] = [];\n\n // Group consecutive fields with the same group label together.\n // We use an ordered map to preserve insertion order of groups.\n const groupMap = new Map<string, FormIRElement[]>();\n const topLevelOrder: (\n | { type: \"group\"; label: string }\n | { type: \"element\"; element: FormIRElement }\n )[] = [];\n\n for (let i = 0; i < fields.length; i++) {\n const field = fields[i];\n const layout = layouts[i];\n if (!field || !layout) continue;\n\n // Wrap in conditional if @ShowWhen is present\n const element = wrapInConditional(field, layout, provenance);\n\n if (layout.groupLabel !== undefined) {\n const label = layout.groupLabel;\n let groupElements = groupMap.get(label);\n if (!groupElements) {\n groupElements = [];\n groupMap.set(label, groupElements);\n topLevelOrder.push({ type: \"group\", label });\n }\n groupElements.push(element);\n } else {\n topLevelOrder.push({ type: \"element\", element });\n }\n }\n\n // Assemble the final element array in order\n for (const entry of topLevelOrder) {\n if (entry.type === \"group\") {\n const groupElements = groupMap.get(entry.label);\n if (groupElements) {\n const groupNode: GroupLayoutNode = {\n kind: \"group\",\n label: entry.label,\n elements: groupElements,\n provenance,\n };\n elements.push(groupNode);\n // Clear so duplicate group labels in topLevelOrder don't re-emit\n groupMap.delete(entry.label);\n }\n } else {\n elements.push(entry.element);\n }\n }\n\n return elements;\n}\n\n/**\n * Wraps a field in a `ConditionalLayoutNode` if the layout has `showWhen` metadata.\n */\nfunction wrapInConditional(\n field: FieldNode,\n layout: FieldLayoutMetadata,\n provenance: Provenance\n): FormIRElement {\n if (layout.showWhen === undefined) {\n return field;\n }\n\n const conditional: ConditionalLayoutNode = {\n kind: \"conditional\",\n fieldName: layout.showWhen.field,\n value: layout.showWhen.value,\n elements: [field],\n provenance,\n };\n\n return conditional;\n}\n","/**\n * JSON Schema 2020-12 generator that consumes the canonical FormIR.\n *\n * This generator is a pure function of the IR. It never consults the TypeScript\n * AST or surface syntax directly — only the IR (per the JSON Schema vocabulary spec §1.2).\n *\n * @see https://json-schema.org/draft/2020-12/schema\n * @see https://json-schema.org/draft/2020-12/schema\n */\n\nimport type {\n FormIR,\n FormIRElement,\n FieldNode,\n TypeNode,\n PrimitiveTypeNode,\n EnumTypeNode,\n ArrayTypeNode,\n ObjectTypeNode,\n UnionTypeNode,\n ReferenceTypeNode,\n DynamicTypeNode,\n CustomTypeNode,\n ConstraintNode,\n AnnotationNode,\n ObjectProperty,\n} from \"@formspec/core\";\n\n// =============================================================================\n// OUTPUT TYPE\n// =============================================================================\n\n/**\n * A JSON Schema 2020-12 document, sub-schema, or keyword collection.\n *\n * This interface covers the subset of JSON Schema 2020-12 that this generator\n * emits, plus an index signature for custom `x-formspec-*` extension keywords.\n */\nexport interface JsonSchema2020 {\n $schema?: string;\n $ref?: string;\n $defs?: Record<string, JsonSchema2020>;\n type?: string;\n properties?: Record<string, JsonSchema2020>;\n required?: string[];\n items?: JsonSchema2020;\n additionalProperties?: boolean;\n enum?: readonly (string | number)[];\n const?: string | number | boolean | null;\n oneOf?: readonly JsonSchema2020[];\n anyOf?: readonly JsonSchema2020[];\n // Constraints\n minimum?: number;\n maximum?: number;\n exclusiveMinimum?: number;\n exclusiveMaximum?: number;\n multipleOf?: number;\n minLength?: number;\n maxLength?: number;\n minItems?: number;\n maxItems?: number;\n pattern?: string;\n uniqueItems?: boolean;\n // Annotations\n title?: string;\n description?: string;\n default?: unknown;\n deprecated?: boolean;\n // Extensions (open for vendor-prefixed keywords, e.g., x-formspec-*, x-stripe-*)\n // The vendor prefix is configurable (white-labelable).\n [key: `x-${string}`]: unknown;\n}\n\n// =============================================================================\n// CONTEXT\n// =============================================================================\n\n/**\n * Mutable accumulator passed through the generation traversal.\n *\n * Using a context object rather than return-value threading keeps the\n * recursive generators simple and avoids repeated object spreading.\n */\ninterface GeneratorContext {\n /** Named type schemas collected during traversal, keyed by reference name. */\n readonly defs: Record<string, JsonSchema2020>;\n}\n\nfunction makeContext(): GeneratorContext {\n return { defs: {} };\n}\n\n// =============================================================================\n// PUBLIC API\n// =============================================================================\n\n/**\n * Generates a JSON Schema 2020-12 object from a canonical FormIR.\n *\n * Groups and conditionals are flattened — they influence UI layout but do not\n * affect the data schema. All fields appear at the level they would occupy in\n * the output data.\n *\n * Named types in the `typeRegistry` are emitted as `$defs` entries and\n * referenced via `$ref` (per PP7 — high-fidelity output).\n *\n * @example\n * ```typescript\n * import { canonicalizeDSL } from \"./canonicalize/index.js\";\n * import { generateJsonSchemaFromIR } from \"./json-schema/ir-generator.js\";\n * import { formspec, field } from \"@formspec/dsl\";\n *\n * const form = formspec(\n * field.text(\"name\", { label: \"Name\", required: true }),\n * field.number(\"age\", { min: 0 }),\n * );\n * const ir = canonicalizeDSL(form);\n * const schema = generateJsonSchemaFromIR(ir);\n * // {\n * // $schema: \"https://json-schema.org/draft/2020-12/schema\",\n * // type: \"object\",\n * // properties: {\n * // name: { type: \"string\", title: \"Name\" },\n * // age: { type: \"number\", minimum: 0 }\n * // },\n * // required: [\"name\"]\n * // }\n * ```\n *\n * @param ir - The canonical FormIR produced by a canonicalizer\n * @returns A plain JSON-serializable JSON Schema 2020-12 object\n */\nexport function generateJsonSchemaFromIR(ir: FormIR): JsonSchema2020 {\n const ctx = makeContext();\n\n // Seed $defs from the type registry so referenced types are available even if\n // the field tree traversal never visits them (e.g., unreferenced types added\n // by a TSDoc canonicalizer pass).\n for (const [name, typeDef] of Object.entries(ir.typeRegistry)) {\n ctx.defs[name] = generateTypeNode(typeDef.type, ctx);\n }\n\n const properties: Record<string, JsonSchema2020> = {};\n const required: string[] = [];\n\n collectFields(ir.elements, properties, required, ctx);\n\n // Deduplicate required (same field can appear across conditional branches).\n const uniqueRequired = [...new Set(required)];\n\n const result: JsonSchema2020 = {\n $schema: \"https://json-schema.org/draft/2020-12/schema\",\n type: \"object\",\n properties,\n ...(uniqueRequired.length > 0 && { required: uniqueRequired }),\n };\n\n if (Object.keys(ctx.defs).length > 0) {\n result.$defs = ctx.defs;\n }\n\n return result;\n}\n\n// =============================================================================\n// ELEMENT TRAVERSAL\n// =============================================================================\n\n/**\n * Recursively visits all IR elements, collecting field schemas and required names.\n *\n * Groups and conditionals are transparent to the schema — their children are\n * lifted to the enclosing level (per the JSON Schema vocabulary spec §1.2).\n */\nfunction collectFields(\n elements: readonly FormIRElement[],\n properties: Record<string, JsonSchema2020>,\n required: string[],\n ctx: GeneratorContext\n): void {\n for (const element of elements) {\n switch (element.kind) {\n case \"field\":\n properties[element.name] = generateFieldSchema(element, ctx);\n if (element.required) {\n required.push(element.name);\n }\n break;\n\n case \"group\":\n // Groups are UI-only; flatten children into the enclosing schema.\n collectFields(element.elements, properties, required, ctx);\n break;\n\n case \"conditional\":\n // Conditional visibility is UI-only; all fields remain in the schema.\n collectFields(element.elements, properties, required, ctx);\n break;\n\n default: {\n const _exhaustive: never = element;\n void _exhaustive;\n }\n }\n }\n}\n\n// =============================================================================\n// FIELD SCHEMA GENERATION\n// =============================================================================\n\n/**\n * Generates the JSON Schema sub-schema for a single FieldNode.\n */\nfunction generateFieldSchema(field: FieldNode, ctx: GeneratorContext): JsonSchema2020 {\n const schema = generateTypeNode(field.type, ctx);\n\n // Apply constraints. multipleOf:1 on a number type is a special case: it\n // promotes the type to \"integer\" and removes the multipleOf keyword.\n applyConstraints(schema, field.constraints);\n\n // Apply annotations (title, description, default, deprecated, etc.).\n applyAnnotations(schema, field.annotations);\n\n return schema;\n}\n\n// =============================================================================\n// TYPE NODE GENERATION\n// =============================================================================\n\n/**\n * Converts a TypeNode to a JSON Schema sub-schema.\n *\n * This function is intentionally exhaustive — all TypeNode variants are handled.\n * TypeScript's exhaustiveness check via the default branch ensures new variants\n * added to the IR are caught at compile time.\n */\nfunction generateTypeNode(type: TypeNode, ctx: GeneratorContext): JsonSchema2020 {\n switch (type.kind) {\n case \"primitive\":\n return generatePrimitiveType(type);\n\n case \"enum\":\n return generateEnumType(type);\n\n case \"array\":\n return generateArrayType(type, ctx);\n\n case \"object\":\n return generateObjectType(type, ctx);\n\n case \"union\":\n return generateUnionType(type, ctx);\n\n case \"reference\":\n return generateReferenceType(type);\n\n case \"dynamic\":\n return generateDynamicType(type);\n\n case \"custom\":\n return generateCustomType(type);\n\n default: {\n // TypeScript exhaustiveness guard.\n const _exhaustive: never = type;\n return _exhaustive;\n }\n }\n}\n\n/**\n * Maps primitive IR types to JSON Schema type keywords.\n *\n * Note: `integer` is NOT a primitive kind in the IR. Integer semantics are\n * expressed via a `multipleOf: 1` constraint on a number type; `applyConstraints`\n * handles the promotion (per the JSON Schema vocabulary spec §2.1).\n */\nfunction generatePrimitiveType(type: PrimitiveTypeNode): JsonSchema2020 {\n return { type: type.primitiveKind };\n}\n\n/**\n * Generates JSON Schema for a static enum type.\n *\n * When any member has a displayName, the output uses the `oneOf` form with\n * per-member `const`/`title` entries (per the JSON Schema vocabulary spec §2.3). Otherwise the\n * flat `enum` keyword is used (simpler, equally valid).\n */\nfunction generateEnumType(type: EnumTypeNode): JsonSchema2020 {\n const hasDisplayNames = type.members.some((m) => m.displayName !== undefined);\n\n if (hasDisplayNames) {\n return {\n oneOf: type.members.map((m) => {\n const entry: JsonSchema2020 = { const: m.value };\n if (m.displayName !== undefined) {\n entry.title = m.displayName;\n }\n return entry;\n }),\n };\n }\n\n return { enum: type.members.map((m) => m.value) };\n}\n\n/**\n * Generates JSON Schema for an array type.\n * Per 2020-12, `items` is a single schema (not an array); tuple types use\n * `prefixItems` + `items: false`.\n */\nfunction generateArrayType(type: ArrayTypeNode, ctx: GeneratorContext): JsonSchema2020 {\n return {\n type: \"array\",\n items: generateTypeNode(type.items, ctx),\n };\n}\n\n/**\n * Generates JSON Schema for an object type.\n *\n * `additionalProperties` is only emitted when the IR explicitly disallows extra\n * properties. The default per the JSON Schema vocabulary spec §2.5 is to omit it (allow policy).\n */\nfunction generateObjectType(type: ObjectTypeNode, ctx: GeneratorContext): JsonSchema2020 {\n const properties: Record<string, JsonSchema2020> = {};\n const required: string[] = [];\n\n for (const prop of type.properties) {\n properties[prop.name] = generatePropertySchema(prop, ctx);\n if (!prop.optional) {\n required.push(prop.name);\n }\n }\n\n const schema: JsonSchema2020 = { type: \"object\", properties };\n\n if (required.length > 0) {\n schema.required = required;\n }\n\n if (!type.additionalProperties) {\n // IR default is false (closed objects). Emit explicitly when disallowed.\n schema.additionalProperties = false;\n }\n\n return schema;\n}\n\n/**\n * Generates a schema for an ObjectProperty, applying its use-site constraints\n * and annotations (per the JSON Schema vocabulary spec §5.4 — inline allOf at use site).\n */\nfunction generatePropertySchema(prop: ObjectProperty, ctx: GeneratorContext): JsonSchema2020 {\n const schema = generateTypeNode(prop.type, ctx);\n applyConstraints(schema, prop.constraints);\n applyAnnotations(schema, prop.annotations);\n return schema;\n}\n\n/**\n * Generates JSON Schema for a union type.\n *\n * Union handling strategy:\n * - Boolean shorthand: `true | false` → `{ type: \"boolean\" }` (not anyOf)\n * - All other unions → `anyOf` (members may overlap; discriminated union\n * detection is deferred to a future phase per design doc 003 §7.4)\n */\nfunction generateUnionType(type: UnionTypeNode, ctx: GeneratorContext): JsonSchema2020 {\n // Boolean shorthand: union of true-literal and false-literal → type: \"boolean\"\n if (isBooleanUnion(type)) {\n return { type: \"boolean\" };\n }\n\n // Default: anyOf for all non-boolean unions.\n // Discriminated union detection (shared required property with distinct consts)\n // is deferred to a future phase.\n return {\n anyOf: type.members.map((m) => generateTypeNode(m, ctx)),\n };\n}\n\n/**\n * Returns true if the union is `true | false` (boolean shorthand).\n */\nfunction isBooleanUnion(type: UnionTypeNode): boolean {\n if (type.members.length !== 2) return false;\n const kinds = type.members.map((m) => m.kind);\n // Both must be primitives; check if both are \"boolean\" primitives.\n // The IR currently does not have a boolean literal node, so boolean union\n // is represented as two primitive boolean members.\n return (\n kinds.every((k) => k === \"primitive\") &&\n type.members.every((m) => m.kind === \"primitive\" && m.primitiveKind === \"boolean\")\n );\n}\n\n/**\n * Generates JSON Schema for a reference type.\n *\n * The referenced type's schema is stored in `$defs` (seeded from the type\n * registry before traversal begins). The reference simply emits a `$ref`.\n */\nfunction generateReferenceType(type: ReferenceTypeNode): JsonSchema2020 {\n return { $ref: `#/$defs/${type.name}` };\n}\n\n/**\n * Generates JSON Schema for a dynamic type (runtime-resolved enum or schema).\n *\n * Dynamic enums emit `x-formspec-source` and optionally `x-formspec-params`.\n * Dynamic schemas emit `x-formspec-schemaSource` with `additionalProperties: true`\n * since the actual schema is determined at runtime (per the JSON Schema vocabulary spec §3.2).\n */\nfunction generateDynamicType(type: DynamicTypeNode): JsonSchema2020 {\n if (type.dynamicKind === \"enum\") {\n const schema: JsonSchema2020 = {\n type: \"string\",\n \"x-formspec-source\": type.sourceKey,\n };\n if (type.parameterFields.length > 0) {\n schema[\"x-formspec-params\"] = [...type.parameterFields];\n }\n return schema;\n }\n\n // dynamicKind === \"schema\"\n return {\n type: \"object\",\n additionalProperties: true,\n \"x-formspec-schemaSource\": type.sourceKey,\n };\n}\n\n/**\n * CustomTypeNode is a placeholder for Phase 8 extensions.\n * Emits a minimal passthrough object type until the extension API is implemented.\n */\nfunction generateCustomType(_type: CustomTypeNode): JsonSchema2020 {\n return { type: \"object\" };\n}\n\n// =============================================================================\n// CONSTRAINT APPLICATION\n// =============================================================================\n\n/**\n * Applies constraint nodes onto an existing JSON Schema object (mutates in place).\n *\n * All callers pass freshly-created objects so there is no aliasing risk.\n *\n * Special rule (per the JSON Schema vocabulary spec §2.1): `multipleOf: 1` on a `\"number\"` type\n * promotes to `\"integer\"` and suppresses the `multipleOf` keyword (integer is a\n * subtype of number; expressing it via multipleOf:1 is redundant).\n *\n * Path-targeted constraints (e.g., `@minimum :value 0`) are emitted at the field\n * level here; full sub-field targeting via allOf composition is a Phase 4 concern.\n */\nfunction applyConstraints(schema: JsonSchema2020, constraints: readonly ConstraintNode[]): void {\n for (const constraint of constraints) {\n switch (constraint.constraintKind) {\n case \"minimum\":\n schema.minimum = constraint.value;\n break;\n\n case \"maximum\":\n schema.maximum = constraint.value;\n break;\n\n case \"exclusiveMinimum\":\n schema.exclusiveMinimum = constraint.value;\n break;\n\n case \"exclusiveMaximum\":\n schema.exclusiveMaximum = constraint.value;\n break;\n\n case \"multipleOf\": {\n const { value } = constraint;\n if (value === 1 && schema.type === \"number\") {\n // Promote number → integer; omit the multipleOf keyword (redundant).\n schema.type = \"integer\";\n } else {\n schema.multipleOf = value;\n }\n break;\n }\n\n case \"minLength\":\n schema.minLength = constraint.value;\n break;\n\n case \"maxLength\":\n schema.maxLength = constraint.value;\n break;\n\n case \"minItems\":\n schema.minItems = constraint.value;\n break;\n\n case \"maxItems\":\n schema.maxItems = constraint.value;\n break;\n\n case \"pattern\":\n schema.pattern = constraint.pattern;\n break;\n\n case \"uniqueItems\":\n schema.uniqueItems = constraint.value;\n break;\n\n case \"allowedMembers\":\n // EnumMemberConstraintNode — not yet emitted to JSON Schema (Phase 6 validation).\n break;\n\n case \"custom\":\n // CustomConstraintNode — handled by Phase 8 extensions.\n break;\n\n default: {\n // TypeScript exhaustiveness guard.\n const _exhaustive: never = constraint;\n void _exhaustive;\n }\n }\n }\n}\n\n// =============================================================================\n// ANNOTATION APPLICATION\n// =============================================================================\n\n/**\n * Applies annotation nodes onto an existing JSON Schema object (mutates in place).\n *\n * Mapping per the JSON Schema vocabulary spec §2.8:\n * - `displayName` → `title`\n * - `description` → `description`\n * - `defaultValue` → `default`\n * - `deprecated` → `deprecated: true` (2020-12 standard annotation)\n *\n * UI-only annotations (`placeholder`, `formatHint`) are silently ignored here —\n * they belong in the UI Schema, not the data schema.\n */\nfunction applyAnnotations(schema: JsonSchema2020, annotations: readonly AnnotationNode[]): void {\n for (const annotation of annotations) {\n switch (annotation.annotationKind) {\n case \"displayName\":\n schema.title = annotation.value;\n break;\n\n case \"description\":\n schema.description = annotation.value;\n break;\n\n case \"defaultValue\":\n schema.default = annotation.value;\n break;\n\n case \"deprecated\":\n schema.deprecated = true;\n break;\n\n case \"placeholder\":\n // UI-only — belongs in UI Schema, not emitted here.\n break;\n\n case \"formatHint\":\n // UI-only — belongs in UI Schema, not emitted here.\n break;\n\n case \"custom\":\n // CustomAnnotationNode — handled by Phase 8 extensions.\n break;\n\n default: {\n // TypeScript exhaustiveness guard.\n const _exhaustive: never = annotation;\n void _exhaustive;\n }\n }\n }\n}\n","/**\n * JSON Schema generator for FormSpec forms.\n *\n * Routes through the canonical IR pipeline: Chain DSL → FormIR → JSON Schema 2020-12.\n */\n\nimport type { FormElement, FormSpec } from \"@formspec/core\";\nimport { canonicalizeChainDSL } from \"../canonicalize/index.js\";\nimport { generateJsonSchemaFromIR, type JsonSchema2020 } from \"./ir-generator.js\";\n\n/**\n * Generates a JSON Schema 2020-12 from a FormSpec.\n *\n * All generation routes through the canonical IR. The chain DSL is first\n * canonicalized to a FormIR, then the IR-based generator produces the schema.\n *\n * @example\n * ```typescript\n * const form = formspec(\n * field.text(\"name\", { label: \"Name\", required: true }),\n * field.number(\"age\", { min: 0 }),\n * );\n *\n * const schema = generateJsonSchema(form);\n * // {\n * // $schema: \"https://json-schema.org/draft/2020-12/schema\",\n * // type: \"object\",\n * // properties: {\n * // name: { type: \"string\", title: \"Name\" },\n * // age: { type: \"number\", minimum: 0 }\n * // },\n * // required: [\"name\"]\n * // }\n * ```\n *\n * @param form - The FormSpec to convert\n * @returns A JSON Schema 2020-12 object\n */\nexport function generateJsonSchema<E extends readonly FormElement[]>(\n form: FormSpec<E>\n): JsonSchema2020 {\n const ir = canonicalizeChainDSL(form);\n return generateJsonSchemaFromIR(ir);\n}\n","/**\n * Zod schemas for JSON Forms UI Schema.\n *\n * These schemas are the source of truth for UI Schema validation.\n * TypeScript types are derived from these schemas via `z.infer<>`.\n *\n * @see https://jsonforms.io/docs/uischema/\n */\n\nimport { z } from \"zod\";\n\n// =============================================================================\n// Primitive helpers\n// =============================================================================\n\n/** JSON Pointer string (e.g., \"#/properties/fieldName\") */\nconst jsonPointerSchema = z.string();\n\n// =============================================================================\n// Rule Effect and Element Type enums\n// =============================================================================\n\n/**\n * Zod schema for rule effect values.\n */\nexport const ruleEffectSchema = z.enum([\"SHOW\", \"HIDE\", \"ENABLE\", \"DISABLE\"]);\n\n/**\n * Rule effect types for conditional visibility.\n */\nexport type RuleEffect = z.infer<typeof ruleEffectSchema>;\n\n/**\n * Zod schema for UI Schema element type strings.\n */\nexport const uiSchemaElementTypeSchema = z.enum([\n \"Control\",\n \"VerticalLayout\",\n \"HorizontalLayout\",\n \"Group\",\n \"Categorization\",\n \"Category\",\n \"Label\",\n]);\n\n/**\n * UI Schema element types.\n */\nexport type UISchemaElementType = z.infer<typeof uiSchemaElementTypeSchema>;\n\n// =============================================================================\n// Rule Condition Schema (recursive)\n// =============================================================================\n\n// Forward-declare the recursive TypeScript type.\n// We use an interface here (rather than z.infer<>) because the recursive\n// z.lazy() type annotation requires us to pre-declare the shape.\n/**\n * JSON Schema subset used in rule conditions.\n */\nexport interface RuleConditionSchema {\n const?: unknown;\n enum?: readonly unknown[];\n type?: string;\n not?: RuleConditionSchema;\n minimum?: number;\n maximum?: number;\n exclusiveMinimum?: number;\n exclusiveMaximum?: number;\n minLength?: number;\n properties?: Record<string, RuleConditionSchema>;\n required?: string[];\n allOf?: RuleConditionSchema[];\n}\n\n// Build the Zod schema referencing the pre-declared interface.\n// We use z.ZodType<RuleConditionSchema> so the recursive reference works.\n// The interface uses `?` (exact optional), and z.ZodType checks output only,\n// so the optional fields (which Zod infers as `T | undefined`) are compatible\n// because `T | undefined` is assignable to the optional field slot.\n//\n// @ts-expect-error -- exactOptionalPropertyTypes: the Zod output type for optional\n// fields is `T | undefined`, but our interface uses `?` (exact optional, key may\n// be absent). This is a known mismatch when using z.ZodType<T> with\n// exactOptionalPropertyTypes:true; the runtime behavior is correct.\nexport const ruleConditionSchema: z.ZodType<RuleConditionSchema> = z.lazy(() =>\n z\n .object({\n const: z.unknown().optional(),\n enum: z.array(z.unknown()).readonly().optional(),\n type: z.string().optional(),\n not: ruleConditionSchema.optional(),\n minimum: z.number().optional(),\n maximum: z.number().optional(),\n exclusiveMinimum: z.number().optional(),\n exclusiveMaximum: z.number().optional(),\n minLength: z.number().optional(),\n properties: z.record(z.string(), ruleConditionSchema).optional(),\n required: z.array(z.string()).optional(),\n allOf: z.array(ruleConditionSchema).optional(),\n })\n .strict()\n);\n\n// =============================================================================\n// Schema-Based Condition and Rule\n// =============================================================================\n\n/**\n * Zod schema for a schema-based rule condition.\n */\nexport const schemaBasedConditionSchema = z\n .object({\n scope: jsonPointerSchema,\n schema: ruleConditionSchema,\n })\n .strict();\n\n/**\n * Condition for a rule.\n */\nexport type SchemaBasedCondition = z.infer<typeof schemaBasedConditionSchema>;\n\n/**\n * Zod schema for a UI Schema rule.\n */\nexport const ruleSchema = z\n .object({\n effect: ruleEffectSchema,\n condition: schemaBasedConditionSchema,\n })\n .strict();\n\n/**\n * Rule for conditional element visibility/enablement.\n */\nexport type Rule = z.infer<typeof ruleSchema>;\n\n// =============================================================================\n// UI Schema Element Schemas (recursive via z.lazy)\n// =============================================================================\n\n// Forward-declare UISchemaElement so layout schemas can reference it.\n// We declare the type up-front and wire the Zod schema below.\n/**\n * Union of all UI Schema element types.\n */\nexport type UISchemaElement =\n | ControlElement\n | VerticalLayout\n | HorizontalLayout\n | GroupLayout\n | Categorization\n | Category\n | LabelElement;\n\n// The Zod schema for UISchemaElement is defined as a const using z.lazy(),\n// which defers evaluation until first use. This allows all element schemas\n// below to be referenced even though they are declared after this line.\nexport const uiSchemaElementSchema: z.ZodType<UISchemaElement> = z.lazy(() =>\n z.union([\n controlSchema,\n verticalLayoutSchema,\n horizontalLayoutSchema,\n groupLayoutSchema,\n categorizationSchema,\n categorySchema,\n labelElementSchema,\n ])\n) as z.ZodType<UISchemaElement>;\n\n// -----------------------------------------------------------------------------\n// Control\n// -----------------------------------------------------------------------------\n\n/**\n * Zod schema for a Control element.\n */\nexport const controlSchema = z\n .object({\n type: z.literal(\"Control\"),\n scope: jsonPointerSchema,\n label: z.union([z.string(), z.literal(false)]).optional(),\n rule: ruleSchema.optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n })\n .passthrough();\n\n/**\n * A Control element that binds to a JSON Schema property.\n */\nexport type ControlElement = z.infer<typeof controlSchema>;\n\n// -----------------------------------------------------------------------------\n// VerticalLayout\n// -----------------------------------------------------------------------------\n\n// Pre-declare the interface so the Zod schema can reference UISchemaElement.\n/**\n * A vertical layout element.\n */\nexport interface VerticalLayout {\n type: \"VerticalLayout\";\n elements: UISchemaElement[];\n rule?: Rule | undefined;\n options?: Record<string, unknown> | undefined;\n [k: string]: unknown;\n}\n\nexport const verticalLayoutSchema: z.ZodType<VerticalLayout> = z.lazy(() =>\n z\n .object({\n type: z.literal(\"VerticalLayout\"),\n elements: z.array(uiSchemaElementSchema),\n rule: ruleSchema.optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n })\n .passthrough()\n);\n\n// -----------------------------------------------------------------------------\n// HorizontalLayout\n// -----------------------------------------------------------------------------\n\n/**\n * A horizontal layout element.\n */\nexport interface HorizontalLayout {\n type: \"HorizontalLayout\";\n elements: UISchemaElement[];\n rule?: Rule | undefined;\n options?: Record<string, unknown> | undefined;\n [k: string]: unknown;\n}\n\nexport const horizontalLayoutSchema: z.ZodType<HorizontalLayout> = z.lazy(() =>\n z\n .object({\n type: z.literal(\"HorizontalLayout\"),\n elements: z.array(uiSchemaElementSchema),\n rule: ruleSchema.optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n })\n .passthrough()\n);\n\n// -----------------------------------------------------------------------------\n// GroupLayout\n// -----------------------------------------------------------------------------\n\n/**\n * A group element with a label.\n */\nexport interface GroupLayout {\n type: \"Group\";\n label: string;\n elements: UISchemaElement[];\n rule?: Rule | undefined;\n options?: Record<string, unknown> | undefined;\n [k: string]: unknown;\n}\n\nexport const groupLayoutSchema: z.ZodType<GroupLayout> = z.lazy(() =>\n z\n .object({\n type: z.literal(\"Group\"),\n label: z.string(),\n elements: z.array(uiSchemaElementSchema),\n rule: ruleSchema.optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n })\n .passthrough()\n);\n\n// -----------------------------------------------------------------------------\n// Category\n// -----------------------------------------------------------------------------\n\n/**\n * A Category element, used inside a Categorization layout.\n */\nexport interface Category {\n type: \"Category\";\n label: string;\n elements: UISchemaElement[];\n rule?: Rule | undefined;\n options?: Record<string, unknown> | undefined;\n [k: string]: unknown;\n}\n\nexport const categorySchema: z.ZodType<Category> = z.lazy(() =>\n z\n .object({\n type: z.literal(\"Category\"),\n label: z.string(),\n elements: z.array(uiSchemaElementSchema),\n rule: ruleSchema.optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n })\n .passthrough()\n);\n\n// -----------------------------------------------------------------------------\n// Categorization\n// -----------------------------------------------------------------------------\n\n/**\n * A Categorization element (tab-based layout).\n */\nexport interface Categorization {\n type: \"Categorization\";\n elements: Category[];\n label?: string | undefined;\n rule?: Rule | undefined;\n options?: Record<string, unknown> | undefined;\n [k: string]: unknown;\n}\n\nexport const categorizationSchema: z.ZodType<Categorization> = z.lazy(() =>\n z\n .object({\n type: z.literal(\"Categorization\"),\n elements: z.array(categorySchema),\n label: z.string().optional(),\n rule: ruleSchema.optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n })\n .passthrough()\n);\n\n// -----------------------------------------------------------------------------\n// LabelElement\n// -----------------------------------------------------------------------------\n\n/**\n * Zod schema for a Label element.\n */\nexport const labelElementSchema = z\n .object({\n type: z.literal(\"Label\"),\n text: z.string(),\n rule: ruleSchema.optional(),\n options: z.record(z.string(), z.unknown()).optional(),\n })\n .passthrough();\n\n/**\n * A Label element for displaying static text.\n */\nexport type LabelElement = z.infer<typeof labelElementSchema>;\n\n// =============================================================================\n// Root UISchema\n// =============================================================================\n\n/**\n * Root UI Schema (always a layout — not a Control, Category, or Label).\n */\nexport type UISchema = VerticalLayout | HorizontalLayout | GroupLayout | Categorization;\n\n/**\n * Zod schema for the root UI Schema (layout types only).\n */\nexport const uiSchema: z.ZodType<UISchema> = z.lazy(() =>\n z.union([verticalLayoutSchema, horizontalLayoutSchema, groupLayoutSchema, categorizationSchema])\n) as z.ZodType<UISchema>;\n","/**\n * JSON Forms UI Schema generator that operates on the canonical FormIR.\n *\n * This generator consumes the IR produced by the Canonicalize phase and\n * produces a JSON Forms UI Schema. All downstream UI Schema generation\n * should use this module for UI Schema generation.\n */\n\nimport type { FormIR, FormIRElement, FieldNode, GroupLayoutNode } from \"@formspec/core\";\nimport type { UISchema, UISchemaElement, ControlElement, GroupLayout, Rule } from \"./types.js\";\nimport { uiSchema as uiSchemaValidator } from \"./schema.js\";\nimport { z } from \"zod\";\n\n// =============================================================================\n// HELPERS\n// =============================================================================\n\n/**\n * Parses a value through a Zod schema, converting validation errors to a\n * descriptive Error.\n */\nfunction parseOrThrow<T>(schema: z.ZodType<T>, value: unknown, label: string): T {\n try {\n return schema.parse(value);\n } catch (error) {\n if (error instanceof z.ZodError) {\n throw new Error(\n `Generated ${label} failed validation:\\n${error.issues.map((i) => ` ${i.path.join(\".\")}: ${i.message}`).join(\"\\n\")}`\n );\n }\n throw error;\n }\n}\n\n/**\n * Converts a field name to a JSON Pointer scope string.\n */\nfunction fieldToScope(fieldName: string): string {\n return `#/properties/${fieldName}`;\n}\n\n/**\n * Creates a SHOW rule for a single conditional field/value pair.\n */\nfunction createShowRule(fieldName: string, value: unknown): Rule {\n return {\n effect: \"SHOW\",\n condition: {\n scope: fieldToScope(fieldName),\n schema: { const: value },\n },\n };\n}\n\n/**\n * Combines two SHOW rules into a single rule using an allOf condition.\n *\n * When elements are nested inside multiple conditionals, all parent conditions\n * must be met for the element to be visible. This function merges the two\n * conditions into a single rule using allOf so that JSON Forms evaluates\n * both predicates simultaneously.\n */\nfunction combineRules(parentRule: Rule, childRule: Rule): Rule {\n const parentCondition = parentRule.condition;\n const childCondition = childRule.condition;\n\n return {\n effect: \"SHOW\",\n condition: {\n scope: \"#\",\n schema: {\n allOf: [\n {\n properties: {\n [parentCondition.scope.replace(\"#/properties/\", \"\")]: parentCondition.schema,\n },\n },\n {\n properties: {\n [childCondition.scope.replace(\"#/properties/\", \"\")]: childCondition.schema,\n },\n },\n ],\n },\n },\n };\n}\n\n// =============================================================================\n// ELEMENT CONVERSION\n// =============================================================================\n\n/**\n * Converts a FieldNode from the IR to a ControlElement.\n *\n * The label is sourced from the first `displayName` annotation on the field,\n * matching how the chain DSL propagates the `label` option through the\n * canonicalization phase.\n */\nfunction fieldNodeToControl(field: FieldNode, parentRule?: Rule): ControlElement {\n const displayNameAnnotation = field.annotations.find((a) => a.annotationKind === \"displayName\");\n\n const control: ControlElement = {\n type: \"Control\",\n scope: fieldToScope(field.name),\n ...(displayNameAnnotation !== undefined && { label: displayNameAnnotation.value }),\n ...(parentRule !== undefined && { rule: parentRule }),\n };\n\n return control;\n}\n\n/**\n * Converts a GroupLayoutNode from the IR to a GroupLayout element.\n *\n * The group's children are recursively converted; the optional parent rule is\n * forwarded to nested elements so that a group inside a conditional inherits\n * the visibility rule.\n */\nfunction groupNodeToLayout(group: GroupLayoutNode, parentRule?: Rule): GroupLayout {\n return {\n type: \"Group\",\n label: group.label,\n elements: irElementsToUiSchema(group.elements, parentRule),\n ...(parentRule !== undefined && { rule: parentRule }),\n };\n}\n\n/**\n * Converts an array of IR elements to UI Schema elements.\n *\n * @param elements - The IR elements to convert\n * @param parentRule - Optional rule inherited from a parent ConditionalLayoutNode\n * @returns Array of UI Schema elements\n */\nfunction irElementsToUiSchema(\n elements: readonly FormIRElement[],\n parentRule?: Rule\n): UISchemaElement[] {\n const result: UISchemaElement[] = [];\n\n for (const element of elements) {\n switch (element.kind) {\n case \"field\": {\n result.push(fieldNodeToControl(element, parentRule));\n break;\n }\n\n case \"group\": {\n result.push(groupNodeToLayout(element, parentRule));\n break;\n }\n\n case \"conditional\": {\n // Build the rule for this conditional level.\n const newRule = createShowRule(element.fieldName, element.value);\n // Combine with the inherited parent rule for nested conditionals.\n const combinedRule = parentRule !== undefined ? combineRules(parentRule, newRule) : newRule;\n // Children are flattened into the parent container with the combined\n // rule attached.\n const childElements = irElementsToUiSchema(element.elements, combinedRule);\n result.push(...childElements);\n break;\n }\n\n default: {\n const _exhaustive: never = element;\n void _exhaustive;\n throw new Error(\"Unhandled IR element kind\");\n }\n }\n }\n\n return result;\n}\n\n// =============================================================================\n// PUBLIC API\n// =============================================================================\n\n/**\n * Generates a JSON Forms UI Schema from a canonical `FormIR`.\n *\n * Mapping rules:\n * - `FieldNode` → `ControlElement` with `scope: \"#/properties/<name>\"`\n * - `displayName` annotation → `label` on the `ControlElement`\n * - `GroupLayoutNode` → `GroupLayout` with recursively converted `elements`\n * - `ConditionalLayoutNode` → children flattened with a `SHOW` rule\n * - Nested conditionals → combined `allOf` rule\n * - Root wrapper is always `{ type: \"VerticalLayout\", elements: [...] }`\n *\n * @example\n * ```typescript\n * const ir = canonicalizeDSL(\n * formspec(\n * group(\"Customer\", field.text(\"name\", { label: \"Name\" })),\n * when(is(\"status\", \"draft\"), field.text(\"notes\", { label: \"Notes\" })),\n * )\n * );\n *\n * const uiSchema = generateUiSchemaFromIR(ir);\n * // {\n * // type: \"VerticalLayout\",\n * // elements: [\n * // {\n * // type: \"Group\",\n * // label: \"Customer\",\n * // elements: [{ type: \"Control\", scope: \"#/properties/name\", label: \"Name\" }]\n * // },\n * // {\n * // type: \"Control\",\n * // scope: \"#/properties/notes\",\n * // label: \"Notes\",\n * // rule: { effect: \"SHOW\", condition: { scope: \"#/properties/status\", schema: { const: \"draft\" } } }\n * // }\n * // ]\n * // }\n * ```\n *\n * @param ir - The canonical FormIR produced by the Canonicalize phase\n * @returns A validated JSON Forms UI Schema\n */\nexport function generateUiSchemaFromIR(ir: FormIR): UISchema {\n const result: UISchema = {\n type: \"VerticalLayout\",\n elements: irElementsToUiSchema(ir.elements),\n };\n\n return parseOrThrow(uiSchemaValidator, result, \"UI Schema\");\n}\n","/**\n * JSON Forms UI Schema generator for FormSpec forms.\n *\n * Routes through the canonical IR pipeline: Chain DSL → FormIR → UI Schema.\n */\n\nimport type { FormElement, FormSpec } from \"@formspec/core\";\nimport { canonicalizeChainDSL } from \"../canonicalize/index.js\";\nimport { generateUiSchemaFromIR } from \"./ir-generator.js\";\nimport type { UISchema } from \"./types.js\";\n\n/**\n * Generates a JSON Forms UI Schema from a FormSpec.\n *\n * All generation routes through the canonical IR. The chain DSL is first\n * canonicalized to a FormIR, then the IR-based generator produces the schema.\n *\n * @example\n * ```typescript\n * const form = formspec(\n * group(\"Customer\",\n * field.text(\"name\", { label: \"Name\" }),\n * ),\n * when(\"status\", \"draft\",\n * field.text(\"notes\", { label: \"Notes\" }),\n * ),\n * );\n *\n * const uiSchema = generateUiSchema(form);\n * // {\n * // type: \"VerticalLayout\",\n * // elements: [\n * // {\n * // type: \"Group\",\n * // label: \"Customer\",\n * // elements: [\n * // { type: \"Control\", scope: \"#/properties/name\", label: \"Name\" }\n * // ]\n * // },\n * // {\n * // type: \"Control\",\n * // scope: \"#/properties/notes\",\n * // label: \"Notes\",\n * // rule: {\n * // effect: \"SHOW\",\n * // condition: { scope: \"#/properties/status\", schema: { const: \"draft\" } }\n * // }\n * // }\n * // ]\n * // }\n * ```\n *\n * @param form - The FormSpec to convert\n * @returns A JSON Forms UI Schema\n */\nexport function generateUiSchema<E extends readonly FormElement[]>(form: FormSpec<E>): UISchema {\n const ir = canonicalizeChainDSL(form);\n return generateUiSchemaFromIR(ir);\n}\n","/**\n * JSON Schema type definitions.\n *\n * These types are a subset of JSON Schema sufficient for form generation.\n */\n\n/**\n * JSON Schema primitive types.\n */\nexport type JSONSchemaType =\n | \"string\"\n | \"number\"\n | \"integer\"\n | \"boolean\"\n | \"object\"\n | \"array\"\n | \"null\";\n\n/**\n * A JSON Schema definition (legacy subset used by Zod validator and types.ts).\n */\nexport interface JSONSchema7 {\n $schema?: string;\n $id?: string;\n $ref?: string;\n\n // Metadata\n title?: string;\n description?: string;\n deprecated?: boolean;\n\n // Type\n type?: JSONSchemaType | JSONSchemaType[];\n\n // String validation\n minLength?: number;\n maxLength?: number;\n pattern?: string;\n\n // Number validation\n minimum?: number;\n maximum?: number;\n exclusiveMinimum?: number;\n exclusiveMaximum?: number;\n\n // Enum\n enum?: readonly (string | number | boolean | null)[];\n const?: string | number | boolean | null;\n\n // Object\n properties?: Record<string, JSONSchema7>;\n required?: string[];\n additionalProperties?: boolean | JSONSchema7;\n\n // Array\n items?: JSONSchema7 | JSONSchema7[];\n minItems?: number;\n maxItems?: number;\n\n // Composition\n allOf?: JSONSchema7[];\n anyOf?: JSONSchema7[];\n oneOf?: JSONSchema7[];\n not?: JSONSchema7;\n\n // Conditional\n if?: JSONSchema7;\n then?: JSONSchema7;\n else?: JSONSchema7;\n\n // Format\n format?: string;\n\n // Default\n default?: unknown;\n\n // =============================================================================\n // FormSpec Extensions (x- prefixed)\n // =============================================================================\n\n /**\n * Data source key for dynamic enum fields.\n * Indicates that options should be fetched from a registered resolver.\n */\n \"x-formspec-source\"?: string;\n\n /**\n * Field names whose values are needed to fetch dynamic enum options.\n * Used for dependent/cascading dropdowns.\n */\n \"x-formspec-params\"?: readonly string[];\n\n /**\n * Schema source identifier for dynamic schema fields.\n * Indicates that the schema should be loaded dynamically at runtime.\n */\n \"x-formspec-schemaSource\"?: string;\n}\n\n/** Extension properties for custom FormSpec decorators. */\nexport type FormSpecSchemaExtensions = Record<`x-formspec-${string}`, unknown>;\n\n/** JSON Schema with FormSpec extension properties for arbitrary x-formspec-* keys. */\nexport type ExtendedJSONSchema7 = JSONSchema7 & FormSpecSchemaExtensions;\n\n/**\n * Sets a FormSpec extension property on a JSON Schema node.\n *\n * Use this to safely add `x-formspec-*` properties to any schema,\n * including nested schemas typed as `JSONSchema7` (which don't carry\n * the extension index signature).\n *\n * @param schema - Any JSON Schema node\n * @param key - Extension key (must start with `x-formspec-`)\n * @param value - Extension value\n */\nexport function setSchemaExtension(\n schema: object,\n key: `x-formspec-${string}`,\n value: unknown\n): void {\n (schema as Record<string, unknown>)[key] = value;\n}\n\n/**\n * Reads a FormSpec extension property from a JSON Schema node.\n *\n * Accepts any schema object — `JSONSchema7`, `JsonSchema2020`, `ExtendedJSONSchema7`, etc.\n *\n * @param schema - Any JSON Schema node\n * @param key - Extension key (must start with `x-formspec-`)\n * @returns The extension value, or `undefined` if not present\n */\nexport function getSchemaExtension(schema: object, key: `x-formspec-${string}`): unknown {\n return (schema as Record<string, unknown>)[key];\n}\n","/**\n * Zod schemas for JSON Schema output validation.\n *\n * These schemas cover the subset of JSON Schema that FormSpec generates,\n * plus the FormSpec-specific `x-formspec-*` extension properties.\n *\n * @see https://json-schema.org/draft/2020-12/schema\n */\n\nimport { z } from \"zod\";\nimport type { JSONSchema7 } from \"./types.js\";\n\n// =============================================================================\n// JSON Schema type enum\n// =============================================================================\n\n/**\n * Zod schema for JSON Schema primitive type strings.\n */\nexport const jsonSchemaTypeSchema = z.enum([\n \"string\",\n \"number\",\n \"integer\",\n \"boolean\",\n \"object\",\n \"array\",\n \"null\",\n]);\n\n// =============================================================================\n// JSON Schema validator schema (recursive)\n// =============================================================================\n\n// We annotate with z.ZodType<JSONSchema7> for the recursive self-reference.\n// The @ts-expect-error is required because exactOptionalPropertyTypes:true causes\n// Zod's inferred output type for optional fields (`T | undefined`) to be\n// incompatible with the JSONSchema7 interface's exact optional fields (`T?`).\n// The runtime behavior is correct: z.optional() will strip `undefined` values\n// during parsing and correctly handle absent keys.\n//\n// @ts-expect-error -- exactOptionalPropertyTypes: Zod optional infers `T | undefined`\n// but JSONSchema7 uses exact optional `?:` which disallows explicit undefined.\nexport const jsonSchema7Schema: z.ZodType<JSONSchema7> = z.lazy(() =>\n z\n .object({\n $schema: z.string().optional(),\n $id: z.string().optional(),\n $ref: z.string().optional(),\n\n // Metadata\n title: z.string().optional(),\n description: z.string().optional(),\n deprecated: z.boolean().optional(),\n\n // Type\n type: z.union([jsonSchemaTypeSchema, z.array(jsonSchemaTypeSchema)]).optional(),\n\n // String validation\n minLength: z.number().optional(),\n maxLength: z.number().optional(),\n pattern: z.string().optional(),\n\n // Number validation\n minimum: z.number().optional(),\n maximum: z.number().optional(),\n exclusiveMinimum: z.number().optional(),\n exclusiveMaximum: z.number().optional(),\n\n // Enum\n enum: z\n .array(z.union([z.string(), z.number(), z.boolean(), z.null()]))\n .readonly()\n .optional(),\n const: z.union([z.string(), z.number(), z.boolean(), z.null()]).optional(),\n\n // Object\n properties: z.record(z.string(), jsonSchema7Schema).optional(),\n required: z.array(z.string()).optional(),\n additionalProperties: z.union([z.boolean(), jsonSchema7Schema]).optional(),\n\n // Array\n items: z.union([jsonSchema7Schema, z.array(jsonSchema7Schema)]).optional(),\n minItems: z.number().optional(),\n maxItems: z.number().optional(),\n\n // Composition\n allOf: z.array(jsonSchema7Schema).optional(),\n anyOf: z.array(jsonSchema7Schema).optional(),\n oneOf: z.array(jsonSchema7Schema).optional(),\n not: jsonSchema7Schema.optional(),\n\n // Conditional\n if: jsonSchema7Schema.optional(),\n then: jsonSchema7Schema.optional(),\n else: jsonSchema7Schema.optional(),\n\n // Format\n format: z.string().optional(),\n\n // Default\n default: z.unknown().optional(),\n\n // FormSpec extensions\n \"x-formspec-source\": z.string().optional(),\n \"x-formspec-params\": z.array(z.string()).readonly().optional(),\n \"x-formspec-schemaSource\": z.string().optional(),\n })\n // passthrough preserves arbitrary x-formspec-* extension properties\n // added by custom decorators without causing validation failures\n .passthrough()\n);\n","/**\n * Constraint validator for the FormSpec IR.\n *\n * Performs the Validate pipeline phase:\n * - Contradiction detection between paired constraints\n * - Type applicability checks (e.g. numeric constraints on string fields)\n * - Custom constraint type applicability (when extension registry is provided)\n * - Unknown extension warnings (when a registry is provided)\n *\n * @packageDocumentation\n */\n\nimport type {\n FormIR,\n FormIRElement,\n FieldNode,\n TypeNode,\n ConstraintNode,\n NumericConstraintNode,\n LengthConstraintNode,\n EnumMemberConstraintNode,\n Provenance,\n ObjectProperty,\n} from \"@formspec/core\";\nimport type { ExtensionRegistry } from \"../extensions/index.js\";\n\n// =============================================================================\n// PUBLIC API TYPES\n// =============================================================================\n\n/**\n * A structured diagnostic produced by constraint validation.\n *\n * The `code` follows the format: `{VENDOR}-{CATEGORY}-{NNN}`.\n * - VENDOR defaults to \"FORMSPEC\" (configurable via `vendorPrefix`).\n * - Categories: CONTRADICTION, TYPE_MISMATCH, UNKNOWN_EXTENSION\n */\nexport interface ValidationDiagnostic {\n readonly code: string;\n readonly message: string;\n readonly severity: \"error\" | \"warning\";\n /** Location of the primary constraint involved in the violation. */\n readonly primaryLocation: Provenance;\n /** Related locations (e.g., the other side of a contradiction pair). */\n readonly relatedLocations: readonly Provenance[];\n}\n\n/** Result of validating a {@link FormIR}. */\nexport interface ValidationResult {\n readonly diagnostics: readonly ValidationDiagnostic[];\n /** `true` if there are no error-severity diagnostics (warnings are OK). */\n readonly valid: boolean;\n}\n\n/** Options for constraint validation. */\nexport interface ValidateIROptions {\n /**\n * Vendor prefix used when constructing diagnostic codes.\n * @defaultValue \"FORMSPEC\"\n */\n readonly vendorPrefix?: string;\n /**\n * Extension registry for resolving custom constraint type applicability.\n * When provided, custom constraints with `applicableTypes` will be\n * validated against the field's type node kind. Custom constraints\n * whose IDs are absent from this registry emit a WARNING (UNKNOWN_EXTENSION).\n * When omitted, custom constraints are silently skipped.\n */\n readonly extensionRegistry?: ExtensionRegistry;\n}\n\n\n// =============================================================================\n// CONTEXT\n// =============================================================================\n\n/** Mutable accumulator threaded through the validation walk. */\ninterface ValidationContext {\n readonly diagnostics: ValidationDiagnostic[];\n readonly vendorPrefix: string;\n readonly extensionRegistry: ExtensionRegistry | undefined;\n}\n\n// =============================================================================\n// DIAGNOSTIC FACTORIES\n// =============================================================================\n\ntype DiagnosticCategory = \"CONTRADICTION\" | \"TYPE_MISMATCH\" | \"UNKNOWN_EXTENSION\";\n\nfunction makeCode(ctx: ValidationContext, category: DiagnosticCategory, number: number): string {\n return `${ctx.vendorPrefix}-${category}-${String(number).padStart(3, \"0\")}`;\n}\n\nfunction addContradiction(\n ctx: ValidationContext,\n message: string,\n primary: Provenance,\n related: Provenance\n): void {\n ctx.diagnostics.push({\n code: makeCode(ctx, \"CONTRADICTION\", 1),\n message,\n severity: \"error\",\n primaryLocation: primary,\n relatedLocations: [related],\n });\n}\n\nfunction addTypeMismatch(\n ctx: ValidationContext,\n message: string,\n primary: Provenance\n): void {\n ctx.diagnostics.push({\n code: makeCode(ctx, \"TYPE_MISMATCH\", 1),\n message,\n severity: \"error\",\n primaryLocation: primary,\n relatedLocations: [],\n });\n}\n\nfunction addUnknownExtension(\n ctx: ValidationContext,\n message: string,\n primary: Provenance\n): void {\n ctx.diagnostics.push({\n code: makeCode(ctx, \"UNKNOWN_EXTENSION\", 1),\n message,\n severity: \"warning\",\n primaryLocation: primary,\n relatedLocations: [],\n });\n}\n\n// =============================================================================\n// CONSTRAINT NARROWING HELPERS\n// =============================================================================\n\n/** Extract the first numeric constraint with the given kind, if present. */\nfunction findNumeric(\n constraints: readonly ConstraintNode[],\n constraintKind: NumericConstraintNode[\"constraintKind\"]\n): NumericConstraintNode | undefined {\n return constraints.find(\n (c): c is NumericConstraintNode => c.constraintKind === constraintKind\n );\n}\n\n/** Extract the first length constraint with the given kind, if present. */\nfunction findLength(\n constraints: readonly ConstraintNode[],\n constraintKind: LengthConstraintNode[\"constraintKind\"]\n): LengthConstraintNode | undefined {\n return constraints.find(\n (c): c is LengthConstraintNode => c.constraintKind === constraintKind\n );\n}\n\n/** Extract all allowedMembers constraints. */\nfunction findAllowedMembers(\n constraints: readonly ConstraintNode[]\n): readonly EnumMemberConstraintNode[] {\n return constraints.filter(\n (c): c is EnumMemberConstraintNode => c.constraintKind === \"allowedMembers\"\n );\n}\n\n// =============================================================================\n// CONTRADICTION DETECTION\n// =============================================================================\n\nfunction checkNumericContradictions(\n ctx: ValidationContext,\n fieldName: string,\n constraints: readonly ConstraintNode[]\n): void {\n const min = findNumeric(constraints, \"minimum\");\n const max = findNumeric(constraints, \"maximum\");\n const exMin = findNumeric(constraints, \"exclusiveMinimum\");\n const exMax = findNumeric(constraints, \"exclusiveMaximum\");\n\n // minimum > maximum\n if (min !== undefined && max !== undefined && min.value > max.value) {\n addContradiction(\n ctx,\n `Field \"${fieldName}\": minimum (${String(min.value)}) is greater than maximum (${String(max.value)})`,\n min.provenance,\n max.provenance\n );\n }\n\n // exclusiveMinimum >= maximum\n if (exMin !== undefined && max !== undefined && exMin.value >= max.value) {\n addContradiction(\n ctx,\n `Field \"${fieldName}\": exclusiveMinimum (${String(exMin.value)}) is greater than or equal to maximum (${String(max.value)})`,\n exMin.provenance,\n max.provenance\n );\n }\n\n // minimum >= exclusiveMaximum\n if (min !== undefined && exMax !== undefined && min.value >= exMax.value) {\n addContradiction(\n ctx,\n `Field \"${fieldName}\": minimum (${String(min.value)}) is greater than or equal to exclusiveMaximum (${String(exMax.value)})`,\n min.provenance,\n exMax.provenance\n );\n }\n\n // exclusiveMinimum >= exclusiveMaximum\n if (exMin !== undefined && exMax !== undefined && exMin.value >= exMax.value) {\n addContradiction(\n ctx,\n `Field \"${fieldName}\": exclusiveMinimum (${String(exMin.value)}) is greater than or equal to exclusiveMaximum (${String(exMax.value)})`,\n exMin.provenance,\n exMax.provenance\n );\n }\n}\n\nfunction checkLengthContradictions(\n ctx: ValidationContext,\n fieldName: string,\n constraints: readonly ConstraintNode[]\n): void {\n const minLen = findLength(constraints, \"minLength\");\n const maxLen = findLength(constraints, \"maxLength\");\n\n if (minLen !== undefined && maxLen !== undefined && minLen.value > maxLen.value) {\n addContradiction(\n ctx,\n `Field \"${fieldName}\": minLength (${String(minLen.value)}) is greater than maxLength (${String(maxLen.value)})`,\n minLen.provenance,\n maxLen.provenance\n );\n }\n\n const minItems = findLength(constraints, \"minItems\");\n const maxItems = findLength(constraints, \"maxItems\");\n\n if (minItems !== undefined && maxItems !== undefined && minItems.value > maxItems.value) {\n addContradiction(\n ctx,\n `Field \"${fieldName}\": minItems (${String(minItems.value)}) is greater than maxItems (${String(maxItems.value)})`,\n minItems.provenance,\n maxItems.provenance\n );\n }\n}\n\nfunction checkAllowedMembersContradiction(\n ctx: ValidationContext,\n fieldName: string,\n constraints: readonly ConstraintNode[]\n): void {\n const members = findAllowedMembers(constraints);\n if (members.length < 2) return;\n\n // Intersect all allowedMembers sets; if empty — contradiction\n const firstSet = new Set(members[0]?.members ?? []);\n for (let i = 1; i < members.length; i++) {\n const current = members[i];\n if (current === undefined) continue;\n for (const m of firstSet) {\n if (!current.members.includes(m)) {\n firstSet.delete(m);\n }\n }\n }\n\n if (firstSet.size === 0) {\n const first = members[0];\n const second = members[1];\n if (first !== undefined && second !== undefined) {\n addContradiction(\n ctx,\n `Field \"${fieldName}\": allowedMembers constraints have an empty intersection (no valid values remain)`,\n first.provenance,\n second.provenance\n );\n }\n }\n}\n\n// =============================================================================\n// TYPE APPLICABILITY CHECKS\n// =============================================================================\n\n/** Return a readable label for a type node for use in diagnostics. */\nfunction typeLabel(type: TypeNode): string {\n switch (type.kind) {\n case \"primitive\":\n return type.primitiveKind;\n case \"enum\":\n return \"enum\";\n case \"array\":\n return \"array\";\n case \"object\":\n return \"object\";\n case \"union\":\n return \"union\";\n case \"reference\":\n return `reference(${type.name})`;\n case \"dynamic\":\n return `dynamic(${type.dynamicKind})`;\n case \"custom\":\n return `custom(${type.typeId})`;\n default: {\n const _exhaustive: never = type;\n return String(_exhaustive);\n }\n }\n}\n\nfunction checkTypeApplicability(\n ctx: ValidationContext,\n fieldName: string,\n type: TypeNode,\n constraints: readonly ConstraintNode[]\n): void {\n const isNumber = type.kind === \"primitive\" && type.primitiveKind === \"number\";\n const isString = type.kind === \"primitive\" && type.primitiveKind === \"string\";\n const isArray = type.kind === \"array\";\n const isEnum = type.kind === \"enum\";\n\n const label = typeLabel(type);\n\n for (const constraint of constraints) {\n const ck = constraint.constraintKind;\n\n switch (ck) {\n case \"minimum\":\n case \"maximum\":\n case \"exclusiveMinimum\":\n case \"exclusiveMaximum\":\n case \"multipleOf\": {\n if (!isNumber) {\n addTypeMismatch(\n ctx,\n `Field \"${fieldName}\": constraint \"${ck}\" is only valid on number fields, but field type is \"${label}\"`,\n constraint.provenance\n );\n }\n break;\n }\n case \"minLength\":\n case \"maxLength\":\n case \"pattern\": {\n if (!isString) {\n addTypeMismatch(\n ctx,\n `Field \"${fieldName}\": constraint \"${ck}\" is only valid on string fields, but field type is \"${label}\"`,\n constraint.provenance\n );\n }\n break;\n }\n case \"minItems\":\n case \"maxItems\":\n case \"uniqueItems\": {\n if (!isArray) {\n addTypeMismatch(\n ctx,\n `Field \"${fieldName}\": constraint \"${ck}\" is only valid on array fields, but field type is \"${label}\"`,\n constraint.provenance\n );\n }\n break;\n }\n case \"allowedMembers\": {\n if (!isEnum) {\n addTypeMismatch(\n ctx,\n `Field \"${fieldName}\": constraint \"allowedMembers\" is only valid on enum fields, but field type is \"${label}\"`,\n constraint.provenance\n );\n }\n break;\n }\n case \"custom\": {\n checkCustomConstraint(ctx, fieldName, type, constraint);\n break;\n }\n default: {\n const _exhaustive: never = constraint;\n throw new Error(\n `Unhandled constraint kind: ${(_exhaustive as ConstraintNode).constraintKind}`\n );\n }\n }\n }\n}\n\n/**\n * Check a custom constraint against the extension registry.\n *\n * When the registry is available:\n * - If the constraint ID is not found, emit UNKNOWN_EXTENSION warning\n * - If found and the registration has `applicableTypes`, verify the field's\n * type kind is in that list (emit TYPE_MISMATCH if not)\n * - If `applicableTypes` is null, the constraint applies to any type\n *\n * When no registry is available, custom constraints are silently skipped.\n */\nfunction checkCustomConstraint(\n ctx: ValidationContext,\n fieldName: string,\n type: TypeNode,\n constraint: ConstraintNode & { readonly constraintKind: \"custom\" }\n): void {\n if (ctx.extensionRegistry === undefined) return;\n\n const registration = ctx.extensionRegistry.findConstraint(constraint.constraintId);\n\n if (registration === undefined) {\n addUnknownExtension(\n ctx,\n `Field \"${fieldName}\": custom constraint \"${constraint.constraintId}\" is not registered in the extension registry`,\n constraint.provenance\n );\n return;\n }\n\n // If applicableTypes is null, the constraint applies to any type\n if (registration.applicableTypes === null) return;\n\n if (!registration.applicableTypes.includes(type.kind)) {\n addTypeMismatch(\n ctx,\n `Field \"${fieldName}\": custom constraint \"${constraint.constraintId}\" is not applicable to type \"${typeLabel(type)}\"`,\n constraint.provenance\n );\n }\n}\n\n// =============================================================================\n// FIELD VALIDATION\n// =============================================================================\n\nfunction validateFieldNode(ctx: ValidationContext, field: FieldNode): void {\n validateConstraints(ctx, field.name, field.type, field.constraints);\n\n // Recurse into object type properties\n if (field.type.kind === \"object\") {\n for (const prop of field.type.properties) {\n validateObjectProperty(ctx, field.name, prop);\n }\n }\n}\n\nfunction validateObjectProperty(\n ctx: ValidationContext,\n parentName: string,\n prop: ObjectProperty\n): void {\n const qualifiedName = `${parentName}.${prop.name}`;\n validateConstraints(ctx, qualifiedName, prop.type, prop.constraints);\n\n // Recurse further if this property is also an object\n if (prop.type.kind === \"object\") {\n for (const nestedProp of prop.type.properties) {\n validateObjectProperty(ctx, qualifiedName, nestedProp);\n }\n }\n}\n\nfunction validateConstraints(\n ctx: ValidationContext,\n name: string,\n type: TypeNode,\n constraints: readonly ConstraintNode[]\n): void {\n checkNumericContradictions(ctx, name, constraints);\n checkLengthContradictions(ctx, name, constraints);\n checkAllowedMembersContradiction(ctx, name, constraints);\n checkTypeApplicability(ctx, name, type, constraints);\n}\n\n// =============================================================================\n// RECURSIVE ELEMENT WALK\n// =============================================================================\n\nfunction validateElement(ctx: ValidationContext, element: FormIRElement): void {\n switch (element.kind) {\n case \"field\":\n validateFieldNode(ctx, element);\n break;\n case \"group\":\n for (const child of element.elements) {\n validateElement(ctx, child);\n }\n break;\n case \"conditional\":\n for (const child of element.elements) {\n validateElement(ctx, child);\n }\n break;\n default: {\n const _exhaustive: never = element;\n throw new Error(`Unhandled element kind: ${(_exhaustive as FormIRElement).kind}`);\n }\n }\n}\n\n// =============================================================================\n// MAIN EXPORT\n// =============================================================================\n\n/**\n * Validate all constraints in a {@link FormIR}.\n *\n * Checks for:\n * - Contradictions between paired constraints (e.g. `minimum > maximum`)\n * - Type applicability violations (e.g. `minLength` on a number field)\n * - Custom constraint type applicability (via extension registry)\n * - Unknown extension constraints (when `extensionRegistry` is provided)\n *\n * @param ir - The form IR to validate.\n * @param options - Optional configuration.\n * @returns A {@link ValidationResult} with diagnostics and a `valid` flag.\n */\nexport function validateIR(ir: FormIR, options?: ValidateIROptions): ValidationResult {\n const ctx: ValidationContext = {\n diagnostics: [],\n vendorPrefix: options?.vendorPrefix ?? \"FORMSPEC\",\n extensionRegistry: options?.extensionRegistry,\n };\n\n for (const element of ir.elements) {\n validateElement(ctx, element);\n }\n\n return {\n diagnostics: ctx.diagnostics,\n valid: ctx.diagnostics.every((d) => d.severity !== \"error\"),\n };\n}\n\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACkDA,kBAA2B;AAO3B,IAAM,uBAAmC;AAAA,EACvC,SAAS;AAAA,EACT,MAAM;AAAA,EACN,MAAM;AAAA,EACN,QAAQ;AACV;AAMA,SAAS,QAAQ,IAAsD;AACr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