Skip to content

Commit 9b40625

Browse files
committed
test: pin the shorthand refusal at the save door and lock_state's text declaration; regenerate translation bundles
Claude-Session: https://claude.ai/code/session_0123qkvzKLQfRRRnuR8wanHJ Co-authored-by: Claude <noreply@anthropic.com>
1 parent 14ac950 commit 9b40625

7 files changed

Lines changed: 190 additions & 28 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* #25013 — `sys_metadata_audit.lock_state` declares a column that holds the
5+
* value its writer stores.
6+
*
7+
* The writer (`metadata-protocol` `protocol.ts`, `lock_state: entry.lockState
8+
* ?? 'none'`) stores a `MetadataLock` as spelled — `no-overlay`, `no-delete`.
9+
* The column used to be `Field.select([...])`, whose shorthand stripped the
10+
* hyphen and declared `nooverlay` / `nodelete`, values no row ever held. A
11+
* select option value is a system identifier, which holds no hyphen, so the
12+
* column is text, and the closed set stays with `MetadataLockSchema`.
13+
*/
14+
import { describe, expect, it } from 'vitest';
15+
import { ObjectSchema, SelectOptionSchema } from '@objectstack/spec/data';
16+
import { MetadataLockSchema } from '@objectstack/spec/kernel';
17+
import { SysMetadataAuditObject } from './sys-metadata-audit.object.js';
18+
19+
describe('sys_metadata_audit.lock_state (#25013)', () => {
20+
const field = SysMetadataAuditObject.fields.lock_state as Record<string, unknown>;
21+
22+
it('is a text column with no option list of its own and no width', () => {
23+
expect(field.type).toBe('text');
24+
expect(field).not.toHaveProperty('options');
25+
// No `maxLength`: an existing `varchar(255)` column reports no width drift.
26+
expect(field).not.toHaveProperty('maxLength');
27+
expect(field.readonly).toBe(true);
28+
});
29+
30+
it('the writer stores values a select option could not declare — which is why it is not a select', () => {
31+
const undeclarable = MetadataLockSchema.options.filter(
32+
(lock) => !SelectOptionSchema.safeParse({ label: lock, value: lock }).success,
33+
);
34+
expect(undeclarable).toEqual(['no-overlay', 'no-delete']);
35+
});
36+
37+
it('the object still parses as a whole', () => {
38+
expect(ObjectSchema.safeParse(SysMetadataAuditObject).success).toBe(true);
39+
});
40+
});
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* #25013 — a `Field.select` shorthand string that is no system identifier is
5+
* refused at the object save door, by the refusal an explicit option value
6+
* already meets.
7+
*
8+
* The shorthand (`packages/spec` `selectWithOptions`) used to delete every
9+
* character outside `[a-z0-9_]`, so `Field.select(['no-overlay'])` declared
10+
* the value `nooverlay` — a different value than the one written, accepted
11+
* without a word. It now only lower-cases and joins whitespace with `_`, so
12+
* the written value reaches `SelectOptionSchema.value` and `saveMetaItem`
13+
* answers `422 INVALID_METADATA` naming the option slot, exactly as for
14+
* `{ label: 'no-overlay', value: 'no-overlay' }`.
15+
*/
16+
import { describe, expect, it } from 'vitest';
17+
import { assertEngineDeleteDispatch, assertEngineUpdateDispatch, assertEngineFindOnePredicate, type EngineFindOneQueryInput } from '@objectstack/metadata-core';
18+
import { Field } from '@objectstack/spec/data';
19+
import { ObjectStackProtocolImplementation } from './protocol.js';
20+
21+
/** The engine surface the repository write path touches (as the container-issue-descent harness). */
22+
function makeProtocol() {
23+
const rows = new Map<string, Record<string, unknown>>();
24+
let nextId = 0;
25+
const engine: any = {
26+
async findOne(object: string, query?: EngineFindOneQueryInput) {
27+
assertEngineFindOnePredicate(object, query); return null; },
28+
async find() { return []; },
29+
async insert(table: string, data: Record<string, unknown>) {
30+
nextId += 1;
31+
if (table === 'sys_metadata') rows.set(`${data.type}|${data.name}`, data);
32+
return { id: `r_${nextId}` };
33+
},
34+
async update(_t: string, data: Record<string, unknown>, opts?: Record<string, unknown>) {
35+
assertEngineUpdateDispatch(data, opts);
36+
return { id: null };
37+
},
38+
async delete(_t: string, opts?: Record<string, unknown>) {
39+
assertEngineDeleteDispatch(opts);
40+
return { deleted: 0 };
41+
},
42+
registry: { registerItem: () => {}, registerObject: () => {} },
43+
};
44+
const protocol: any = new ObjectStackProtocolImplementation(engine, () => new Map());
45+
return { protocol, rows };
46+
}
47+
48+
async function rejection(promise: Promise<unknown>): Promise<any> {
49+
try {
50+
await promise;
51+
} catch (err) {
52+
return err;
53+
}
54+
throw new Error('expected the save to be rejected, but it resolved');
55+
}
56+
57+
const objectWith = (status: unknown) => ({
58+
name: 'ticket',
59+
label: 'Ticket',
60+
sharingModel: 'private',
61+
fields: { status: { name: 'status', label: 'Status', ...(status as object) } },
62+
});
63+
64+
describe('#25013 the save door refuses a shorthand option that is no system identifier', () => {
65+
it('`Field.select([\'open\', \'no-overlay\'])` answers 422 INVALID_METADATA at the option slot, and nothing is stored', async () => {
66+
const { protocol, rows } = makeProtocol();
67+
68+
const err = await rejection(protocol.saveMetaItem({
69+
type: 'object', name: 'ticket', item: objectWith(Field.select(['open', 'no-overlay'])),
70+
}));
71+
72+
expect(err.code).toBe('INVALID_METADATA');
73+
expect(err.status).toBe(422);
74+
expect(err.issues).toEqual([
75+
expect.objectContaining({ path: 'fields.status.options.1.value', code: 'invalid_format' }),
76+
]);
77+
expect(rows.size).toBe(0);
78+
});
79+
80+
it('the explicit `{ value: \'no-overlay\' }` meets the identical refusal — one refusal set, not two', async () => {
81+
const shorthand = await rejection(makeProtocol().protocol.saveMetaItem({
82+
type: 'object', name: 'ticket', item: objectWith(Field.select(['open', 'no-overlay'])),
83+
}));
84+
const explicit = await rejection(makeProtocol().protocol.saveMetaItem({
85+
type: 'object', name: 'ticket',
86+
item: objectWith(Field.select({ options: ['open', { label: 'no-overlay', value: 'no-overlay' }] })),
87+
}));
88+
89+
expect([explicit.code, explicit.status]).toEqual(['INVALID_METADATA', 422]);
90+
expect(shorthand.issues).toEqual(explicit.issues);
91+
});
92+
93+
it('control: the documented transform still saves (`In Progress` → `in_progress`)', async () => {
94+
const { protocol, rows } = makeProtocol();
95+
96+
const result = await protocol.saveMetaItem({
97+
type: 'object', name: 'ticket', item: objectWith(Field.select(['Open', 'In Progress'])),
98+
});
99+
100+
expect(result.success).toBe(true);
101+
expect(rows.size).toBe(1);
102+
});
103+
});

