compare 規則
compare 約束同一列中兩個欄位的關係,例如「開始日不得晚於結束日」或「實付金額不得高於核准額度」。把這類契約放在伺服器端,可讓所有寫入入口得到相同判斷。
Payload
{
"type": "compare",
"name": "開始日不得晚於結束日",
"left": "開始日",
"op": "lte",
"right": "結束日",
"when": [
{ "column": "狀態", "op": "neq", "value": "草稿" }
]
}op 可用 lt、lte、gt、gte、eq、neq。兩側都必須是 stored scalar,且屬於同一可比較類別:numeric、date、datetime 或 string。Boolean 只能與 boolean 進行 eq / neq。Principal 欄位同理但更嚴格:只能 eq / neq,而且只能對照同一種 principal 型別的另一個欄位——principal、user、social_client 是三個各自獨立的可比較類別,因為帶標籤的 cell 永遠不可能等於裸 id(400 principal column '<display>' supports only eq/neq against another column of the same principal type)。
執行語意
規則在 create 與 update 的新值上判斷。任一側缺值或為空字串時,本規則不否決;若欄位本身必填,另加 require。需要只對部分列套用時,加入 when。
Tip 日期與 datetime 是不同的比較類別。不要依賴隱式轉換;若產品需要跨型別比較,先把資料模型正規化。
把這個物件放進完整 rules 陣列,先呼叫 rules.preview,再以 rules.set 發布。
Last updated on