Skip to content

Commit 27b3ced

Browse files
committed
feat(spec): a field group's requiresFeature lowers into its visibleWhen; sys_organization hides group structure on one organization
Claude-Session: https://claude.ai/code/session_016WV4cyCnzVPr9GuD38DdGt Co-authored-by: Claude <noreply@anthropic.com>
1 parent 1371b9d commit 27b3ced

5 files changed

Lines changed: 241 additions & 19 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
//
3+
// On a deployment that runs one organization, Parent Organization and Sort
4+
// Order order and group organizations against nothing, so Edit organization
5+
// hides them there (#30692, ruling 6670372892 on #30210, row 4 A). The two
6+
// fields sit in the object's `group_structure` field group, declared with
7+
// `requiresFeature: 'multiOrgEnabled'`, which the spec lowers into the group's
8+
// `visibleWhen` at parse. Both renderers evaluate that predicate with the
9+
// host's `features` root in scope (the form's section gate and the record
10+
// page's `record:details`), fail-closed.
11+
import { describe, expect, it } from 'vitest';
12+
import { celEngine } from '@objectstack/formula';
13+
import { deriveFieldGroupLayout } from '@objectstack/spec/data';
14+
import { featureGatePredicate } from '@objectstack/spec/kernel';
15+
import { SysOrganization } from './sys-organization.object.js';
16+
17+
type Group = { key: string; visibleWhen?: { dialect?: string; source?: string }; requiresFeature?: unknown };
18+
const groups = (SysOrganization.fieldGroups ?? []) as Group[];
19+
// `null` = grouping does not apply (no declared groups), and both renderers
20+
// fall back to a flat layout of every field: what a deployment saw before the
21+
// groups were declared.
22+
const layout = deriveFieldGroupLayout(SysOrganization as Parameters<typeof deriveFieldGroupLayout>[0])
23+
?? [{ fields: Object.keys(SysOrganization.fields), visibleWhen: undefined }];
24+
25+
/**
26+
* The section-gate rule both renderers apply: no predicate shows the section,
27+
* a predicate shows it only when it answers TRUE, and a fault hides it.
28+
*/
29+
function sectionShown(visibleWhen: Group['visibleWhen'], features: Record<string, unknown>): boolean {
30+
if (!visibleWhen?.source) return true;
31+
const r = celEngine.evaluate({ dialect: 'cel', source: visibleWhen.source }, { record: {}, extra: { features } });
32+
return r.ok && r.value === true;
33+
}
34+
35+
/** Field names the layout renders for this deployment, in render order. */
36+
function renderedFields(features: Record<string, unknown>): string[] {
37+
return layout.filter((s) => sectionShown(s.visibleWhen as Group['visibleWhen'], features)).flatMap((s) => s.fields);
38+
}
39+
40+
describe('sys_organization: Parent Organization and Sort Order hide on a single-organization deployment', () => {
41+
it('both fields are in one field group that the multiOrgEnabled gate guards', () => {
42+
const fields = SysOrganization.fields as Record<string, { group?: string }>;
43+
expect(fields.parent_organization_id?.group).toBe('group_structure');
44+
expect(fields.sort_order?.group).toBe('group_structure');
45+
const group = groups.find((g) => g.key === 'group_structure');
46+
expect(group?.visibleWhen).toEqual({ dialect: 'cel', source: featureGatePredicate('multiOrgEnabled') });
47+
// Lowered and stripped: no consumer sees the sugar key.
48+
expect(group && 'requiresFeature' in group).toBe(false);
49+
});
50+
51+
it('multiOrgEnabled off: the group is hidden', () => {
52+
const shown = renderedFields({ multiOrgEnabled: false });
53+
expect(shown).not.toContain('parent_organization_id');
54+
expect(shown).not.toContain('sort_order');
55+
expect(shown).toContain('name');
56+
});
57+
58+
it('multiOrgEnabled on: the group is shown', () => {
59+
const shown = renderedFields({ multiOrgEnabled: true });
60+
expect(shown).toContain('parent_organization_id');
61+
expect(shown).toContain('sort_order');
62+
});
63+
64+
it('no other group is gated, and the fields keep their declaration order', () => {
65+
expect(groups.filter((g) => g.visibleWhen !== undefined).map((g) => g.key)).toEqual(['group_structure']);
66+
const declared = Object.keys(SysOrganization.fields).filter((name) =>
67+
renderedFields({ multiOrgEnabled: true }).includes(name),
68+
);
69+
expect(renderedFields({ multiOrgEnabled: true })).toEqual(declared);
70+
});
71+
});

