OOZOU
與我們聯繫
返回 TIL 列表
ruby on rails

在 collection JSON schema 中使用 minItems/maxItems

October 13, 2020

要讓你的 collection JSON schema 更可靠,請使用 minItems 和 maxItems,這樣你才能信任你的 API。

{
  "type": "array",
  "minItems": 1,
  "items": {
    "$ref": "subscription_notification.json"
  }
}

而不是

{
  "type": "array",
  "items": {
    "$ref": "subscription_notification.json"
  }
}

如果你的 API 回傳 [] 空陣列,當你在 spec 中做斷言時,後面那個 schema 仍然會通過。

expect(response).to match_response_schema(:subscription_notifications)

但前面那個就不會

The property '#/' did not contain a minimum number of items 1 in schema

<https://json-schema.org/understanding-json-schema/reference/array.html>

認識 JSON Schema 陣列

JSON Schema 陣列對於定義 JSON 資料中陣列的結構和限制非常重要。JSON 的陣列是一組有序的值,而 JSON Schema 陣列則用來指定這些陣列的特性和限制。透過 JSON Schema 陣列,你可以驗證陣列的長度、內容和唯一性,確保資料符合預期的格式。當你需要對資料結構強制執行特定規則時,例如要求一定數量的項目,或確保所有項目都是特定型別,這就特別有用。

陣列的驗證關鍵字

JSON Schema 提供了幾個可以用來限制陣列的驗證關鍵字。這些關鍵字包括:

  • items:指定陣列中每個項目的 schema。
  • additionalItems:指定陣列中超出第一個 items schema 的額外項目的 schema。
  • minItems 和 maxItems:指定陣列中項目數量的下限和上限。
  • uniqueItems:指定陣列中的項目是否必須唯一。
  • contains:指定陣列中至少要有一個項目符合的 schema。
  • minContains 和 maxContains:指定陣列中必須符合 contains schema 的項目數量下限和上限。

這些關鍵字讓你可以對陣列定義精確的限制,確保資料符合要求。

限制陣列長度

JSON Schema 提供了幾種限制陣列長度的方式。minItems 和 maxItems 關鍵字可以用來指定陣列中項目數量的下限和上限。例如:{ "type": "array", "minItems": 2, "maxItems": 5 } 這個 schema 規定陣列必須至少有 2 個項目、最多 5 個項目。使用這些關鍵字,你就能確保陣列長度落在想要的範圍內,避免項目太少或太多的問題。

陣列驗證的最佳實務

用 JSON Schema 驗證陣列時,建議搭配多個驗證關鍵字,確保陣列符合所需的限制。舉例來說,你可以用 items 指定陣列中每個項目的 schema,並用 minItems 和 maxItems 指定陣列中項目數量的下限和上限。

如果資料有唯一性需求,也建議使用 uniqueItems 關鍵字來確保陣列中的項目不重複。把這些關鍵字組合起來,你就能建立一個穩健的 schema,徹底驗證陣列的結構和內容。

使用範例

以下是幾個 JSON Schema 陣列的使用範例:

  • 驗證使用者 ID 清單:你可以用像這樣的 schema 來驗證使用者 ID 清單:
{ "type": "array", 
"items": {"type": "integer"}, 
"minItems": 1, "maxItems": 10 } 
  • 驗證產品名稱清單:你可以用像這樣的 schema 來驗證產品名稱清單:
{ "type": "array", "items": {"type": "string"}, "minItems": 1, "maxItems": 5, "uniqueItems": true } 
  • 驗證地址清單:你可以用像這樣的 schema 來驗證地址清單:
{ "type": "array", "items": { "type": "object", "properties": { "street": {"type": "string"}, "city": {"type": "string"}, "state": {"type": "string"}, "zip": {"type": "string"} }, "required": ["street", "city", "state", "zip"] }, "minItems": 1, "maxItems": 10 } 

這些範例說明了如何把 JSON Schema 陣列套用在不同類型的資料上,確保陣列符合指定的限制和需求。

更多關於 ruby on rails 的筆記

ruby on railsFebruary 10, 2022

用 Pundit Policy 管理 permitted parameters、預設值與各 action 的參數

要管理 permitted params,你可以在你的 policy 中加入 permitted\attributes 和 permitted\attributes\for\#{action},例如

ruby on railsJanuary 31, 2022

在 Turbo 中用 `requestSubmit` 送出表單

Turbo 會監聽 form 上的 submit 事件。因此,用 JS 送出表單時,請一律使用 requestSubmit。

ruby on railsJanuary 19, 2022

在 GitHub Actions 中讓 Rubocop 與 ESLint 忽略 bundled gems

Rails 專案在 GitHub actions 中通常會把 gems 安裝到 vendor 目錄,方便在後續執行時使用快取。

有專案構想嗎?

我們很樂意聆聽您正在打造的產品。

開始對話hello@oozou.com
OOZOU Logo

曼谷 · 新加坡 · 香港

X
Facebook
Instagram
LinkedIn

服務

  • Web 開發
  • AI 代理人與生成式 AI
  • 行動應用開發
  • 數據分析與工程
  • UI/UX 與產品設計
  • 數位轉型

公司

  • 關於我們
  • 職涯
  • 部落格
  • 案例研究
  • Today I Learned
  • 聯繫我們

更多

  • 產業
  • 合作夥伴網絡
© 2026 OOZOU. 版權所有。
隱私政策行為準則反賄賂反貪腐政策