【问题标题】:Python - Validating Schemas, trying to require a fieldPython - 验证模式,试图要求一个字段
【发布时间】:2022-01-13 19:35:34
【问题描述】:

大家!我感谢您的帮助。这是我第一次创建架构,它涉及航班数据,但我遇到了多个错误,因为在某些记录中,它不涉及特定字段(在本例中为 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 部分,但我认为它实际上并没有起到任何作用。

有人可以帮忙吗?谢谢!

【问题讨论】:

    标签: python json validation schema


    【解决方案1】:

    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 语法感到困惑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 2014-06-17
      • 1970-01-01
      相关资源
      最近更新 更多