json-schema-validator について
json-schema-validatorは、JSON Schemaに照らしてJSON文書を検証し、サンプルデータからスキーマを生成します。JSONファイルを扱う開発者向けで、Draft 4、6、7に対応し、APIとコマンドラインから利用できます。
主な機能
- 01スキーマに基づくJSON検証
- 02詳細な検証エラーの表示
- 03複数ファイルの一括検証
- 04サンプルからのスキーマ生成
- 05Draft 4、6、7への対応
活用シーン
- 01スキーマに照らしたJSONファイルの確認
- 02複数データファイルの一括検証
- 03JSONサンプルからのスキーマ作成
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ dkyazzentwatwa/ chatgpt-skills --skill json-schema-validatorインストール後、Claude Code で/json-schema-validatorを入力して読み込まれたか確認します。
同梱ファイルscripts/json_validator.pyscripts/requirements.txt
詳細
JSON Schema Validator
JSON Schema(Draft 7)の仕様に照らして JSON ドキュメントを検証します。
Features
- スキーマ検証:スキーマに照らして JSON を検証します
- エラーの詳細:詳細な検証エラーメッセージを表示します
- 一括検証:複数のファイルを検証します
- スキーマ生成:サンプルからスキーマを生成します
- 複数のドラフト:Draft 4、6、7 をサポートします
Quick Start
from json_validator import JSONValidator
validator = JSONValidator()
# Validate data against schema
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "minimum": 0}
},
"required": ["name"]
}
data = {"name": "John", "age": 30}
result = validator.validate(data, schema)
print(f"Valid: {result['valid']}")
CLI Usage
# Validate JSON file against schema
python json_validator.py --data user.json --schema user_schema.json
# Validate with inline schema
python json_validator.py --data config.json --schema '{"type": "object"}'
# Generate schema from sample
python json_validator.py --generate sample.json --output schema.json
# Batch validate directory
python json_validator.py --data-dir ./configs/ --schema config_schema.json
API Reference
JSONValidator クラス
class JSONValidator:
def __init__(self, draft: str = "draft7")
# Validation
def validate(self, data: dict, schema: dict) -> dict
def validate_file(self, data_path: str, schema_path: str) -> dict
def validate_batch(self, data_files: list, schema: dict) -> list
# Schema operations
def generate_schema(self, data: dict) -> dict
def check_schema(self, schema: dict) -> dict
Validation Result
{
"valid": True, # or False
"errors": [], # List of error details
"path": "$", # JSON path to data root
}
# With errors:
{
"valid": False,
"errors": [
{
"message": "'name' is a required property",
"path": "$",
"schema_path": "required"
},
{
"message": "-5 is less than minimum 0",
"path": "$.age",
"schema_path": "properties.age.minimum"
}
]
}
Schema Examples
オブジェクトスキーマ
schema = {
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string", "minLength": 1},
"email": {"type": "string", "format": "email"},
"active": {"type": "boolean", "default": True}
},
"required": ["id", "name", "email"],
"additionalProperties": False
}
配列スキーマ
schema = {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {"type": "integer"},
"value": {"type": "number"}
}
},
"minItems": 1,
"uniqueItems": True
}
ネストされたスキーマ
schema = {
"type": "object",
"properties": {
"user": {
"type": "object",
"properties": {
"name": {"type": "string"},
"address": {
"type": "object",
"properties": {
"street": {"type": "string"},
"city": {"type": "string"}
}
}
}
}
}
}
データからスキーマを生成
validator = JSONValidator()
sample_data = {
"name": "Product A",
"price": 29.99,
"tags": ["electronics", "sale"],
"inStock": True
}
schema = validator.generate_schema(sample_data)
# Returns inferred schema based on data types
サポートされているキーワード
型の検証
type:string、number、integer、boolean、array、object、nullenum:許可される値const:厳密な値
文字列の検証
minLength、maxLengthpattern:正規表現パターンformat:email、uri、date、date-time、ipv4、ipv6、uuid
数値の検証
minimum、maximumexclusiveMinimum、exclusiveMaximummultipleOf
配列の検証
items:項目のスキーマminItems、maxItemsuniqueItemscontains
オブジェクトの検証
properties:プロパティのスキーマrequired:必須プロパティadditionalPropertiesminProperties、maxPropertiespatternProperties
エラー処理
result = validator.validate(data, schema)
if not result['valid']:
for error in result['errors']:
print(f"Error at {error['path']}: {error['message']}")
依存関係
- jsonschema>=4.20.0
コメント
Markdown 対応