‎packages/platform-objects/src/apps/translations/en.objects.generated.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,13 +3245,7 @@ export const enObjects: NonNullable<TranslationData['objects']> = {
32453245
label: "Code"
32463246
},
32473247
lock_state: {
3248-
label: "Lock State",
3249-
options: {
3250-
none: "none",
3251-
nooverlay: "no-overlay",
3252-
nodelete: "no-delete",
3253-
full: "full"
3254-
}
3248+
label: "Lock State"
32553249
},
32563250
lock_overridden: {
32573251
label: "Lock Overridden"

‎packages/platform-objects/src/apps/translations/es-ES.objects.generated.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,13 +3245,7 @@ export const esESObjects: NonNullable<TranslationData['objects']> = {
32453245
label: "Código"
32463246
},
32473247
lock_state: {
3248-
label: "Estado de bloqueo",
3249-
options: {
3250-
none: "Ninguno",
3251-
nooverlay: "Sin superposición",
3252-
nodelete: "Sin eliminación",
3253-
full: "Bloqueo total"
3254-
}
3248+
label: "Estado de bloqueo"
32553249
},
32563250
lock_overridden: {
32573251
label: "Bloqueo anulado"

‎packages/platform-objects/src/apps/translations/ja-JP.objects.generated.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,13 +3245,7 @@ export const jaJPObjects: NonNullable<TranslationData['objects']> = {
32453245
label: "コード"
32463246
},
32473247
lock_state: {
3248-
label: "ロック状態",
3249-
options: {
3250-
none: "なし",
3251-
nooverlay: "オーバーレイ禁止",
3252-
nodelete: "削除禁止",
3253-
full: "完全ロック"
3254-
}
3248+
label: "ロック状態"
32553249
},
32563250
lock_overridden: {
32573251
label: "ロックの上書き"

‎packages/platform-objects/src/apps/translations/zh-CN.objects.generated.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,13 +3245,7 @@ export const zhCNObjects: NonNullable<TranslationData['objects']> = {
32453245
label: "代码"
32463246
},
32473247
lock_state: {
3248-
label: "锁定状态",
3249-
options: {
3250-
none: "无",
3251-
nooverlay: "禁止叠加",
3252-
nodelete: "禁止删除",
3253-
full: "完全锁定"
3254-
}
3248+
label: "锁定状态"
32553249
},
32563250
lock_overridden: {
32573251
label: "锁定已被覆盖"

‎packages/spec/src/data/field.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,49 @@ describe('Field Factory Helpers', () => {
14851485
expect(selectField.options[1]).toEqual({ label: 'Inactive', value: 'inactive' });
14861486
expect(selectField.options[2]).toEqual({ label: 'Pending', value: 'pending' });
14871487
});
1488+
1489+
// #25013 — the shorthand's only transforms are lower-casing and
1490+
// whitespace → `_`. It used to also DELETE every other character, so
1491+
// `'no-overlay'` was declared as `nooverlay`: a value nobody wrote.
1492+
describe('a shorthand string is kept as written beyond the documented transform', () => {
1493+
const issuesOf = (field: unknown) => {
1494+
const result = FieldSchema.safeParse({ name: 'status', ...(field as object) });
1495+
expect(result.success).toBe(false);
1496+
return (result as { error: z.ZodError }).error.issues;
1497+
};
1498+
1499+
it('keeps a hyphen, so the value is the one written', () => {
1500+
expect(Field.select(['no-overlay']).options).toEqual([{ label: 'no-overlay', value: 'no-overlay' }]);
1501+
expect(Field.select({ options: ['user-agent-based'] }).options)
1502+
.toEqual([{ label: 'user-agent-based', value: 'user-agent-based' }]);
1503+
});
1504+
1505+
it('meets the refusal an explicit `{ value: \'no-overlay\' }` meets — the same issue, the same path', () => {
1506+
const shorthand = issuesOf(Field.select(['open', 'no-overlay']));
1507+
const explicit = issuesOf(Field.select({ options: ['open', { label: 'no-overlay', value: 'no-overlay' }] }));
1508+
expect(shorthand).toEqual(explicit);
1509+
expect(shorthand).toHaveLength(1);
1510+
expect(shorthand[0]).toMatchObject({ code: 'invalid_format', path: ['options', 1, 'value'] });
1511+
});
1512+
1513+
it('refuses at module load when an object declares it through `ObjectSchema.create`', () => {
1514+
let thrown: unknown;
1515+
try {
1516+
ObjectSchema.create({ name: 'probe_lock', label: 'Probe', fields: { lock_state: Field.select(['none', 'no-overlay']) } });
1517+
} catch (e) {
1518+
thrown = e;
1519+
}
1520+
expect(thrown).toBeInstanceOf(z.ZodError);
1521+
expect((thrown as z.ZodError).issues.map((i) => [i.code, i.path.join('.')]))
1522+
.toEqual([['invalid_format', 'fields.lock_state.options.1.value']]);
1523+
});
1524+
1525+
it('still lower-cases and joins whitespace with `_`, and those values parse', () => {
1526+
const field = Field.select(['In Progress', 'Closed Won', 'v1.2']);
1527+
expect(field.options.map((o) => o.value)).toEqual(['in_progress', 'closed_won', 'v1.2']);
1528+
expect(FieldSchema.safeParse({ name: 'stage', ...field }).success).toBe(true);
1529+
});
1530+
});
14881531
});
14891532

14901533
describe('Lookup and Master-Detail Fields', () => {

0 commit comments

Comments
 (0)