Skip to content

Commit adc1b3f

Browse files
committed
feat(lint): field/embedded-sub-fields — a composite/repeater declaring sub-fields is an error on the unparsed doors
Claude-Session: https://claude.ai/code/session_01T2U7eGg8aRTpJ8kKXxDiog Co-authored-by: Claude <noreply@anthropic.com>
1 parent 8565c18 commit adc1b3f

5 files changed

Lines changed: 233 additions & 3 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
});

‎packages/lint/src/data-model-rules.fix-lines.test.ts‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
// three ADR-0120 uniqueness ids carry it as the `hint` `os validate` and
66
// `os build` print. What the hints used to explain is `os explain` text.
77
//
8-
// The three `lintDataModel` sweep ids (`object/missing-name-field`,
9-
// `field/select-missing-options`, `rollup/non-numeric-aggregand`) are never
8+
// The four `lintDataModel` sweep ids (`object/missing-name-field`,
9+
// `field/select-missing-options`, `rollup/non-numeric-aggregand`,
10+
// `field/embedded-sub-fields`) are never
1011
// adapted into an `AuthoringFinding`, so a census that reads `hint` never sees
1112
// them. This file reads `fix` directly, over every arm each rule has, with long
1213
// object and field names where a name is interpolated.
1314

1415
import { describe, expect, it } from 'vitest';
1516

1617
import {
18+
FIELD_EMBEDDED_SUB_FIELDS,
1719
lintDataModel,
1820
lintLegacyOrganizationComposites,
1921
lintUniqueDeclarations,
@@ -36,6 +38,7 @@ const IDS = [
3638
MISSING_NAME_FIELD,
3739
SELECT_MISSING_OPTIONS,
3840
ROLLUP_NON_NUMERIC_AGGREGAND,
41+
FIELD_EMBEDDED_SUB_FIELDS,
3942
];
4043

4144
const LONG_OBJECT = 'acme_customer_purchase_order_header';
@@ -170,6 +173,24 @@ describe('[#22161] data-model-rules.ts — one remedy line per fix', () => {
170173
}
171174
});
172175

