Skip to content

Commit ef7e1b7

Browse files
committed
test(rest): the /data batch row answers a declared field's NOT NULL refusal by label; changeset
Claude-Session: https://claude.ai/code/session_01E1oXN1ZJ8NnZKKhzWeCZ6q Co-authored-by: Claude <noreply@anthropic.com>
1 parent ee3ea87 commit ef7e1b7

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
'@objectstack/objectql': patch
3+
'@objectstack/types': patch
4+
'@objectstack/rest': patch
5+
---
6+
7+
fix(objectql,types,rest)!: a database NOT NULL refusal on a declared field is the record validator's `ValidationError` at every door, named by the field's label
8+
9+
A field declared `storage: { notNull: true }` without `required` is ADR-0113's engine-populated column: the record validator judges `required` only, so a write with no value for it reaches the driver and the database refuses it. The engine let the driver's error out, and each HTTP door classified it from the driver's text its own way. For the same refused write, the runtime dispatcher's `/data` POST and PATCH answered `500 INTERNAL_ERROR`, the actions door answered `400 VALIDATION_ERROR` with the driver's text and `declaredCode: 'SQLITE_CONSTRAINT_NOTNULL'`, and `RestServer` answered `400 VALIDATION_FAILED` relabelled by a second object read on its own `POST` / `PATCH` only.
10+
11+
Clause-②: yes (narrowing: `@objectstack/types` adds the export `notNullViolationSite` and retires `notNullRefusalSite` and `relabelNotNullRefusal`; the HTTP doors' answer for this refusal moves to the record validator's envelope)
12+
Metadata-protocol: none
13+
14+
<!-- adr-0087: not-required (runtime-export-retired @objectstack/types#notNullRefusalSite, @objectstack/types#relabelNotNullRefusal) No metadata moves: no spec key, authorable spelling, object definition or stored shape is added, removed, renamed or re-shaped, so there is nothing for `objectstack migrate meta` to rewrite. What retires is two function exports of `@objectstack/types`' published entry, whose only consumer was `@objectstack/rest`'s door-side relabel, retired in this same change; no other consumer was found in this repository (its `ui/` and `examples/` included) or in hotcrm. The affected party is a TypeScript caller, and the compiler at that call site delivers the break. -->
15+
16+
**BREAKING** (an export retirement and a wire-answer change), shipped as `patch`: no metadata protocol kind moves.
17+
18+
- **`@objectstack/objectql`.** The write doors' driver exits (single-row and batch insert, the autonumber retry, by-id and predicate update) translate a driver NOT NULL refusal on a field the object's field map holds into the record validator's own `ValidationError` (`code: 'VALIDATION_FAILED'`), built by the validator's own builder with the validator's message context: one field error, `code: 'required'`, the field named by its label (translation bundle, then declared `label`, then API name) in the caller's locale, with the sentence the validator gives the same verb (`required` on insert, `required_cleared` on update). The driver's error rides `cause`, and the doors' log line still carries the database's own diagnosis. Left as the driver threw it: a column no field declares, a refusal the driver attributes to another table, and a NOT NULL code whose column the driver did not name. `required` and `storage.notNull` stay apart (ADR-0113 D1 / D2): no pre-write check reads `storage.notNull`.
19+
- **Every door answers it alike**, because every door already answers that class alike: the dispatcher `/data` POST and PATCH now answer `400 VALIDATION_FAILED` (was `500 INTERNAL_ERROR`), the actions door `400 VALIDATION_FAILED` with the field envelope (was `400 VALIDATION_ERROR` with the driver's text), and `RestServer`'s `POST` / `PATCH` and its `/data/:object/batch` rows answer the validator's envelope and sentence. Import rows, the public form submit and MCP write through the same engine doors and answer the same class through the same arms; they were not driven separately. On `RestServer` the body is now exactly the validator's `required` shape: `fields[0]` carries `label`, there is no `hint`, and a `PATCH` writing null reads the clear-out sentence.
20+
- **`@objectstack/types`.** New export `notNullViolationSite(error)`: the column (and table, where the dialect names it) of a driver's NOT NULL refusal, read from the error's codes (better-sqlite3 `SQLITE_CONSTRAINT_NOTNULL`; libsql `extendedCode` / `rawCode: 1299`; node-postgres `23502` with its `column` / `table` fields; mysql2 `ER_BAD_NULL_ERROR` / `errno: 1048`), then its text (all sql.js gives), then one step down its `cause` chain. An error whose code names another refusal is not read for NOT NULL text. `mapDataError`'s NOT NULL branch is unchanged, and answers the column no field declares.
21+
- **`@objectstack/rest`.** The `/data` create and update doors' NOT NULL relabel (`not-null-refusal-label.ts`) retires: every path it served is an engine write.
22+
23+
Migration, for a TypeScript caller of the retired exports:
24+
25+
- FROM `notNullRefusalSite(answer)` TO `notNullViolationSite(error)`: ask the driver's error, not `mapDataError`'s answer.
26+
- FROM `relabelNotNullRefusal(answer, { message, declaresNotNull })` TO nothing (no replacement): write through the engine, which answers a declared field's NOT NULL refusal as a labelled `ValidationError` itself.

‎packages/rest/src/data-not-null-field-label-24715.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ async function boot(opts: { locale?: string; i18n?: boolean } = {}) {
154154
call('PATCH', '/api/v1/data/:object/:id', data, { object: OBJECT, id: 'e1' });
155155
const create = (data: Record<string, unknown>) => call('POST', '/api/v1/data/:object', data);
156156
const stored = async () => (await engine.find(OBJECT, { where: { id: 'e1' } }) as any[])[0];
157-
return { engine, patch, create, stored };
157+
return { engine, patch, create, stored, call };
158158
}
159159

160160
/** The catalog's sentence for `label`, in `locale` — what the record validator renders. */
@@ -214,6 +214,24 @@ describe('[#24715] a declared `storage.notNull` field\'s refusal names its label
214214
});
215215
});
216216

217+
describe('[#24768] the batch rows answer the same refusal by the same label', () => {
218+
it('`POST /data/:object/batch`, a create row with no value: the row\'s error is the validator\'s code and sentence', async () => {
219+
const { call, engine } = await boot();
220+
const door = await call('POST', '/api/v1/data/:object/batch', {
221+
operation: 'create',
222+
records: [{ data: { id: 'b1', name: 'new', list_price: 1, drift_col: 'd' } }],
223+
options: { atomic: false },
224+
});
225+
expect(door.status, JSON.stringify(door.body)).toBe(200);
226+
expect(door.body).toMatchObject({ succeeded: 0, failed: 1 });
227+
expect(door.body.results[0].errors).toEqual([
228+
expect.objectContaining({ code: 'VALIDATION_FAILED', message: requiredSentence('Sales Price', 'unit_price') }),
229+
]);
230+
expect(JSON.stringify(door.body)).not.toMatch(/constraint failed|unit_price is required/);
231+
expect(await engine.find(OBJECT, { where: { id: 'b1' } })).toHaveLength(0);
232+
});
233+
});
234+
217235
describe('[#24715] localized the way the record validator localizes its own `required`', () => {
218236
it('zh-CN: the translated label in the catalog\'s zh-CN sentence — the validator\'s template, word for word', async () => {
219237
const { patch, create } = await boot({ locale: 'zh-CN', i18n: true });

0 commit comments

Comments
 (0)