Repository navigation
Commit 9e6df40
<!-- claude-projects-attribution: {"github_login":"os-steve"} -->
_Requested by **Steve** · [project
thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi92VQfXZ8RGvh3U16jJnRjhT)_
Fixes #29176
Clause-②: no
Metadata-protocol: none
## 维护者速读
- **改了什么**:可选的唯一文本字段(SKU、税号、工号这类)被清空时,服务端写入口把 `''` 存成 `null`。第二条清空的记录不再被
`409 UNIQUE_VIOLATION` 拒绝;REST、批量、导入都走这同一个入口。
- **为什么改**:唯一索引把 `''` 当成一个真实值,而 `null`
互不冲突。用户清空第二条记录就保存不了,只能填假值污染数据。按分诊裁决修在服务端写路径(`packages/objectql`),不改表单。
- **风险与回滚**:只动「可选 + 有唯一约束 + 文本类」字段上恰好为 `''` 的值;必填字段照旧拒绝,真重复照旧
409,`password`/`secret` 的空串拒绝不变,非唯一字段仍存 `''`。已存的 `''` 不迁移。回滚 = revert 本
PR。
- **先看**:`packages/objectql/src/validation/record-validator.ts` 的
`optionalUniqueTextColumns` 与 `normalizeBlankTypedRow`
改动的那一行;`packages/rest/src/rest-data-optional-unique-blank.test.ts`;`.changeset/29176-optional-unique-blank-null.md`。
- **两分钟验证**:`pnpm --filter @objectstack/rest exec vitest run
src/rest-data-optional-unique-blank.test.ts`,预期 8 个测试全绿(主干上同一文件 6
红:第二次清空是 409)。
- **需要你做的**:无,席位落地。
## What changes
- **The seam.** `normalizeBlankTypedValues`
(`packages/objectql/src/validation/record-validator.ts`) is the engine's
existing blank write door: `ObjectQL.insert()` / `update()` and the dry
run `validate()` apply it to the caller's payload before the middleware,
the caller snapshots, the defaults, the hooks and validation read it
(#20308 put it there for non-string-typed columns). Every REST, batch,
upsert and import door reaches the engine through those methods.
`engine.ts` is not touched.
- **The new arm.** Exactly `''` becomes `null` on a column that is all
of:
- text-like: the spec's `STRING_VALUE_TYPES`, read as the constant,
minus `password` and `secret`;
- not `required: true`;
- covered by a unique constraint, on either surface the drivers build
one from: the field's own `unique`, or membership in a declared
`indexes[]` entry carrying `unique`. Both are read through the spec's
`isUniqueDeclared`, the predicate the drivers use.
- Lifecycle columns the engine owns (`id`, `created_at`, …) are never
rewritten.
- **Edges, each deliberate.**
- Whitespace stays a value. The `$empty` operator's ruled table reads
`''` and `null` as one answer on a text-like column and whitespace as a
value, so this rewrite changes no `$empty` / `is_empty` answer.
- `required` columns are excluded, so a required unique field's `''` is
refused `required` exactly as today. Without the exclusion, a required
field with a `defaultValue` would take the default instead of being
refused.
- `password` / `secret` are excluded: a maintainer ruling refuses `''`
on them while `null` means clear, and rewriting would turn that refusal
into a silent clear.
- `select` / `radio` / `lookup` / `user` are excluded: the same `$empty`
table reads their `''` as a value. See Acceptance notes.
- **Defaults.** An optional unique field with a `defaultValue` takes it
on insert when sent `''`, exactly as when sent `null` (the typed arm
already behaves this way).
- `FieldDef` in the validator gains `unique?: unknown`; the function's
parameter type gains `indexes?: unknown`. Both widen only.
## Why
Triage 6573215352 ruled the layer: "the fix lands on the server write
path in packages/objectql: normalise an empty string to null for an
optional field before the unique index sees it, which also covers REST
and import callers." The scope is the fields a unique constraint covers.
A non-unique optional text field keeps storing `''`: the validator's
docblock, the `str_empty` round-trip case and two existing pins
(`engine-blank-typed-value-door.test.ts`,
`rest-data-blank-typed-value.test.ts`) depend on that, and none of them
needed an edit.
## Tests
No existing test was edited. Three new files:
-
`packages/objectql/src/validation/record-validator.optional-unique-blank.test.ts`
(9 tests): the population pinned to the spec set (12 types), all three
unique scopes, the declared-index surface (a plain index member keeps
`''`), the controls (no unique, `unique: false`, required, credentials,
other string-stored types, a lifecycle column), whitespace and non-blank
pass-through, purity.
- `packages/objectql/src/engine-optional-unique-blank-door.test.ts` (8
tests): what the driver receives on insert, insert array, `insertMany`,
update by id, update by predicate; whitespace and non-blank untouched;
the `defaultValue` interaction; `required` unchanged on insert, update
and the dry run, including the required-with-default case; `''` on a
unique `password` still refused (`EmptyCredentialWriteError`,
`VALIDATION_ERROR` / 400).
- `packages/rest/src/rest-data-optional-unique-blank.test.ts` (8 tests),
real `ObjectQL` + SQLite `SqlDriver` + `RestServer` routes, physical
column read past the engine: POST `''` twice, PATCH clear twice, a
declared composite unique index member, batch create and `createMany`, a
non-unique control keeping `''`, a real duplicate still `409
UNIQUE_VIOLATION` (by POST and by PATCH), a required unique field `400
VALIDATION_FAILED` / `required` with and without a `defaultValue`, and a
legacy stored `''` row.
<details><summary>Red on main, green on head</summary>
`record-validator.ts` restored to `dedc3505` in the tree only, objectql
rebuilt (`optionalUniqueTextColumns` absent from all 14 built files),
then restored to HEAD (blob `b450ff13` equal to HEAD, `git diff HEAD`
empty) and rebuilt.
| Reading | objectql (2 new files) | rest (new file) |
|---|---|---|
| Base `dedc3505` | `Tests 9 failed, 8 passed (17)` | `Tests 6 failed, 2
passed (8)`: POST `[201, 409]`, PATCH `[200, 409]`, declared index
`[201, 409]`, batch `[true, false]` |
| Head | `Tests 17 passed (17)` | `Tests 8 passed (8)` |
The controls that pass on base are the ones that must: required
refusals, the credential refusal, the non-unique `''`.
</details>
<details><summary>Ablations of the three negative pins</summary>
Each mutation was made with `scripts/ablation-replace.mjs` (anchor hit 1
→ 0, blob changed), objectql rebuilt, and the marker counted in dist by
`scripts/ablation-dist-preflight.mjs` (present in 4 built files) before
the run. Each was restored to the HEAD blob with `git diff HEAD` empty.
A final rebuild shows all three markers absent and the tree clean.
| Ablation | Red |
|---|---|
| A1: the door also rewrites a non-blank value on a covered column |
rest `a real duplicate is still refused`: `expected 201 to be 409`;
objectql `whitespace and every non-blank value pass through untouched` |
| A2: the `required` exclusion removed | objectql `… with a defaultValue
refuses "" — it is NOT defaulted` and the unit `required` control; rest
`a required unique field refuses ""`: `expected 201 to be 400` |
| A3: the credential exclusion removed | objectql `the credential ruling
is unchanged`: `expected undefined to be an instance of
EmptyCredentialWriteError`, and the unit credentials control |
</details>
## Acceptance notes
- **Driver families, measured on `dedc3505`** (two rows in a unique text
column, through each driver's own `create`): driver-sql on SQLite: two
`''` → second refused, two `null` → both land. driver-memory: the same.
sqlite-wasm and turso local share `SqlDriver`'s compiler and DDL path,
so they were not measured separately. The engine now hands every driver
`null`; driver-memory already pins NULL-distinct in
`memory-unique-constraint.test.ts`.
- **MongoDB, NOT MEASURED** (the `mongod` download for
`mongodb-memory-server` is refused with 403 here). By reading:
`mongodb-schema.ts` builds field uniqueness as `{ unique: true, sparse:
true }`. A sparse index skips a document only when the field is absent,
and still indexes an explicit `null`. If that holds, two cleared rows
still collide on that driver, because the engine now sends `null`. That
would be a driver divergence from the NULL-distinct rule the other
drivers keep, and it is outside this card's ruled layer. Reported to the
seat.
- **Not covered, by scope.** A unique single-value `select` / `lookup`
that is cleared also sends `''` (the card's own reading on
`showcase_field_zoo`) and still collides on the second record. The
`$empty` table reads `''` as a value on those types, so rewriting it is
a separate decision. Reported to the seat.
- **Stored rows.** Existing `''` values are not migrated (pinned): they
keep matching `field = ''`; a newly cleared row matches `$null: true`;
`$empty: true` matches both; a new clear no longer collides with a
stored `''`.
## Verification
Head after `git merge origin/main`: see the PR's check-runs. Local
readings:
<details><summary>Suites, typecheck, gates, lint</summary>
| Run | Head | Result |
|---|---|---|
| `pnpm --filter @objectstack/objectql test` | `9ef4c2cf` | Test Files
529 passed (529) · Tests 10155 passed (10155) |
| `pnpm --filter @objectstack/objectql typecheck` | `9ef4c2cf` | exit 0;
`check:test-typecheck` held (new files carry no debt) |
| `pnpm --filter @objectstack/rest test` | `9ef4c2cf` | Test Files 368
passed (368) · Tests 6971 passed, 332 skipped (7303) |
| `pnpm --filter @objectstack/rest typecheck` | `9ef4c2cf` | exit 0; 0
test-typecheck debt |
| `git merge origin/main` (`49aba82b`), then `pnpm install
--frozen-lockfile`, rebuild of the rest closure, `spec check:generated`
| `8c537232` | all exit 0; every generated artifact up to date. The
incoming commits touch neither objectql nor rest, so the scoped re-check
is the three new files: 17/17 and 8/8 |
| the one later commit (test helpers renamed, see below) | `28fb6173` |
the edited file 8/8 |
| `dispatch-gates.mjs --commands` (61 families), each run | `28fb6173` |
61 × exit 0; `--ran`: 61 derived, 61 run, 0 NOT-MEASURED, 0 UNRUN |
| `pnpm lint` | `28fb6173` | exit 0 |
| `check:driver-conformance` before (`49aba82b`) and after | `28fb6173`
| 66 covered, 0 DEBT, both |
The first gate round (`8c537232`) had one real red:
`check:where-matcher` discovered the two expectation helpers in
`engine-optional-unique-blank-door.test.ts` as WHERE matchers it could
not judge, because they iterated the keys of a payload object. They now
iterate a list of names, as the sibling blank-door test does
(`28fb6173`). In the same round, two families exited 3 (prerequisite not
met, nothing measured): `check-plugin-teardown-shape --self-test` needed
its pinned fixture commit fetched, and `check:dual-build-cjs-loads`
needed every package built. Both were fixed before round two.
Not run locally: the full dogfood battery. The title carries no `!` and
`Clause-②` has no `(narrowing)` arm, so it is not owed.
</details>
## Human Minimum
- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
(not touched)
- [x] 2. Every name I added uses the casing for its kind.
- [x] 3. No formatter ran; `pnpm lint` passes.
- [x] 4. One issue, one branch off an up-to-date `main`, one pull
request.
- [x] 5. A change that publishes carries a changeset, and both
declaration lines are filled in here and in the changeset.
- [x] 6. `packages/spec` touched: n/a.
- [x] 7. No hand edits to `content/docs/references/`,
`packages/*/CHANGELOG.md` or `content/docs/releases/`.
- [x] 8. Title, body and comments are in English (the maintainer brief
above is Chinese by repo rule).
---
_Generated by [Claude
Code](https://claude.ai/code/session_01ADHkmPtnUk1SkRHh5gtX6u)_
---------
Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6dd6a53 commit 9e6df40
5 files changed
Lines changed: 680 additions & 12 deletions
File tree
- .changeset
- packages
- objectql/src
- validation
- rest/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
0 commit comments