176+
it('field/embedded-sub-fields: one line, whatever the type, the keys or the names', () => {
177+
const fix =
178+
'Make the typed list a child object whose `master_detail` field points back here (an inline ' +
179+
'related list), and remove the key.';
180+
for (const type of ['repeater', 'composite']) {
181+
const objects = [
182+
{
183+
name: LONG_OBJECT,
184+
fields: {
185+
name: { type: 'text' },
186+
[LONG_FIELD]: { type, fields: [{ name: 'item' }], subFields: [{ name: 'item' }], columns: ['item'] },
187+
},
188+
},
189+
];
190+
expect(fixOf(record(lintDataModel(objects)), FIELD_EMBEDDED_SUB_FIELDS)).toBe(fix);
191+
}
192+
});
193+
173194
it('rollup/non-numeric-aggregand: one line naming the child type, not the parent', () => {
174195
expect(fixOf(record(lintDataModel(rollup('datetime', LONG_OBJECT))), ROLLUP_NON_NUMERIC_AGGREGAND)).toBe(
175196
"Aggregate a numeric or boolean child field, or use function: 'count'; a roll-up cannot store a " +
@@ -181,7 +202,7 @@ describe('[#22161] data-model-rules.ts — one remedy line per fix', () => {
181202
});
182203

183204
it('every fix the cases above fired is one line of at most 200 characters', () => {
184-
// The coverage control first: all six ids fired.
205+
// The coverage control first: all seven ids fired.
185206
expect([...new Set(fired.map((f) => f.rule))].sort()).toEqual([...IDS].sort());
186207
for (const f of fired) {
187208
expect(f.fix, f.rule).toBeTruthy();

‎packages/lint/src/data-model-rules.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,50 @@ export const RELATIONSHIP_DELETE_BEHAVIOR = 'relationship/delete-behavior';
261261
/** The rule id R13 reports under (a `min`/`max` roll-up over a non-numeric child). */
262262
export const ROLLUP_NON_NUMERIC_AGGREGAND = 'rollup/non-numeric-aggregand';
263263

264+
/**
265+
* The rule id R14 reports under: an object field of type `composite` /
266+
* `repeater` that declares sub-fields or columns.
267+
*
268+
* Ruling 6386744473: an object-level `composite` / `repeater` is untyped JSON,
269+
* no sub-field member is added to `FieldSchema`, and a list that needs typed
270+
* columns is a child object (`master_detail`). A form view's `fields` sub-rows
271+
* (`view.zod.ts`) and the Studio metadata forms are other schemas and are not
272+
* judged here.
273+
*
274+
* ## Intake — which doors reach this rule at all
275+
*
276+
* `FieldSchema` is strict, and its `FIELD_KEY_GUIDANCE` rows for these three
277+
* keys make the parse refusal name the child-object route. So on every door
278+
* that PARSES first this rule is unreachable by construction — the object
279+
* never arrives. Measured on the source tree this rule shipped against:
280+
*
281+
* | door | reaches the rule? |
282+
* |--------------------------------------------------------------|-------------------------|
283+
* | `defineStack(x)` (strict default), hence `os validate` / | no — refused at load |
284+
* | `os build` / `os lint` on such a config | |
285+
* | `saveMetaItem` (Studio / REST `/meta` / MCP) | no — 422 at the parse |
286+
* | **`os lint` on a RAW object-literal config** (never parses) | **yes — fires** |
287+
* | **`defineStack(x, { strict: false })`** (keeps the key) | **yes — fires** |
288+
* | **the metadata-generation rubric** (`scoreMetadata`), beside | **yes — fires** |
289+
* | its own schema verdict | |
290+
* | **`lintDataModel` called directly with an unparsed stack** | **yes — fires** |
291+
*
292+
* It is not a second opinion on the parse: it exists so the unparsed doors
293+
* name the same route instead of letting the key ride to `os build`. A
294+
* consumer crediting it as coverage on a `defineStack`-authored app is
295+
* crediting the wrong gate — there the credit belongs to the schema.
296+
*/
297+
export const FIELD_EMBEDDED_SUB_FIELDS = 'field/embedded-sub-fields';
298+
299+
/** The two embedded-JSON field types R14 judges. */
300+
const EMBEDDED_JSON_FIELD_TYPES = new Set(['composite', 'repeater']);
301+
302+
/**
303+
* The keys an author writes when they expect a typed list on R14's types — the
304+
* three the ruling names, the same three `FIELD_KEY_GUIDANCE` answers.
305+
*/
306+
const SUB_FIELD_KEYS = ['fields', 'subFields', 'columns'] as const;
307+
264308
/**
265309
* [#22161] The consequence every `controlled_by_parent` arm of R2 ends with —
266310
* one wording, so the three shapes cannot drift. Why such a record is
@@ -778,6 +822,25 @@ export function lintDataModel(objects: any[]): LintIssue[] {
778822
}
779823
}
780824

825+
// R14 — an object-level `composite` / `repeater` declares no sub-fields
826+
// (see FIELD_EMBEDDED_SUB_FIELDS for the ruling and the doors it reaches).
827+
if (EMBEDDED_JSON_FIELD_TYPES.has(type)) {
828+
const declared = SUB_FIELD_KEYS.filter((k) => Object.prototype.hasOwnProperty.call(def, k));
829+
if (declared.length > 0) {
830+
issues.push({
831+
severity: 'error',
832+
rule: FIELD_EMBEDDED_SUB_FIELDS,
833+
message:
834+
`${type} field "${obj.name}.${fieldName}" declares \`${declared.join('`, `')}\`, but an ` +
835+
`object-level ${type} is untyped JSON with no sub-fields`,
836+
path: `${fieldPath}.${declared[0]}`,
837+
fix:
838+
'Make the typed list a child object whose `master_detail` field points back here (an inline ' +
839+
'related list), and remove the key.',
840+
});
841+
}
842+
}
843+
781844
// R13 — a `min`/`max` roll-up must aggregate a child field whose ANSWER
782845
// fits the column the roll-up is stored into.
783846
//

‎packages/lint/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@ export {
10041004
RELATIONSHIP_MASTER_DETAIL_REQUIRED,
10051005
RELATIONSHIP_DELETE_BEHAVIOR,
10061006
ROLLUP_NON_NUMERIC_AGGREGAND,
1007+
FIELD_EMBEDDED_SUB_FIELDS,
10071008
} from './data-model-rules.js';
10081009
export type { LintIssue, LocatedLintIssue, Severity } from './data-model-rules.js';
10091010

‎packages/lint/src/rule-explanations.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,6 +2936,32 @@ const ROLLUP_NON_NUMERIC_AGGREGAND_EXPLANATION: RuleExplanation = {
29362936
],
29372937
};
29382938

2939+
const FIELD_EMBEDDED_SUB_FIELDS_EXPLANATION: RuleExplanation = {
2940+
rule: 'field/embedded-sub-fields',
2941+
covers: 'why a composite or repeater field has no sub-fields',
2942+
paragraphs: [
2943+
'On an object field, `composite` and `repeater` are untyped JSON containers of CMS lineage (a ' +
2944+
'Strapi component, an ACF group or repeater): the value is stored as JSON on the parent row, ' +
2945+
'and `FieldSchema` has no member that declares its inner keys. So nothing gives a row its ' +
2946+
'labels, per-column types, options, required flags or translations, nothing validates one, ' +
2947+
'and nothing — a flow included — can address a single row. Declaring `fields`, `subFields` ' +
2948+
'or `columns` on one promises a typed list the platform does not build.',
2949+
'A list that needs typed columns is a child object: its own fields carry the types, labels, ' +
2950+
'options and validation, a `master_detail` field on it points back at the parent, and the ' +
2951+
'parent\'s record page shows the children as an inline related list, editable in place with ' +
2952+
'`inlineEdit` on that field. Each row then has a record identity that a lookup, a roll-up or a ' +
2953+
'flow can reach. Keep a `composite` / `repeater` only for JSON whose shape the platform does ' +
2954+
'not need to know.',
2955+
'Not judged: a form view\'s `fields` sub-rows and the metadata forms Studio renders — those are ' +
2956+
'other schemas that do declare sub-rows.',
2957+
'Reach: the strict field schema refuses all three keys at the parse with this same route, so ' +
2958+
'`defineStack`, `os validate`, `os build` and the metadata save door never hand such a field ' +
2959+
'to this rule. It fires only on the doors that never parse — `os lint` on a raw object-literal ' +
2960+
'config, `defineStack(x, { strict: false })`, and the metadata-generation rubric beside its own ' +
2961+
'schema verdict — where without it the key would ride unnamed to `os build`.',
2962+
],
2963+
};
2964+
29392965
const RELATIONSHIP_DELETE_BEHAVIOR_EXPLANATION: RuleExplanation = {
29402966
rule: 'relationship/delete-behavior',
29412967
covers: 'what deleting a master does to its details',
@@ -4974,6 +5000,7 @@ export const RULE_EXPLANATIONS: Readonly<Record<string, RuleExplanation>> = Obje
49745000
[RELATIONSHIP_MASTER_DETAIL_REQUIRED_EXPLANATION.rule]: RELATIONSHIP_MASTER_DETAIL_REQUIRED_EXPLANATION,
49755001
[ROLLUP_NON_NUMERIC_AGGREGAND_EXPLANATION.rule]: ROLLUP_NON_NUMERIC_AGGREGAND_EXPLANATION,
49765002
[RELATIONSHIP_DELETE_BEHAVIOR_EXPLANATION.rule]: RELATIONSHIP_DELETE_BEHAVIOR_EXPLANATION,
5003+
[FIELD_EMBEDDED_SUB_FIELDS_EXPLANATION.rule]: FIELD_EMBEDDED_SUB_FIELDS_EXPLANATION,
49775004
[DEFAULT_AGENT_LEGACY_ALIAS_EXPLANATION.rule]: DEFAULT_AGENT_LEGACY_ALIAS_EXPLANATION,
49785005
[DEFAULT_AGENT_OUTSIDE_ROSTER_EXPLANATION.rule]: DEFAULT_AGENT_OUTSIDE_ROSTER_EXPLANATION,
49795006
[AGENT_AUTHORING_WITHDRAWN_EXPLANATION.rule]: AGENT_AUTHORING_WITHDRAWN_EXPLANATION,

0 commit comments

Comments
 (0)