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
OOZOU Co., Ltd.
548 One City Centre
Ploenchit Road
Bangkok 10330
Thailand
+66 2544 0000
hello@oozou.com

服务

  • Web 开发
  • AI 智能体与生成式 AI
  • 移动应用开发
  • 数据分析与工程
  • UI/UX 与产品设计
  • 数字化转型

公司

  • 关于我们
  • 加入我们
  • 博客
  • 案例研究
  • Today I Learned
  • 联系我们

更多

  • 行业
  • 合作伙伴网络
© 2026 OOZOU. 版权所有。
隐私政策行为准则反贿赂反贪腐政策