关于 json-schema-validator
json-schema-validator 可依据 JSON Schema 校验 JSON 文档,也能从样例数据生成 Schema,适合处理 JSON 文件的开发者。它支持 Draft 4、6 和 7,并提供 API 与命令行用法。
主要功能
- 01按 Schema 校验 JSON
- 02提供详细校验错误
- 03批量校验文件
- 04从样例生成 Schema
- 05支持 Draft 4、6 和 7
适用场景
- 01按 Schema 检查 JSON 文件
- 02批量校验多个数据文件
- 03根据 JSON 样例创建 Schema
安装
复制下面的命令,在终端中运行。
$
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 验证器
根据 JSON Schema(Draft 7)规范验证 JSON 文档。
功能
- Schema 验证:根据 schema 验证 JSON
- 错误详情:提供详细的验证错误消息
- 批量验证:验证多个文件
- Schema 生成:根据示例生成 schema
- 多个草案版本:支持 Draft 4、6、7
快速开始
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 用法
# 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 参考
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
验证结果
{
"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 示例
对象 Schema
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
schema = {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {"type": "integer"},
"value": {"type": "number"}
}
},
"minItems": 1,
"uniqueItems": True
}
嵌套 Schema
schema = {
"type": "object",
"properties": {
"user": {
"type": "object",
"properties": {
"name": {"type": "string"},
"address": {
"type": "object",
"properties": {
"street": {"type": "string"},
"city": {"type": "string"}
}
}
}
}
}
}
从数据生成 Schema
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:数组项的 schemaminItems、maxItemsuniqueItemscontains
对象验证
properties:属性的 schemarequired:必需属性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