Skip to content

Commit 6eb560a

Browse files
committed
test(spec,lint,rest): pin field.searchable joining the auto-default search set
Claude-Session: https://claude.ai/code/session_01ADHkmSR2TvJTmoYyVtcQv9 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 574bc7e commit 6eb560a

4 files changed

Lines changed: 380 additions & 2 deletions

File tree

‎packages/lint/src/validate-searchable-fields.test.ts‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
resolveSearchFields,
66
SEARCHABLE_ENUM_TYPES,
77
SEARCHABLE_TEXTUAL_TYPES,
8+
SEARCH_FLAG_EXCLUDED_TYPES,
89
SEARCH_VIRTUAL_TYPES,
910
} from '@objectstack/spec/data';
1011
import {
@@ -822,7 +823,76 @@ describe('validateSearchableFields — objectstack-ui SKILL.md parity (#6675)',
822823
const refused = validateSearchableFields(viewStack(['account_id'], noDeclaration));
823824
expect(refused).toHaveLength(1);
824825
expect(refused[0].rule).toBe(SEARCHABLE_FIELD_UNSEARCHABLE);
825-
expect(refused[0].message).toContain("of type 'lookup', which 'search' cannot scan");
826+
// [#29008] The verdict names the opt-in the field lacks: the runtime now
827+
// admits a field of any type marked `searchable: true` (ruling record
828+
// 6573569376), so "which 'search' cannot scan" stopped being true.
829+
expect(refused[0].message).toContain("of type 'lookup' and not marked `searchable: true`");
830+
});
831+
});
832+
833+
/**
834+
* [#29008] The field's own `searchable: true` — the lint resolves the allowed
835+
* set with the runtime's function, so its field-meta projection must carry the
836+
* flag, or a narrowing the ingress accepts would be refused here.
837+
*/
838+
describe('[#29008] validateSearchableFields — a field marked searchable: true', () => {
839+
const noteStack = (fields: Record<string, unknown>, viewSet: string[], objectSet?: string[]) => ({
840+
objects: [{ name: 'dev_note', ...(objectSet ? { searchableFields: objectSet } : {}), fields }],
841+
views: [{
842+
name: 'dev_note',
843+
objectName: 'dev_note',
844+
list: { label: 'All', type: 'grid', data: { provider: 'object', object: 'dev_note' }, columns: ['name'] },
845+
listViews: {
846+
code: {
847+
label: 'Code', type: 'grid', data: { provider: 'object', object: 'dev_note' },
848+
columns: ['name'], searchableFields: viewSet,
849+
},
850+
},
851+
}],
852+
});
853+
const flagged = {
854+
name: { type: 'text' },
855+
snippet: { type: 'code', searchable: true },
856+
body: { type: 'html', searchable: true },
857+
};
858+
859+
it('with no object declaration, a view narrowing to flagged `code` / `html` fields lints clean', () => {
860+
expect(validateSearchableFields(noteStack(flagged, ['snippet', 'body']))).toEqual([]);
861+
});
862+
863+
it('CONTROL — the same fields unflagged are refused, and the fix line offers the flag', () => {
864+
const refused = validateSearchableFields(noteStack(
865+
{ name: { type: 'text' }, snippet: { type: 'code' } },
866+
['snippet'],
867+
));
868+
expect(refused).toHaveLength(1);
869+
expect(refused[0].rule).toBe(SEARCHABLE_FIELD_UNSEARCHABLE);
870+
expect(refused[0].message).toContain("of type 'code' and not marked `searchable: true`");
871+
expect(refused[0].hint).toBe(
872+
'Mark "snippet" `searchable: true` on "dev_note", drop it from this view, or target a text-like field instead',
873+
);
874+
});
875+
876+
it('CONTROL — a flagged `vector` stays refused, and the verdict says the flag cannot admit it', () => {
877+
const refused = validateSearchableFields(noteStack(
878+
{ name: { type: 'text' }, embedding: { type: 'vector', searchable: true } },
879+
['embedding'],
880+
));
881+
expect(refused).toHaveLength(1);
882+
expect(refused[0].message).toContain("of type 'vector', never scanned even with `searchable: true`");
883+
expect(refused[0].hint).toBe('Drop "embedding" from this view, or target a text-like field instead');
884+
});
885+
886+
it('CONTROL — an object that DECLARES its set ignores the flag: outside the declaration is refused', () => {
887+
const refused = validateSearchableFields(noteStack(flagged, ['snippet'], ['name']));
888+
expect(refused).toHaveLength(1);
889+
expect(refused[0].message).toContain('outside the declared searchableFields (name)');
890+
});
891+
892+
it('the allowed set is the runtime\'s: indexObjectSearchTargets carries the flag into the resolution', () => {
893+
const target = indexObjectSearchTargets(noteStack(flagged, ['snippet'])).get('dev_note');
894+
expect(target?.fields.snippet).toEqual({ type: 'code', hidden: false, searchable: true });
895+
expect(target?.fields.name).toEqual({ type: 'text', hidden: false, searchable: false });
826896
});
827897
});
828898

