jsonschemaで「オブジェクトの配列」を定義する (pythonで実装)

# 概要

- こういうjsonのデータの jsonschema を定義したい

```python
employees = [
    {
        "id": 12,
        "name": "Tom",
    },
    {
        "id": 24,
        "name": "Bob",
        "age": 28,
    },
]
```

# 環境

- Ubuntu 20.04.2 LTS
- Python 3.8.10 (venv)
- jsonschema 3.2.0

# パッケージのインストール

```
pip install jsonschema 
```

# シンプルな方法

- "type" 要素を "array" に設定
- "items" 要素を定義して、
- "items" 要素の値に 「"type" が "object"の値」を定義

```python:jsonschema_array.py
import json
from jsonschema import validate, ValidationError


def main():
    """
    https://json-schema.org/learn/miscellaneous-examples.html
    """
    employee_jsonschema = {
        "$id": "",
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "description": "Array of things without ref/defs keyword.",
        "type": "array",
        "items": {
            "type": "object",
            "required": ["id", "name"],
            "properties": {
                "id": {
                    "type": "number"
                },
                "name": {
                    "type": "string"
                },
                "age": {
                    "type": "number"
                }
            },
            "additionalProperties": False
        }
    }

    employees = [
        {
            "id": 12,
            "name": "Tom",
        },
        {
            "id": 24,
            "name": "Bob",
            "age": 28,
        },
    ]

    result = "OK"

    try:
        validate(employees, employee_jsonschema)
    except ValidationError as e:
        result = e.message

    print(result)


if __name__ == '__main__':
    main()


# OK
```

# 参照を使う例

- ネストが深くなって見づらくなるのを回避
- ref と defs というキーワードを利用
- ref / defsを紐づけるための id (この例では、employee）を設定

```python:jsonschema_array_ref.py
import json
from jsonschema import validate, ValidationError


def main():
    """
    https://json-schema.org/learn/miscellaneous-examples.html
    """
    employee_jsonschema = {
        "$id": "",
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "description": "Array of things using by ref/defs keyword.",
        "type": "array",
        "items": {"$ref": "#/$defs/employee"},
        "$defs": {
            "employee": {
                "type": "object",
                "required": ["id", "name"],
                "properties": {
                    "id": {
                        "type": "number"
                    },
                    "name": {
                        "type": "string"
                    },
                    "age": {
                        "type": "number"
                    }
                },
                "additionalProperties": False
            }
        }
    }

    employees = [
        {
            "id": 12,
            "name": "Tom",
        },
        {
            "id": 24,
            "name": "Bob",
            "age": 28
        },
    ]

    result = "OK"

    try:
        validate(employees, employee_jsonschema)
    except ValidationError as e:
        result = e.message

    print(result)


if __name__ == '__main__':
    main()


# OK
```


# 参考

- *1 https://json-schema.org/learn/miscellaneous-examples.html