|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// `field/embedded-sub-fields` — an object-level `composite` / `repeater` is |
| 4 | +// untyped JSON (ruling 6386744473), so declaring `fields` / `subFields` / |
| 5 | +// `columns` on one is an error whose route is a child object. |
| 6 | +// |
| 7 | +// The rule reaches only the doors that never parse (its docblock's intake |
| 8 | +// table): the strict field schema refuses the same keys first, with the same |
| 9 | +// route. Both halves are pinned below, so neither can be credited for the |
| 10 | +// other's door. |
| 11 | +import { describe, expect, it } from 'vitest'; |
| 12 | +import { FieldSchema } from '@objectstack/spec/data'; |
| 13 | + |
| 14 | +import { FIELD_EMBEDDED_SUB_FIELDS, lintDataModel } from './data-model-rules.js'; |
| 15 | +import { explainRule } from './rule-explanations.js'; |
| 16 | + |
| 17 | +const RULE = 'field/embedded-sub-fields'; |
| 18 | + |
| 19 | +/** One object carrying `checklist` as `def`, in the record shape authors write. */ |
| 20 | +const audit = (def: Record<string, unknown>) => [ |
| 21 | + { name: 'supplier_audit', fields: { name: { type: 'text' }, checklist: def } }, |
| 22 | +]; |
| 23 | +const findings = (objects: unknown[]) => lintDataModel(objects as any[]).filter((i) => i.rule === RULE); |
| 24 | + |
| 25 | +const SUB_ROWS = [{ name: 'item', label: 'Item', type: 'text' }]; |
| 26 | + |
| 27 | +describe('field/embedded-sub-fields — refuses a typed list on an embedded-JSON field', () => { |
| 28 | + for (const type of ['repeater', 'composite'] as const) { |
| 29 | + for (const [key, value] of [['fields', SUB_ROWS], ['subFields', SUB_ROWS], ['columns', ['item', 'score']]] as const) { |
| 30 | + it(`${type} + \`${key}\` is an error at that key`, () => { |
| 31 | + const [issue, ...rest] = findings(audit({ type, [key]: value })); |
| 32 | + expect(rest).toEqual([]); |
| 33 | + expect(issue).toMatchObject({ |
| 34 | + severity: 'error', |
| 35 | + rule: RULE, |
| 36 | + path: `objects[0].fields.checklist.${key}`, |
| 37 | + }); |
| 38 | + }); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + it('the array field shape is judged the same as the record shape', () => { |
| 43 | + const objects = [{ name: 'supplier_audit', fields: [{ name: 'checklist', type: 'repeater', fields: SUB_ROWS }] }]; |
| 44 | + expect(findings(objects)).toHaveLength(1); |
| 45 | + }); |
| 46 | + |
| 47 | + it('several keys on one field are one finding naming every key, located at the first', () => { |
| 48 | + const [issue, ...rest] = findings(audit({ type: 'repeater', fields: SUB_ROWS, columns: ['item'] })); |
| 49 | + expect(rest).toEqual([]); |
| 50 | + expect(issue!.path).toBe('objects[0].fields.checklist.fields'); |
| 51 | + expect(issue!.message).toContain('`fields`, `columns`'); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe('field/embedded-sub-fields — stays silent outside its scope', () => { |
| 56 | + it('a bare composite / repeater is untyped JSON, not an error', () => { |
| 57 | + expect(findings(audit({ type: 'repeater' }))).toEqual([]); |
| 58 | + expect(findings(audit({ type: 'composite' }))).toEqual([]); |
| 59 | + }); |
| 60 | + |
| 61 | + it('other field types are not judged here, whatever they carry', () => { |
| 62 | + expect(findings(audit({ type: 'json', fields: SUB_ROWS }))).toEqual([]); |
| 63 | + expect(findings(audit({ type: 'record', fields: SUB_ROWS }))).toEqual([]); |
| 64 | + expect(findings(audit({ type: 'lookup', reference: 'account', columns: ['name'] }))).toEqual([]); |
| 65 | + }); |
| 66 | + |
| 67 | + it('a key that merely shares a name with an inherited property is not authored', () => { |
| 68 | + const inherited = Object.create({ fields: SUB_ROWS }) as Record<string, unknown>; |
| 69 | + inherited.type = 'repeater'; |
| 70 | + expect(findings(audit(inherited))).toEqual([]); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe('field/embedded-sub-fields — the parsed doors refuse first, with the same route', () => { |
| 75 | + it('the strict field schema refuses each key by name and names `master_detail`', () => { |
| 76 | + for (const key of ['fields', 'subFields', 'columns']) { |
| 77 | + const r = FieldSchema.safeParse({ name: 'checklist', type: 'repeater', [key]: SUB_ROWS }); |
| 78 | + expect(r.success, key).toBe(false); |
| 79 | + if (r.success) continue; |
| 80 | + const issue = r.error.issues.find((i) => i.code === 'unrecognized_keys'); |
| 81 | + expect(issue, key).toBeTruthy(); |
| 82 | + expect(issue!.message, key).toContain('`master_detail`'); |
| 83 | + } |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +describe('field/embedded-sub-fields — one-line verdict, `os explain` carries the rest', () => { |
| 88 | + it('the published constant spells the id the findings carry', () => { |
| 89 | + expect(FIELD_EMBEDDED_SUB_FIELDS).toBe(RULE); |
| 90 | + }); |
| 91 | + |
| 92 | + it('the verdict names the type, the field and the keys, on one line of at most 200 characters', () => { |
| 93 | + const [issue] = findings(audit({ type: 'repeater', fields: SUB_ROWS })); |
| 94 | + expect(issue!.message).toBe( |
| 95 | + 'repeater field "supplier_audit.checklist" declares `fields`, but an object-level repeater is untyped ' + |
| 96 | + 'JSON with no sub-fields', |
| 97 | + ); |
| 98 | + expect(issue!.message).not.toContain('\n'); |
| 99 | + expect(issue!.message.length).toBeLessThanOrEqual(200); |
| 100 | + }); |
| 101 | + |
| 102 | + it('`os explain` carries the route, the exclusions and the reach', () => { |
| 103 | + const entry = explainRule(RULE); |
| 104 | + expect(entry, 'no `os explain field/embedded-sub-fields` entry').toBeDefined(); |
| 105 | + const text = entry!.paragraphs.join('\n'); |
| 106 | + for (const fact of [ |
| 107 | + 'untyped JSON', |
| 108 | + 'child object', |
| 109 | + '`master_detail`', |
| 110 | + 'inline related list', |
| 111 | + 'form view', |
| 112 | + 'refuses all three keys at the parse', |
| 113 | + 'never parse', |
| 114 | + ]) { |
| 115 | + expect(text, fact).toContain(fact); |
| 116 | + } |
| 117 | + }); |
| 118 | +}); |
0 commit comments