‎packages/platform-objects/src/identity/sys-organization.object.ts‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,22 @@ export const SysOrganization = ObjectSchema.create({
332332
using: 'id == current_user.organization_id',
333333
},
334334

335+
// Declared in the fields' own order, so every field keeps its place. The
336+
// System trio (`id`, `created_at`, `updated_at`) stays in no declared group
337+
// and renders last, as before. `group_structure` carries the ADR-0105 D6
338+
// reporting fields, which order and group organizations against each other
339+
// and so do nothing on a deployment that runs one organization: the group is
340+
// gated on `multiOrgEnabled` (lowered at parse into its `visibleWhen`), and
341+
// the form and the record page both hide it where the flag is off (ruling
342+
// 6670372892 on #30210, row 4 A). Presentation only — the fields stay
343+
// readable and writable through the API, and their help still says so.
344+
fieldGroups: [
345+
{ key: 'identity', label: 'Identity' },
346+
{ key: 'branding', label: 'Branding' },
347+
{ key: 'configuration', label: 'Configuration' },
348+
{ key: 'group_structure', label: 'Group Structure', requiresFeature: 'multiOrgEnabled' },
349+
],
350+
335351
fields: {
336352
// ── Identity ─────────────────────────────────────────────────
337353
// ADR-0092 D4 — with the generic edit affordance open (#15873), every
@@ -360,7 +376,7 @@ export const SysOrganization = ObjectSchema.create({
360376
description:
361377
'The organization\'s own name. Change it with Rename Organization at the top of this record. ' +
362378
'The product name the console shows is a different setting, set in Settings → Branding.',
363-
group: 'Identity',
379+
group: 'identity',
364380
}),
365381

366382
slug: Field.text({
@@ -370,15 +386,15 @@ export const SysOrganization = ObjectSchema.create({
370386
searchable: true,
371387
maxLength: 255,
372388
description: 'URL-friendly identifier',
373-
group: 'Identity',
389+
group: 'identity',
374390
}),
375391

376392
// ── Branding ─────────────────────────────────────────────────
377393
logo: Field.url({
378394
label: 'Logo',
379395
required: false,
380396
readonly: true,
381-
group: 'Branding',
397+
group: 'branding',
382398
}),
383399

384400
// ── Configuration ────────────────────────────────────────────
@@ -389,7 +405,7 @@ export const SysOrganization = ObjectSchema.create({
389405
required: false,
390406
readonly: true,
391407
description: 'JSON-serialized organization metadata',
392-
group: 'Configuration',
408+
group: 'configuration',
393409
}),
394410

395411
// ADR-0069 D3 — per-org MFA tightening above the global floor. When true,
@@ -402,7 +418,7 @@ export const SysOrganization = ObjectSchema.create({
402418
label: 'Require Multi-Factor Auth',
403419
required: false,
404420
defaultValue: false,
405-
group: 'Configuration',
421+
group: 'configuration',
406422
description:
407423
'Turn on to require every member of this organization to set up an authenticator app before they can access data. ' +
408424
'Needs two-factor authentication on for this deployment (OS_AUTH_TWO_FACTOR=true); while it is off, ' +
@@ -458,7 +474,7 @@ export const SysOrganization = ObjectSchema.create({
458474
valueDomain: 'iana_time_zone',
459475
description:
460476
'IANA time zone (e.g. UTC, Asia/Shanghai), kept as the root default for business units without a zone of their own. It does not change any date calculation today: formulas, reports and displayed dates use Settings → Localization → Default timezone.',
461-
group: 'Configuration',
477+
group: 'configuration',
462478
}),
463479

464480
// ── Group structure (ADR-0105 D6) ────────────────────────────
@@ -484,15 +500,15 @@ export const SysOrganization = ObjectSchema.create({
484500
parent_organization_id: Field.lookup('sys_organization', {
485501
label: 'Parent Organization',
486502
required: false,
487-
group: 'Group Structure',
503+
group: 'group_structure',
488504
description:
489505
'Reporting/grouping parent. Grants NOTHING — visibility across organizations comes from membership, never from this reference. Meaningful only where the deployment runs several organizations.',
490506
}),
491507

492508
sort_order: Field.number({
493509
label: 'Sort Order',
494510
required: false,
495-
group: 'Group Structure',
511+
group: 'group_structure',
496512
description:
497513
'Display order among sibling organizations. Presentation only. Meaningful only where the deployment runs several organizations.',
498514
}),
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
import { describe, expect, it } from 'vitest';
4+
import { ObjectFieldGroupSchema, ObjectSchema } from './object.zod';
5+
import { featureGatePredicate } from '../kernel/public-auth-features';
6+
7+
/**
8+
* `fieldGroups[].requiresFeature` — the field-group twin of the action-level
9+
* `requiresFeature`. It names a flag of `PUBLIC_AUTH_FEATURES`, is lowered at
10+
* parse time into the group's `visibleWhen` by the same helper the action key
11+
* uses, and is stripped from the output. These pins hold the four facts a
12+
* consumer relies on: the lowering, the strip, the refusal of an unknown flag,
13+
* and the composition with an authored `visibleWhen`.
14+
*/
15+
describe('ObjectFieldGroupSchema.requiresFeature', () => {
16+
const gate = featureGatePredicate('multiOrgEnabled');
17+
18+
it('lowers into visibleWhen and strips the key', () => {
19+
const out = ObjectFieldGroupSchema.parse({
20+
key: 'group_structure',
21+
label: 'Group Structure',
22+
requiresFeature: 'multiOrgEnabled',
23+
});
24+
expect(out.visibleWhen).toEqual({ dialect: 'cel', source: 'features.multiOrgEnabled != false' });
25+
expect(out.visibleWhen).toEqual({ dialect: 'cel', source: gate });
26+
expect('requiresFeature' in out).toBe(false);
27+
});
28+
29+
it('lowers an opt-in flag to its own gate, the one an action gets', () => {
30+
const out = ObjectFieldGroupSchema.parse({ key: 'mfa', label: 'MFA', requiresFeature: 'twoFactor' });
31+
expect(out.visibleWhen).toEqual({ dialect: 'cel', source: 'features.twoFactor == true' });
32+
});
33+
34+
it('refuses a flag outside the public feature registry, at requiresFeature', () => {
35+
const res = ObjectFieldGroupSchema.safeParse({ key: 'g', label: 'G', requiresFeature: 'multiOrg' });
36+
expect(res.success).toBe(false);
37+
const issue = res.error!.issues[0]!;
38+
expect(issue.code).toBe('invalid_value');
39+
expect(issue.path).toEqual(['requiresFeature']);
40+
});
41+
42+
it('AND-composes with an authored visibleWhen: the author first, the gate last', () => {
43+
const fromString = ObjectFieldGroupSchema.parse({
44+
key: 'g',
45+
label: 'G',
46+
visibleWhen: "record.kind == 'child'",
47+
requiresFeature: 'multiOrgEnabled',
48+
});
49+
expect(fromString.visibleWhen).toEqual({
50+
dialect: 'cel',
51+
source: `(record.kind == 'child') && ${gate}`,
52+
});
53+
54+
const fromEnvelope = ObjectFieldGroupSchema.parse({
55+
key: 'g',
56+
label: 'G',
57+
visibleWhen: { dialect: 'cel', source: "record.kind == 'child'" },
58+
requiresFeature: 'multiOrgEnabled',
59+
});
60+
expect(fromEnvelope.visibleWhen).toEqual(fromString.visibleWhen);
61+
});
62+
63+
it('leaves a group without the key exactly as before', () => {
64+
const out = ObjectFieldGroupSchema.parse({ key: 'g', label: 'G', visibleWhen: 'record.x == 1' });
65+
expect(out.visibleWhen).toEqual({ dialect: 'cel', source: 'record.x == 1' });
66+
expect('requiresFeature' in out).toBe(false);
67+
expect(ObjectFieldGroupSchema.parse({ key: 'g', label: 'G' }).visibleWhen).toBeUndefined();
68+
});
69+
70+
it('lowers inside an object, where authors write it', () => {
71+
const obj = ObjectSchema.create({
72+
name: 'acme_unit',
73+
label: 'Unit',
74+
fields: {
75+
title: { type: 'text', label: 'Title', group: 'basics' },
76+
parent_note: { type: 'text', label: 'Parent note', group: 'tree' },
77+
},
78+
fieldGroups: [
79+
{ key: 'basics', label: 'Basics' },
80+
{ key: 'tree', label: 'Tree', requiresFeature: 'multiOrgEnabled' },
81+
],
82+
});
83+
const [basics, tree] = obj.fieldGroups!;
84+
expect(basics!.visibleWhen).toBeUndefined();
85+
expect(tree!.visibleWhen).toEqual({ dialect: 'cel', source: gate });
86+
expect('requiresFeature' in tree!).toBe(false);
87+
});
88+
});

‎packages/spec/src/data/object.zod.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { ObjectListViewSchema } from '../ui/view.zod';
1818
*/
1919
import { EvaluatedExpressionInputSchema, TemplateExpressionInputSchema, type EvaluatedExpression, type EvaluatedExpressionInput } from '../shared/expression.zod';
2020
import { lazySchema } from '../shared/lazy-schema';
21+
import { PUBLIC_AUTH_FEATURE_NAMES, lowerRequiresFeature } from '../kernel/public-auth-features';
2122
import { MetadataProtectionFields } from '../kernel/metadata-protection.zod';
2223
import { closedObject, strictObject } from '../shared/strict-object';
2324
import { ProtectionSchema } from '../shared/protection.zod';
@@ -1289,6 +1290,31 @@ export const ObjectFieldGroupSchema = lazySchema(() => strictObject({
12891290
"Section visibility predicate (CEL) — the whole group (header included) is shown only when TRUE, else hidden (fail-closed). e.g. P`record.type == 'invoice'`",
12901291
).meta({ title: 'Visible When' }),
12911292

1293+
/**
1294+
* Declarative capability gate — the field-group twin of the action-level
1295+
* `requiresFeature` (`ui/action.zod.ts`). Names a deployment feature flag of
1296+
* `PUBLIC_AUTH_FEATURES` (`@objectstack/spec/kernel`), the same vocabulary
1297+
* actions use, so a flag outside it is refused at parse. Lowered at parse
1298+
* time into this group's `visibleWhen` by `lowerRequiresFeature` — the same
1299+
* gate an action gets (`features.multiOrgEnabled != false` for a default-on
1300+
* flag, `== true` for an opt-in one) — and stripped from the output, so a
1301+
* consumer only ever sees the canonical `visibleWhen`.
1302+
*
1303+
* Composition with an authored `visibleWhen`: AND, the author's predicate
1304+
* first and the gate last — `(<author predicate>) && <gate>` — so the group
1305+
* shows only where both hold. A non-CEL `visibleWhen` refuses the pair;
1306+
* write the combined predicate by hand.
1307+
*
1308+
* Both renderers bind the host's `features` root when they evaluate a
1309+
* group's `visibleWhen` (the form's section gate and the record page's
1310+
* `record:details`), so the lowered gate is enforced on both. Like every
1311+
* field-group predicate it is presentation only: the fields stay readable
1312+
* through the API.
1313+
*/
1314+
requiresFeature: z.enum(PUBLIC_AUTH_FEATURE_NAMES).optional().describe(
1315+
"Deployment feature flag gating this group, from the same set an action's `requiresFeature` names (e.g. 'multiOrgEnabled'). Lowered at parse time into the group's `visibleWhen` (AND-composed after an authored `visibleWhen`) and stripped from the output; the whole group is hidden where the flag is off. Presentation only: the fields stay readable through the API.",
1316+
).meta({ title: 'Requires Feature' }),
1317+
12921318
/**
12931319
* [ADR-0085] Collapse behaviour of the group's rendered section, on every
12941320
* surface (form, detail, drawer). One enum, three valid states — replaces
@@ -1310,7 +1336,9 @@ export const ObjectFieldGroupSchema = lazySchema(() => strictObject({
13101336
collapsible: z.boolean().optional().describe("[DEPRECATED → collapse] Boolean pair with `collapsed`; use the `collapse` enum.").meta({ title: 'Collapsible' }),
13111337
/** @deprecated [ADR-0085 → `collapse`] UI-dialect alias (pair with `collapsible`); mapped onto `collapse` at parse. */
13121338
collapsed: z.boolean().optional().describe("[DEPRECATED → collapse] true → 'collapsed' (collapsible, starts closed) on its own — it needs no `collapsible` and outranks `collapsible: false`; false → 'none', or 'expanded' beside `collapsible: true`. Use the `collapse` enum.").meta({ title: 'Collapsed' }),
1313-
}));
1339+
// `requiresFeature` → `visibleWhen`, the action precedent's lowering with the
1340+
// group's slot name; the key never reaches the output.
1341+
}).transform((group, ctx) => lowerRequiresFeature(group, ctx, 'visibleWhen')));
13141342

13151343
export type ObjectFieldGroup = z.input<typeof ObjectFieldGroupSchema>;
13161344
/** Post-parse shape of {@link ObjectFieldGroup} — defaults applied, transforms run (ADR-0122). */

0 commit comments

Comments
 (0)