when 前置條件
when 讓規則只在目前列符合條件時才執行。它使用與 filter predicate 相同的陣列 shape,因此前端可以共用欄位選擇器、operator 控制項與型別驗證,不必再設計一套規則專用 DSL。
Shape 與 AND 語意
{
"type": "require",
"name": "已送出案件必須有負責人",
"column": "負責人",
"when": [
{ "column": "狀態", "op": "eq", "value": "已送出" },
{ "column": "已封存", "op": "eq", "value": false }
]
}陣列內最多 5 個 predicate,全部以 AND 組合。column 可用顯示名稱或內部 col_<hex> key 輸入;伺服器會解析並儲存 rename-stable key。可用的欄位是 stored scalar 型別——string、text、integer、float、decimal、boolean、date、datetime、select、user、social_client、principal——以及 json;json 只接受 eq/neq(對任何 JSON 值做深度比較)與 is_null/is_not_null。Link、rollup、lookup、formula、multi_select、interval 與 attachment 欄位會被拒絕。
座標 extension 另允許 coordinate 欄位上的 spatial predicates,但 aggregate_limit.when 會在 rule authoring 拒絕這些 predicates;其具型別 value shape 與 indeterminate 處理見座標 consumers:一般 rule condition 對不確定 capture/origin 拒絕,approval 與 pre-image lock condition 則保守地視該 predicate 為命中。
每段 native-link origin hop、target location/欄位型 radius 及 origins.where filter 欄位,都必須以 managers/none 保護寫入。Authoring 檢查、求值重查,ACL PATCH/IaC 也保護這些 dependencies;保護失效為 origin_untrusted。Strict-accuracy 不確定性為 accuracy_ambiguous,不是原點缺失。Record-based date_column_reached scheduler 遇不確定 condition 會消耗 occurrence,記錄 done/skipped-condition reason 且不執行 action;不對所有 schedule type 宣稱相同合約。
Operator 與 value
op | value shape |
|---|---|
eq, neq, gt, gte, lt, lte, contains | 與欄位型別相符的單一 scalar(decimal 欄位要用像 "12.50" 的純十進位字串;JSON 數字會回 400 … requires a decimal string) |
in | 非空 scalar 陣列,最多 100 項 |
is_null, is_not_null | 不帶 value,或帶 null |
json 欄位上的 eq、neq | 任何 JSON 值,包括物件或陣列,以深度相等比較;"" 是真正的值,只有 null 或不存在的 key 才算 null |
json 欄位只接受這四個 operator:其他 operator 會回 400 when column '<display>' is type json — only is_null/is_not_null/eq/neq are supported,eq/neq 的值為 null 時是 op "eq" requires a value,null 類 operator 帶了值則是 op "is_null" must not set a value。require_approval 規則的 when 也由同一套比對邏輯判斷。
前端應依欄位型別縮小 operator 選項,並在送出前拒絕型別不符的值;除了 json 欄位與座標 extension 的 spatial value grammar 之外,也要拒絕物件與巢狀陣列。伺服器仍會做最終驗證。
哪些規則不能用 when
unique 與 unique_tuple 是無條件、由資料庫支撐的約束;same_dimension_reference 與 date_interval_exclusion 是結構性的完整性規則;transition 只看狀態前後值;channel 與 invariant 在各自的安全 floor 執行。immutable_when 拒絕它的理由則是第四種:when 是以 post-image 判斷,而鎖定條件若在寫入之後才讀,就會被那筆寫入自己打敗 —— 呼叫者在同一個 request 裡解鎖並修改({"Status": "draft", "Qty": 6}),Status eq posted 就永遠不會命中。它改用 prior_when,以列的 pre-image 判斷。
有八類不接受有效的 when:unique、unique_tuple、transition、channel、invariant 與 immutable_when 帶了非空的 when 會在設定階段收到 rules.set 的 400,detail 是一個點名該 type 的純字串(unique_tuple 沿用 unique rules do not take a when pre-condition (SQL-enforced; unconditional));same_dimension_reference 與 date_interval_exclusion 的 request schema 只允許 null 或 [],所以非空的 when 是 422。immutable_when 的訊息是:
immutable_when rules do not take a when pre-condition — use `prior_when`, which evaluates against the row's PRE-image對 SCP 的條件式邏輯,請使用 policy 的 if / then / else,見 channel 與 invariant。
Note
GET回來的規則可能帶有"when": null。這是可安全原樣重送的正規化表示,不必刪除。
端點與完整錯誤模型見 rules.set;可先用 rules.preview 驗證組合後的候選清單。