0

我是使用 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"]
        }
    }
}
4

1 回答 1

0

是的,您可以在其他文档中引用模式,但 URI 需要正确,并且如果文件不可用网络或文件系统,则需要手动将文件添加到评估程序。

在您的第一个模式中,您声明其 uri 是“http://mtm/category”。但是你说"$ref": "/mtm/metric"——因为这不是绝对的,$id URI 将用作解析它的基础。完整的 URI 解析为“http://mtm/mtm/metric”,这与第二个模式中使用的标识符不同,因此不会找到该文档。这应该在错误消息中指出(您没有提供)。

于 2021-07-27T22:47:46.460 回答