|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Build-time form-layout diagnostics (#2578). |
| 5 | + * |
| 6 | + * Authored form views carry field references and column-layout hints that are |
| 7 | + * Zod-valid but can be silently wrong at render time — the "parsed, unmarked, |
| 8 | + * silently inert" shape ADR-0078 prohibits. This lint catches the two that |
| 9 | + * matter for multi-column, AI-authored forms, uniformly for `os build` / |
| 10 | + * `os validate`, MCP authoring and hand authors (ADR-0019). |
| 11 | + * |
| 12 | + * Both rules are warnings, not errors — nothing is fully broken (an unknown |
| 13 | + * field name is skipped; an over-wide colSpan is clamped) — but each is almost |
| 14 | + * certainly an authoring mistake worth surfacing at author time: |
| 15 | + * |
| 16 | + * - `form-field-unknown` — a section references a field that is not on the |
| 17 | + * form's bound object, so the field silently does not render. |
| 18 | + * - `absolute-colspan-discouraged` — a field uses the absolute `colSpan`. Under |
| 19 | + * a per-surface DERIVED column count (mobile 1 / modal 2 / page 3-4) a fixed |
| 20 | + * span only lines up at the one width the author imagined; the renderer |
| 21 | + * clamps it. The robust primitive is the relative `span: 'full'`. |
| 22 | + * |
| 23 | + * Scope: top-level form `views` (a `sections` array). Forms embedded inside |
| 24 | + * page component trees are a follow-up — the walker deliberately stays shallow |
| 25 | + * so it never guesses at an arbitrary component's object binding. |
| 26 | + */ |
| 27 | + |
| 28 | +export const FORM_FIELD_UNKNOWN = 'form-field-unknown'; |
| 29 | +export const FORM_COLSPAN_ABSOLUTE = 'absolute-colspan-discouraged'; |
| 30 | + |
| 31 | +export type FormLayoutSeverity = 'error' | 'warning'; |
| 32 | + |
| 33 | +export interface FormLayoutFinding { |
| 34 | + /** Always `warning` today — both rules are advisory (see module note). */ |
| 35 | + severity: FormLayoutSeverity; |
| 36 | + /** Diagnostic rule id, e.g. `form-field-unknown`. */ |
| 37 | + rule: string; |
| 38 | + /** Human-readable location, e.g. `view "contract_form"`. */ |
| 39 | + where: string; |
| 40 | + /** Config path, e.g. `views[2].sections[0].fields[3]`. */ |
| 41 | + path: string; |
| 42 | + /** What is wrong. */ |
| 43 | + message: string; |
| 44 | + /** How to fix it. */ |
| 45 | + hint: string; |
| 46 | +} |
| 47 | + |
| 48 | +type AnyRec = Record<string, unknown>; |
| 49 | + |
| 50 | +/** Coerce a collection (array or name-keyed map) to an array of records. */ |
| 51 | +function asArray(v: unknown): AnyRec[] { |
| 52 | + if (Array.isArray(v)) return v as AnyRec[]; |
| 53 | + if (v && typeof v === 'object') { |
| 54 | + return Object.entries(v as AnyRec).map(([name, def]) => ({ name, ...(def as AnyRec) })); |
| 55 | + } |
| 56 | + return []; |
| 57 | +} |
| 58 | + |
| 59 | +/** A section field entry is either a bare field name or `{ field, colSpan, … }`. */ |
| 60 | +function fieldNameOf(entry: unknown): string | null { |
| 61 | + if (typeof entry === 'string') return entry.length > 0 ? entry : null; |
| 62 | + if (entry && typeof entry === 'object' && !Array.isArray(entry)) { |
| 63 | + const f = (entry as AnyRec).field; |
| 64 | + return typeof f === 'string' && f.length > 0 ? f : null; |
| 65 | + } |
| 66 | + return null; |
| 67 | +} |
| 68 | + |
| 69 | +/** The object a form view binds to: `data.object` (canonical) or `objectName`. */ |
| 70 | +function boundObject(view: AnyRec): string | undefined { |
| 71 | + const data = view.data; |
| 72 | + if (data && typeof data === 'object' && typeof (data as AnyRec).object === 'string') { |
| 73 | + return (data as AnyRec).object as string; |
| 74 | + } |
| 75 | + return typeof view.objectName === 'string' ? (view.objectName as string) : undefined; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Validate authored form-view layout. Returns findings (empty = clean). |
| 80 | + * Advisory only — the caller must never fail the build on these alone. |
| 81 | + */ |
| 82 | +export function validateFormLayout(stack: AnyRec): FormLayoutFinding[] { |
| 83 | + const findings: FormLayoutFinding[] = []; |
| 84 | + |
| 85 | + // object name → its field-name set, for reference checking. |
| 86 | + const objectFields = new Map<string, Set<string>>(); |
| 87 | + for (const obj of asArray(stack.objects)) { |
| 88 | + const name = typeof obj.name === 'string' ? obj.name : undefined; |
| 89 | + if (!name) continue; |
| 90 | + const fields = (obj.fields && typeof obj.fields === 'object' && !Array.isArray(obj.fields)) |
| 91 | + ? Object.keys(obj.fields as AnyRec) |
| 92 | + : []; |
| 93 | + objectFields.set(name, new Set(fields)); |
| 94 | + } |
| 95 | + |
| 96 | + const views = asArray(stack.views); |
| 97 | + for (let i = 0; i < views.length; i++) { |
| 98 | + const view = views[i]; |
| 99 | + if (!view || typeof view !== 'object') continue; |
| 100 | + const sections = Array.isArray(view.sections) ? view.sections : null; |
| 101 | + if (!sections) continue; // only form views carry a sections array |
| 102 | + |
| 103 | + const viewName = typeof view.name === 'string' ? view.name : `(view ${i})`; |
| 104 | + const objName = boundObject(view); |
| 105 | + // Only reference-check when the bound object resolves; otherwise we can't. |
| 106 | + const known = objName ? objectFields.get(objName) : undefined; |
| 107 | + const where = `view "${viewName}"`; |
| 108 | + const base = `views[${i}]`; |
| 109 | + |
| 110 | + for (let s = 0; s < sections.length; s++) { |
| 111 | + const sec = sections[s]; |
| 112 | + const secFields = sec && typeof sec === 'object' && Array.isArray((sec as AnyRec).fields) |
| 113 | + ? ((sec as AnyRec).fields as unknown[]) |
| 114 | + : []; |
| 115 | + for (let f = 0; f < secFields.length; f++) { |
| 116 | + const entry = secFields[f]; |
| 117 | + const fname = fieldNameOf(entry); |
| 118 | + const fpath = `${base}.sections[${s}].fields[${f}]`; |
| 119 | + |
| 120 | + // ── (a) section field references a real field on the bound object ── |
| 121 | + if (fname && known && !known.has(fname)) { |
| 122 | + findings.push({ |
| 123 | + severity: 'warning', |
| 124 | + rule: FORM_FIELD_UNKNOWN, |
| 125 | + where, |
| 126 | + path: fpath, |
| 127 | + message: |
| 128 | + `${viewName}: field "${fname}" is not a field on object "${objName}" — ` + |
| 129 | + `it is silently skipped and never renders on the form`, |
| 130 | + hint: |
| 131 | + `Fix the field name, or add "${fname}" to ${objName}. Section field ` + |
| 132 | + `references must match the object's field names exactly.`, |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + // ── (b) absolute colSpan → steer to the surface-independent span ── |
| 137 | + const colSpan = entry && typeof entry === 'object' && !Array.isArray(entry) |
| 138 | + ? (entry as AnyRec).colSpan |
| 139 | + : undefined; |
| 140 | + if (colSpan != null) { |
| 141 | + findings.push({ |
| 142 | + severity: 'warning', |
| 143 | + rule: FORM_COLSPAN_ABSOLUTE, |
| 144 | + where, |
| 145 | + path: `${fpath}.colSpan`, |
| 146 | + message: |
| 147 | + `${viewName}: field "${fname ?? '?'}" sets absolute colSpan ${String(colSpan)} — ` + |
| 148 | + `the form's column count is derived per surface (mobile 1 / modal 2 / page 3-4), ` + |
| 149 | + `so a fixed span only aligns at one width`, |
| 150 | + hint: |
| 151 | + `Prefer span: 'full' (whole row at any column count), or omit for auto ` + |
| 152 | + `width. The renderer clamps colSpan to the current column count.`, |
| 153 | + }); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return findings; |
| 160 | +} |
0 commit comments