JSON配列のYAML Schemaを定義する
JSON配列をitems制約付きのYAML Schema定義に変換する方法を学びます。型付き配列、オブジェクトの配列、タプル検証、minItems/maxItems、uniqueItemsを解説します。
Basic Schemas
詳細な説明
JSON配列からYAML Schemaへ
JSON配列はYAML Schemaでtype: arrayにマッピングされ、itemsキーワードで要素型を記述します。コンバーターは配列要素を検査して正しいアイテムスキーマを推論します。
プリミティブ配列
{
"tags": ["api", "production", "v2"],
"ports": [80, 443, 8080]
}
type: object
properties:
tags:
type: array
items:
type: string
ports:
type: array
items:
type: integer
オブジェクトの配列
{
"users": [
{ "id": 1, "name": "Alice", "role": "admin" },
{ "id": 2, "name": "Bob", "role": "viewer" }
]
}
type: object
properties:
users:
type: array
items:
type: object
properties:
id:
type: integer
name:
type: string
role:
type: string
required:
- id
- name
- role
配列制約
YAML Schemaは配列固有の制約をいくつかサポートしています:
tags:
type: array
items:
type: string
minItems: 1
maxItems: 10
uniqueItems: true
| 制約 | 目的 |
|---|---|
minItems |
要素の最小数 |
maxItems |
要素の最大数 |
uniqueItems |
すべての要素が一意でなければならない |
タプル検証
各位置が異なる型を持つ固定長配列の場合:
coordinates:
type: array
items:
- type: number
description: 緯度
- type: number
description: 経度
minItems: 2
maxItems: 2
空の配列
空のJSON配列([])は曖昧です -- コンバーターはアイテムの型を判別できません。多くのコンバーターはデフォルトでitems: {}(何でも受け入れる)にします。生成後に正しいアイテム型を手動で設定してください。
ネストされた配列
配列の配列([[1,2],[3,4]])は以下を生成します:
matrix:
type: array
items:
type: array
items:
type: integer
このパターンは行列データ、バッチ操作、グリッド設定で使用されます。
ユースケース
ロードバランサー、ファイアウォールルール、CIパイプラインステップの設定ファイルはオブジェクトの配列を広く使用します。適切な配列アイテム定義を持つYAMLスキーマにより、リスト内のすべての要素が期待される形状に一致することが保証され、ランタイム障害を引き起こす前に不正な形式のエントリを検出します。