【发布时间】:2021-07-27 22:04:03
【问题描述】:
我是使用 JSON 模式的新手,我对子模式很困惑。我做了很多搜索并阅读了https://json-schema.org/understanding-json-schema/structuring.html,但我觉得我没有得到一些基本概念。
我想将一个架构分解为几个文件。例如,我有一个我想嵌套在类别模式中的度量模式。子模式可以是被引用的单独文件,还是与基本模式在同一文件中的代码块?如果它们是单独的文件,您如何引用另一个文件?我尝试使用嵌套文件的 $id 为 $ref 使用很多不同的值,但它似乎不起作用。
我认为我并不真正了解 $id 和 $schema 字段。我已经阅读了他们的文档,但仍然感到困惑。 $id 是否需要是有效的 URI?文档似乎说他们没有。我只是从 jsonschema 站点示例中复制了 $schema 值。
如果我做错了什么,我们将不胜感激。
(在以太回复后添加以下内容) 我得到的错误信息是:
KeyError: 'http://mtm/metric'
和变化
jsonschema.exceptions.RefResolutionError: HTTPConnectionPool(host='mtm', port=80): Max retries exceeded with url: /metric (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fe9204a31c0>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known'))
这是 category_schema.json 中的类别架构:
{
"$id": "http://mtm/category",
"$schema":"https://json-schema.org/draft/2020-12/schema",
"title":"Category Schema",
"type":"object",
"required":["category_name", "metrics"],
"properties": {
"category_name":{
"description": "The name of the category.",
"type":"string"
},
"metrics":{
"description": "The list of metrics for this category.",
"type":"array",
"items": {
"$ref": "/metric"
}
}
}
}
这是 metric_schema.json 中的度量模式:
{
"$id": "http://mtm/metric",
"$schema":"https://json-schema.org/draft/2020-12/schema",
"title":"Metric Schema",
"description":"Schema of metric data.",
"type":"object",
"required": ["metric_name"],
"properties": {
"metric_name":{
"description": "The name of the metric in standard English. e.g. Live Views (Millions)",
"type":"string"
},
"metric_format": {
"description": "The format of the metric value. Can be one of: whole, decimal, percent, or text",
"type": "string",
"enum": ["integer", "decimal", "percent", "text"]
}
}
}
【问题讨论】:
-
我一直在努力更新理解 JSON Schema 站点,如果您就您发现的困惑提供任何反馈,我将不胜感激。 github.com/json-schema-org/understanding-json-schema
标签: jsonschema python-jsonschema