【问题标题】:JsonSchema Validation not throwing error for different property NAMEJsonSchema Validation 不会为不同的属性 NAME 抛出错误
【发布时间】:2021-03-29 22:02:11
【问题描述】:

这是我的 json 架构

{
"title":"Sl",
"description":"A user request json",
"type":"object",
"properties":{
    "FRETL":{
    "id": "#FRETL",
    "type": "array",
    "items":{
        "type": "object",
        "properties":{
            "instances":{
            "description":"ThE Instance(Comma Separated Allowed)",
            "type":"string",
            "minLength": 1
            },
            "ID":{
            "description":"The ID of the Instance",
            "type":"string",
            "minLength": 1
            },
            "Number":{
                "description":"Mention The Number associated",
                "type":"string"
                },
            "EndDate":{
                "type":"string",
                "format": "date",
                "pattern": "(((19|20)([2468][048]|[13579][26]|0[48])|2000)[/-]02[/-]29|((19|20)[0-9]{2}[/-](0[469]|11)[/-](0[1-9]|[12][0-9]|30)|(19|20)[0-9]{2}[/-](0[13578]|1[02])[/-](0[1-9]|[12][0-9]|3[01])|(19|20)[0-9]{2}[/-]02[/-](0[1-9]|1[0-9]|2[0-8])))",
                "description": "We expect yyyy-MM-dd"
                },
            "StartDate":{
                "type":"string",
                "format": "date",
                "pattern": "(((19|20)([2468][048]|[13579][26]|0[48])|2000)[/-]02[/-]29|((19|20)[0-9]{2}[/-](0[469]|11)[/-](0[1-9]|[12][0-9]|30)|(19|20)[0-9]{2}[/-](0[13578]|1[02])[/-](0[1-9]|[12][0-9]|3[01])|(19|20)[0-9]{2}[/-]02[/-](0[1-9]|1[0-9]|2[0-8])))",
                "description": "We expect yyyy-MM-dd"
                },
            "Comments":{
                "type":"string",
                "description":"Justification"
            }
        },
        "if":{
            "properties":{
                "instances":{
                    "enum": [
                        "*"
                    ]
                }
            }
        },
        "then":{
            "properties":{
                "ID":{
                    "description": "please enter the AssetID",
                    "minLength": 1
                }
            }
        },
        "required":[
            "instances",
            "ID"
        ]
        },
    "ABTRIC":{
    "id": "#ABTRIC",
    "type": "array",
    "items":{
        "type": "object",
        "properties":{
            "instances":{
            "description":"ThE Instance(Comma Separated Allowed)",
            "type":"string",
            "minLength": 1
            },
            "ID":{
            "description":"The ID of the Instance",
            "type":"string",
            "minLength": 1
            },
            "Number":{
                "description":"Mention The Number associated",
                "type":"string"
                },
            "EndDate":{
                "type":"string",
                "format": "date",
                "pattern": "(((19|20)([2468][048]|[13579][26]|0[48])|2000)[/-]02[/-]29|((19|20)[0-9]{2}[/-](0[469]|11)[/-](0[1-9]|[12][0-9]|30)|(19|20)[0-9]{2}[/-](0[13578]|1[02])[/-](0[1-9]|[12][0-9]|3[01])|(19|20)[0-9]{2}[/-]02[/-](0[1-9]|1[0-9]|2[0-8])))",
                "description": "We expect yyyy-MM-dd"
                },
            "StartDate":{
                "type":"string",
                "format": "date",
                "pattern": "(((19|20)([2468][048]|[13579][26]|0[48])|2000)[/-]02[/-]29|((19|20)[0-9]{2}[/-](0[469]|11)[/-](0[1-9]|[12][0-9]|30)|(19|20)[0-9]{2}[/-](0[13578]|1[02])[/-](0[1-9]|[12][0-9]|3[01])|(19|20)[0-9]{2}[/-]02[/-](0[1-9]|1[0-9]|2[0-8])))",
                "description": "We expect yyyy-MM-dd"
                },
            "Comments":{
                "type":"string",
                "description":"Justification"
            }
        },
        "if":{
            "properties":{
                "instances":{
                    "enum": [
                        "*"
                    ]
                }
            }
        },
        "then":{
            "properties":{
                "ID":{
                    "description": "please enter the AssetID",
                    "minLength": 1
                }
            }
        },
        "required":[
            "instances",
            "ID"
        ]
        }
    }

这是我提供的 json。

 {
"FRETL":
[
    {
        "instances": "i-123",
        "AssetID": "1231",
        "Number": "12312",
        "StartDate": "2021/12/12", 
        "EndDate": "2021/12/12", 
        "Justification": "Testing Example"
    }
]

}

如果我处理此问题并运行 python 验证器,它将正常工作并按预期工作,并且当我将 FRETL 的名称更改为 ABTRIC 时,JSON 表示它仍然有效并且它根据我在中提到的任何规则运行架构。但是,如果我将 JSON 输入中的属性名称更改为上述 2 之外的其他名称(FRETL 和 ABTRIC),它仍会将其标记为有效。如果 JSON 输入不包含 ABTRIC 或 FRETL,我想确保它失败。

【问题讨论】:

    标签: python json validation jsonschema json-schema-validator


    【解决方案1】:

    想通了。必须使用 oneOf

    "oneOf": [
        { "required": ["FRETL"] },
        { "required": ["ABTRIC"] },
        { "required": ["ABCD"] }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多