Python - 验证模式,试图要求一个字段

Python - Validating Schemas, trying to require a field

大家好!我感谢您的帮助。这是我第一次创建模式,它涉及航班数据,但我遇到了多个错误,因为在某些记录中,它不涉及特定字段(在本例中为 dst_airport)。

这是我包含的内容:

        "dst_airport": {
            "type": "object",
            "properties": {
                 "airport_id": {
                     "type": "number"
                 },
                 "name": {
                     "type": "string"
                 },
                 "city": {
                     "type": "string"
                 },
                 "country": {
                     "type": "string"
                 },
                 "iata": {
                     "type": "string"
                 },
                 "iaco": {
                     "type": "string"
                 },
                 "latitude": {
                     "type": "number"
                 },
                  "longitude": {
                     "type": "number"
                 },
                  "altitude": {
                     "type": "number"
                  },
                  "timezone": {
                     "type": "number"
                  },
                  "dst": {
                     "type": "string"
                  },
                  "tz_id": {
                     "type": "string"
                  },
                  "type": {
                     "type": "string"
                  },
                  "source": {
                     "type": "string"
                  },
                "anyOf": [
                    {
                        "required": [
                            "dst_airport"
                            ]
                    }
                    ]
                }
        }

我添加了我在网上找到的 anyof 部分试图修复它,但我认为它实际上没有任何作用。

有人能帮忙吗?谢谢!

required 应该与定义它们的 properties 处于同一级别。示例:

import jsonschema

schema = {
    "type": "object",
    "properties": {
        "src_airport": {
            "type": "object",
            "properties": {
                "airport_id": {"type": "number"}
            }
        },
        "dst_airport": {
            "type": "object",
            "properties": {
                "airport_id": {"type": "number"}
            }
        },
    },
    "required": [
        "src_airport",
        # do not put `dst_airport` here, meaning it is optional
    ]
}

example1 = {
    "src_airport": {"airport_id": 1},
    "dst_airport": {"airport_id": 2}
}
example2 = {
    "src_airport": {"airport_id": 1},
    # "dst_airport": {"airport_id": 2}
}
example3 = {
    # "src_airport": {"airport_id": 1},
    "dst_airport": {"airport_id": 2}
}

try:
    jsonschema.validate(example1, schema=schema)
except jsonschema.exceptions.ValidationError:
    print("example1: fail")
else:
    print("example1: pass")

try:
    jsonschema.validate(example2, schema=schema)
except jsonschema.exceptions.ValidationError:
    print("example2: fail")
else:
    print("example2: pass")

try:
    jsonschema.validate(example3, schema=schema)
except jsonschema.exceptions.ValidationError:
    print("example3: fail")
else:
    print("example3: pass")

我也对 jsonschema 语法感到困惑。