繁体   English   中英

JsonSchema Validation 不会为不同的属性 NAME 抛出错误

[英]JsonSchema Validation not throwing error for different property NAME

这是我的 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,我想确保它失败。

想通了。 必须使用 oneOf

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM