/** * L1.1 — VisualAiModel JSON Schema validator * * Validates AI form generation output against: * 1. JNPF7 official VisualAiModel contract (VisualAiModel.java:18-30 et al.) * 2. dacli extensions (f_ prefix, snake_case, no _fk suffix, relationForm completeness) * * Usage: * npx tsx tools/validate-form-schema.ts * npx tsx tools/validate-form-schema.ts --stdin * * Exit codes: * 0 = valid * 1 = validation failed (errors printed) * 2 = input not parseable * * No external deps — hand-rolled validator handles our schema subset. */ import { readFileSync } from 'node:fs' // JNPF7 24-component whitelist (AiConstants.java:23-25) + dacli relationForm const COMPONENT_WHITELIST = new Set([ // basic 'input', 'textarea', 'inputNumber', 'switch', 'radio', 'checkbox', 'select', 'datePicker', 'timePicker', 'editor', // upload / media 'uploadFile', 'uploadImg', 'colorPicker', 'rate', 'slider', // system selectors 'organizeSelect', 'posSelect', 'userSelect', 'roleSelect', 'areaSelect', 'signature', 'sign', 'location', // dacli exclusive 'relationForm', ]) const RELATIONFORM_REQUIRED = ['modelId', 'propsValue', 'relationField', 'showField', 'hasPage', 'queryType', 'columnOptions'] interface Issue { severity: 'error' | 'warn' rule: string message: string path: string } // ---------- Hand-rolled JSON Schema subset validator ---------- type Schema = any function validate(value: unknown, schema: Schema, path: string, issues: Issue[]): void { if (!schema || typeof schema !== 'object') return // $ref resolution (only local #/definitions/) if (schema.$ref) { const ref = schema.$ref if (ref.startsWith('#/')) { const parts = ref.slice(2).split('/') let cur: any = schema for (const p of parts) cur = cur?.[p] return validate(value, cur, path, issues) } return } // allOf if (Array.isArray(schema.allOf)) { for (const sub of schema.allOf) validate(value, sub, path, issues) } // anyOf / oneOf — only handle oneOf with simple branch matching if (Array.isArray(schema.anyOf)) { const branchIssues: Issue[][] = schema.anyOf.map(() => []) for (let i = 0; i < schema.anyOf.length; i++) { validate(value, schema.anyOf[i], path, branchIssues[i]) } if (branchIssues.every(b => b.some(x => x.severity === 'error'))) { // all branches errored — propagate first const first = branchIssues.find(b => b.some(x => x.severity === 'error')) if (first) issues.push(...first.filter(x => x.severity === 'error').slice(0, 1)) } } // type if (schema.type) { const ok = checkType(value, schema.type) if (!ok) { issues.push({ severity: 'error', rule: 'schema:type', message: `expected type ${schema.type}, got ${describeType(value)}`, path, }) return // no point continuing type-incompatible checks } } // enum if (Array.isArray(schema.enum) && !schema.enum.includes(value)) { issues.push({ severity: 'error', rule: 'schema:enum', message: `expected one of [${schema.enum.join(', ')}], got ${JSON.stringify(value)}`, path, }) } // const if ('const' in schema && value !== schema.const) { issues.push({ severity: 'error', rule: 'schema:const', message: `expected const ${JSON.stringify(schema.const)}, got ${JSON.stringify(value)}`, path, }) } // pattern (string only) if (schema.pattern && typeof value === 'string' && !new RegExp(schema.pattern).test(value)) { issues.push({ severity: 'error', rule: 'schema:pattern', message: `does not match pattern ${schema.pattern} (value: ${JSON.stringify(value).slice(0, 80)})`, path, }) } // minLength / maxLength if (typeof value === 'string') { if (schema.minLength !== undefined && value.length < schema.minLength) { issues.push({ severity: 'error', rule: 'schema:minLength', message: `length ${value.length} < minLength ${schema.minLength}`, path }) } if (schema.maxLength !== undefined && value.length > schema.maxLength) { issues.push({ severity: 'error', rule: 'schema:maxLength', message: `length ${value.length} > maxLength ${schema.maxLength}`, path }) } } // minItems / maxItems if (Array.isArray(value)) { if (schema.minItems !== undefined && value.length < schema.minItems) { issues.push({ severity: 'error', rule: 'schema:minItems', message: `length ${value.length} < minItems ${schema.minItems}`, path }) } if (schema.maxItems !== undefined && value.length > schema.maxItems) { issues.push({ severity: 'error', rule: 'schema:maxItems', message: `length ${value.length} > maxItems ${schema.maxItems}`, path }) } } // object: required, properties, additionalProperties if (typeof value === 'object' && value !== null && !Array.isArray(value)) { const obj = value as Record if (Array.isArray(schema.required)) { for (const req of schema.required) { if (!(req in obj)) { issues.push({ severity: 'error', rule: 'schema:required', message: `missing required property "${req}"`, path, }) } } } if (schema.properties) { for (const [key, sub] of Object.entries(schema.properties)) { if (key in obj) { validate(obj[key], sub, path ? `${path}.${key}` : key, issues) } } } if (schema.additionalProperties === false) { const allowed = new Set(Object.keys(schema.properties ?? {})) for (const key of Object.keys(obj)) { if (!allowed.has(key)) { issues.push({ severity: 'warn', rule: 'schema:additionalProperties', message: `unexpected property "${key}"`, path: path ? `${path}.${key}` : key, }) } } } } // array: items if (Array.isArray(value) && schema.items) { for (let i = 0; i < value.length; i++) { validate(value[i], schema.items, `${path}[${i}]`, issues) } } } function checkType(value: unknown, type: string | string[]): boolean { const types = Array.isArray(type) ? type : [type] for (const t of types) { if (t === 'null' && value === null) return true if (t === 'string' && typeof value === 'string') return true if (t === 'number' && typeof value === 'number') return true if (t === 'integer' && typeof value === 'number' && Number.isInteger(value)) return true if (t === 'boolean' && typeof value === 'boolean') return true if (t === 'array' && Array.isArray(value)) return true if (t === 'object' && typeof value === 'object' && value !== null && !Array.isArray(value)) return true } return false } function describeType(value: unknown): string { if (value === null) return 'null' if (Array.isArray(value)) return 'array' return typeof value } // ---------- dacli-specific antipattern checks ---------- function checkAntipatterns(data: any): Issue[] { const issues: Issue[] = [] const tables = data?.aiModelList ?? [] for (let ti = 0; ti < tables.length; ti++) { const t = tables[ti] for (let fi = 0; fi < (t.fields ?? []).length; fi++) { const f = t.fields[fi] const path = `aiModelList[${ti}].fields[${fi}]` if (/_fk$/i.test(f.fieldName ?? '')) { issues.push({ severity: 'error', rule: 'BS-新117-no-_fk-suffix', message: `fieldName ends with "_fk" — JNPF silently drops these; dacli should map to relationForm. Found: ${f.fieldName}`, path: `${path}.fieldName`, }) } if (/_num\d{3}$/i.test(f.fieldName ?? '')) { issues.push({ severity: 'warn', rule: 'BS-JNPF-numNNN-rename', message: `fieldName ends with "_numNNN" — JNPF silent rename; dacli should reject. Found: ${f.fieldName}`, path: `${path}.fieldName`, }) } const comp = f.fieldComponent if (comp && !COMPONENT_WHITELIST.has(comp)) { issues.push({ severity: 'error', rule: 'BS-JNPF-component-not-in-whitelist', message: `fieldComponent "${comp}" not in 24-component whitelist + relationForm. JNPF silently downgrades to "input".`, path: `${path}.fieldComponent`, }) } if (f.fieldName && !/^f_/.test(f.fieldName)) { issues.push({ severity: 'warn', rule: 'dacli-f-prefix', message: `fieldName should start with "f_" (dacli convention). Found: ${f.fieldName}`, path: `${path}.fieldName`, }) } if (comp === 'relationForm') { for (const req of RELATIONFORM_REQUIRED) { if (f[req] === undefined || f[req] === null || f[req] === '') { issues.push({ severity: 'error', rule: 'BS-新132-relationForm-incomplete', message: `relationForm field missing required key "${req}". dacli requires all 7 fields.`, path: `${path}.${req}`, }) } } } } } return issues } // ---------- Main ---------- function loadInput(): unknown { const args = process.argv.slice(2) if (args.includes('--stdin')) { return JSON.parse(readFileSync(0, 'utf8')) } const file = args[0] if (!file) { console.error('Usage: validate-form-schema.ts | --stdin') process.exit(2) } return JSON.parse(readFileSync(file, 'utf8')) } function main() { let data: unknown try { data = loadInput() } catch (e: any) { console.error(`✗ input parse failed: ${e.message}`) process.exit(2) } const baseSchema = JSON.parse( readFileSync(new URL('./schemas/visual-ai-model.schema.json', import.meta.url), 'utf8'), ) const dacliExtensions = JSON.parse( readFileSync(new URL('./schemas/dacli-extensions.schema.json', import.meta.url), 'utf8'), ) const issues: Issue[] = [] validate(data, baseSchema, '', issues) validate(data, dacliExtensions, '', issues) issues.push(...checkAntipatterns(data)) if (issues.length === 0) { console.log('✓ VisualAiModel valid: schema + dacli extensions + no antipatterns') process.exit(0) } console.error(`✗ ${issues.length} issue(s) found:`) for (const i of issues) { console.error(` [${i.severity.toUpperCase()}] ${i.rule}`) console.error(` at ${i.path}: ${i.message}`) } const errorCount = issues.filter(i => i.severity === 'error').length process.exit(errorCount > 0 ? 1 : 0) } main()