OOZOU
お問い合わせ
Back to TIL
ruby on rails

Use minItems/maxItems in collection JSON schemas

2020年10月13日

To make your collection JSON schemas more reilable use minItems and maxItems so that you can trust your API.

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

instead of

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

If your API returns [] empty array that last one would pass if you make an assertion in your specs.

expect(response).to match_response_schema(:subscription_notifications)

But not the first one

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

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

Understanding JSON Schema Arrays

JSON Schema arrays are essential for defining the structure and constraints of arrays in JSON data. An array in JSON is an ordered collection of values, and a JSON Schema array is used to specify the characteristics and restrictions of these arrays. By using a JSON Schema array, you can validate the length, contents, and uniqueness of an array, ensuring that the data adheres to the expected format. This is particularly useful when you need to enforce specific rules on the data structure, such as requiring a certain number of items or ensuring that all items are of a particular type.

Validation Keywords for Arrays

JSON Schema provides several validation keywords that can be used to constrain arrays. These keywords include:

  • items: Specifies the schema for each item in the array.
  • additionalItems: Specifies the schema for any additional items in the array beyond the first items schema.
  • minItems and maxItems: Specify the minimum and maximum number of items in the array.
  • uniqueItems: Specifies whether the items in the array must be unique.
  • contains: Specifies a schema that at least one item in the array must match.
  • minContains and maxContains: Specify the minimum and maximum number of items in the array that must match the contains schema.

These keywords allow you to define precise constraints on the array, ensuring that the data meets the required criteria.

Constraining Array Length

JSON Schema provides several ways to constrain the length of an array. The minItems and maxItems keywords can be used to specify the minimum and maximum number of items in the array. For example: { "type": "array", "minItems": 2, "maxItems": 5 } This schema specifies that the array must have at least 2 items and at most 5 items. By using these keywords, you can ensure that the array length falls within the desired range, preventing issues with too few or too many items.

Best Practices for Array Validation

When validating arrays with JSON Schema, it’s a good idea to use a combination of validation keywords to ensure that the array meets the required constraints. For example, you might use items to specify the schema for each item in the array, and minItems and maxItems to specify the minimum and maximum number of items in the array.

It’s also a good idea to use the uniqueItems keyword to ensure that the items in the array are unique, if that’s a requirement for your data. By combining these keywords, you can create a robust schema that thoroughly validates the array structure and contents.

Example Use Cases

Here are a few example use cases for JSON Schema arrays:

  • Validating a list of user IDs: You might use a schema like this to validate a list of user IDs:
{ "type": "array", 
"items": {"type": "integer"}, 
"minItems": 1, "maxItems": 10 } 
  • Validating a list of product names: You might use a schema like this to validate a list of product names:
{ "type": "array", "items": {"type": "string"}, "minItems": 1, "maxItems": 5, "uniqueItems": true } 
  • Validating a list of addresses: You might use a schema like this to validate a list of addresses:
{ "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 } 

These examples illustrate how to apply JSON Schema arrays to different types of data, ensuring that the arrays meet the specified constraints and requirements.

More on ruby on rails

ruby on rails2022年2月10日

Use Pundit Policies to manage permitted parameters, defaults and per action

To manage permitted params you can add permitted\attributes and permitted\attributes\for\#{action} in your policies e.g.

ruby on rails2022年1月31日

Use `requestSubmit` for form submits with Turbo

Turbo listens to submit events on form.  Hence, always use requestSubmit to submit  a form using JS.

ruby on rails2022年1月19日

Ignore bundled gems for Rubocop and ESLint in GitHub Actions

Rails projects will often install gems into the vendor directory in GitHub actions so we can cache them for the consecutive runs.

プロジェクトをお考えですか?

あなたが構築しているものについてお聞かせください。

会話を始めるhello@oozou.com
OOZOU Logo

バンコク · シンガポール · 香港

X
Facebook
Instagram
LinkedIn

サービス

  • ウェブ開発
  • AI・生成AIソリューション
  • モバイルアプリ開発
  • データ分析・エンジニアリング
  • UI/UXデザイン・プロダクトデザイン
  • デジタルトランスフォーメーション

会社情報

  • 会社概要
  • 採用情報
  • ブログ
  • ホワイトペーパー
  • 事例研究
  • Today I Learned
  • お問い合わせ

その他

  • 業界
  • 見積もりを取得
  • パートナーネットワーク
© 2026 OOZOU. All rights reserved.
プライバシーポリシー行動規範ABACポリシー