@@ -1239,6 +1309,9 @@ describe('[#22161] one-line verdicts — the three searchable ids', () => {
12391309
// the virtual-type claim it states are held to their spec sources here.
12401310
const types = [...SEARCHABLE_TEXTUAL_TYPES, ...SEARCHABLE_ENUM_TYPES].join(' / ');
12411311
expect(explanationOf(SEARCHABLE_FIELD_UNSEARCHABLE)).toContain(`(${types})`);
1312+
// [#29008] …and the opt-in's hard exclusions, as the explanation spells them.
1313+
const flagExcluded = [...SEARCH_FLAG_EXCLUDED_TYPES].map((t) => `\`${t}\``).join(' / ');
1314+
expect(explanationOf(SEARCHABLE_FIELD_UNSEARCHABLE)).toContain(`of any type but ${flagExcluded}`);
12421315
expect([...SEARCH_VIRTUAL_TYPES]).toEqual(['formula']);
12431316
expect(explanationOf(SEARCHABLE_FIELD_UNSEARCHABLE)).toContain('`formula` alone');
12441317
});

‎packages/lint/src/validate-searchable-fields.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ export function checkSearchableFieldList(
524524
// `searchable: true`, except the hard exclusions — so "cannot scan" is
525525
// said only of those, and every other type names the opt-in it lacks.
526526
why = SEARCH_FLAG_EXCLUDED_TYPES.has(meta.type)
527-
? `of type '${meta.type}', which 'search' never scans, even marked \`searchable: true\``
527+
? `of type '${meta.type}', never scanned even with \`searchable: true\``
528528
: `of type '${meta.type}' and not marked \`searchable: true\``;
529529
} else {
530530
continue; // no readable type — unresolvable, not wrong (ADR-0072 D1)
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#29008] A field marked `searchable: true` is searched, and a `$searchFields`
5+
* override naming it is accepted, when its object declares no
6+
* `searchableFields` — over the Data API door on a real `SqlDriver` (SQLite),
7+
* the harness of `data-search-fields-credential-door.test.ts`.
8+
*
9+
* Ruling record 6573569376 (letter A): with no declaration, the set `search`
10+
* scans is the type-based auto-default plus every field marked
11+
* `searchable: true`, of any type except `secret` / `password` / `vector`, and
12+
* the ingress allow-list is the same set. Before, the card's three requests
13+
* answered `[]`, `[]`, then `400 INVALID_FIELD` "is not searchable … 'snippet'
14+
* is type 'code'" while the served schema said `searchable: true`.
15+
*
16+
* Pinned here: the card's three requests (the row comes back through `snippet`
17+
* and through `body`, and `$searchFields=snippet` answers 200 with it), and the
18+
* controls the ruling keeps — an unflagged `code` field is still neither
19+
* scanned nor accepted, a flagged `secret` is still refused, and an object
20+
* that DECLARES `searchableFields` keeps its declaration as the whole set.
21+
*/
22+
23+
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
24+
import { ObjectQL } from '@objectstack/objectql';
25+
import { SqlDriver } from '@objectstack/driver-sql';
26+
import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol';
27+
import { RestServer } from './rest-server';
28+
import { installDefaultOrganization } from './default-organization.testkit.js';
29+
30+
/** No declaration: the flag decides. */
31+
const AUTO = 'rest_search_flag_auto_29008';
32+
/** The same fields, under a declaration that names only `name`. */
33+
const DECLARED = 'rest_search_flag_declared_29008';
34+
35+
const FIELDS = {
36+
name: { name: 'name', type: 'text' as const },
37+
snippet: { name: 'snippet', type: 'code' as const, searchable: true },
38+
body: { name: 'body', type: 'html' as const, searchable: true },
39+
// CONTROL — a `code` field NOT marked: the auto-default still skips its type.
40+
scratch: { name: 'scratch', type: 'code' as const },
41+
// CONTROL — a hard exclusion: the flag cannot admit a credential.
42+
api_key: { name: 'api_key', type: 'secret' as const, searchable: true },
43+
};
44+
45+
const ROWS = [
46+
{ id: 'r1', name: 'fs2 row', snippet: 'select QQCODE from x', body: '<p>holding QQHTML</p>', scratch: 'QQSCRATCH' },
47+
{ id: 'r2', name: 'other row', snippet: 'select 1', body: '<p>nothing</p>', scratch: 'nothing' },
48+
];
49+
50+
function createMockServer() {
51+
const noop = () => {};
52+
return { get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, listen: async () => {}, close: async () => {} };
53+
}
54+
55+
function makeRes() {
56+
const res: any = {
57+
write: () => true, end: () => {},
58+
header: () => res,
59+
status: (code: number) => { res._status = code; return res; },
60+
json: (body: any) => { res._json = body; return res; },
61+
};
62+
return res;
63+
}
64+
65+
describe('[#29008] field.searchable: true on the Data API door — sqlite', () => {
66+
let engine: ObjectQL;
67+
let driver: any;
68+
const reads = { n: 0 };
69+
let list: (object: string, query: Record<string, string>) => Promise<{ status: number; body: any }>;
70+
71+
beforeAll(async () => {
72+
driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true } as any);
73+
engine = new ObjectQL();
74+
engine.registerDriver(driver, true);
75+
await engine.init();
76+
engine.registry.registerObject({ name: AUTO, label: 'Search flag auto 29008', fields: FIELDS } as any);
77+
engine.registry.registerObject({
78+
name: DECLARED, label: 'Search flag declared 29008', searchableFields: ['name'], fields: FIELDS,
79+
} as any);
80+
await engine.syncSchemas();
81+
// A real `single` install's Default Organization (ADR-0131 D9, D1).
82+
await installDefaultOrganization(engine);
83+
for (const object of [AUTO, DECLARED]) {
84+
for (const row of ROWS) await engine.insert(object, row as any);
85+
}
86+
87+
const watched = new Set<string>([AUTO, DECLARED]);
88+
for (const verb of ['find', 'findOne', 'count', 'aggregate'] as const) {
89+
const real = driver[verb].bind(driver);
90+
driver[verb] = (o: string, ...rest: unknown[]) => { if (watched.has(o)) reads.n += 1; return real(o, ...rest); };
91+
}
92+
93+
const protocol = new ObjectStackProtocolImplementation(engine as any);
94+
const rest = new RestServer(createMockServer() as any, protocol as any, { api: { requireAuth: false } } as any);
95+
(rest as any).resolveExecCtx = async () => ({ userId: 'test-user' });
96+
rest.registerRoutes();
97+
const getRoute = rest.getRoutes().find((r: any) => r.method === 'GET' && r.path === '/api/v1/data/:object');
98+
expect(getRoute, 'GET /api/v1/data/:object').toBeDefined();
99+
list = async (object, q) => {
100+
const res = makeRes();
101+
// What a querystring carries: strings.
102+
await getRoute!.handler({ params: { object }, query: { $orderby: 'name', ...q }, headers: {} } as any, res);
103+
return { status: res._status ?? 200, body: res._json };
104+
};
105+
});
106+
107+
afterAll(async () => {
108+
try { await engine?.destroy(); } catch { /* noop */ }
109+
});
110+
111+
const names = (body: any) => (body?.records ?? []).map((r: any) => r.name);
112+
113+
it('the card\'s three requests: found through `snippet`, through `body`, and `$searchFields=snippet` is 200', async () => {
114+
const byCode = await list(AUTO, { $search: 'QQCODE' });
115+
expect(byCode.status, JSON.stringify(byCode.body)).toBe(200);
116+
expect(names(byCode.body)).toEqual(['fs2 row']);
117+
118+
const byHtml = await list(AUTO, { $search: 'QQHTML' });
119+
expect(byHtml.status, JSON.stringify(byHtml.body)).toBe(200);
120+
expect(names(byHtml.body)).toEqual(['fs2 row']);
121+
122+
const narrowed = await list(AUTO, { $search: 'QQCODE', $searchFields: 'snippet' });
123+
expect(narrowed.status, JSON.stringify(narrowed.body)).toBe(200);
124+
expect(names(narrowed.body)).toEqual(['fs2 row']);
125+
// …and the narrowing is real: the html term is not in `snippet`.
126+
const narrowedMiss = await list(AUTO, { $search: 'QQHTML', $searchFields: 'snippet' });
127+
expect(narrowedMiss.status, JSON.stringify(narrowedMiss.body)).toBe(200);
128+
expect(names(narrowedMiss.body)).toEqual([]);
129+
});
130+
131+
it('CONTROL — an unflagged `code` field is not scanned, and naming it is still 400 INVALID_FIELD before any read', async () => {
132+
const unscanned = await list(AUTO, { $search: 'QQSCRATCH' });
133+
expect(unscanned.status, JSON.stringify(unscanned.body)).toBe(200);
134+
expect(names(unscanned.body)).toEqual([]);
135+
136+
const before = reads.n;
137+
const refused = await list(AUTO, { $search: 'QQSCRATCH', $searchFields: 'scratch' });
138+
expect(refused.status, JSON.stringify(refused.body)).toBe(400);
139+
expect(refused.body.code).toBe('INVALID_FIELD');
140+
expect(refused.body.error).toContain("Field 'scratch' on object '" + AUTO + "' is not searchable");
141+
expect(reads.n - before, 'the refusal precedes the driver').toBe(0);
142+
});
143+
144+
it('CONTROL — a flagged `secret` stays excluded: naming it is 400 INVALID_FIELD before any read', async () => {
145+
const before = reads.n;
146+
const refused = await list(AUTO, { $search: 'QQKEY', $searchFields: 'api_key' });
147+
expect(refused.status, JSON.stringify(refused.body)).toBe(400);
148+
expect(refused.body.code).toBe('INVALID_FIELD');
149+
expect(reads.n - before, 'the refusal precedes the driver').toBe(0);
150+
});
151+
152+
it('CONTROL — a declared `searchableFields` stays the whole set: the flag is not read there', async () => {
153+
const byCode = await list(DECLARED, { $search: 'QQCODE' });
154+
expect(byCode.status, JSON.stringify(byCode.body)).toBe(200);
155+
expect(names(byCode.body)).toEqual([]);
156+
157+
const byName = await list(DECLARED, { $search: 'fs2' });
158+
expect(byName.status, JSON.stringify(byName.body)).toBe(200);
159+
expect(names(byName.body)).toEqual(['fs2 row']);
160+
161+
const before = reads.n;
162+
const refused = await list(DECLARED, { $search: 'QQCODE', $searchFields: 'snippet' });
163+
expect(refused.status, JSON.stringify(refused.body)).toBe(400);
164+
expect(refused.body.code).toBe('INVALID_FIELD');
165+
expect(refused.body.error).toContain("The object declares 'searchableFields' (name)");
166+
expect(reads.n - before, 'the refusal precedes the driver').toBe(0);
167+
});
168+
});

0 commit comments

Comments
 (0)