如何实现条件JSON模式

时间:2017-11-08 07:53:23

标签: java json rest web-services validation

我正在使用JSON模式文件,该文件已被用作对Web服务中的JSON输入进行验证的参考,并且我正在使用everit库。下面是我正在使用的代码JSON模式文件。

{
 "oneOf": [
             {
             "type": "object",
                 "properties": {
                     "foo": {
                     "enum": ["firstValue"]
                 },
                 "bar": {
                     "type": "array",
                     "items": {"type": "number"}
                 }                               
             },             
             "required": ["foo", "bar"]
             },
             {
             "type": "object",
                 "properties": {
                     "foo": {                     
                     "enum": ["secondValue"]
                 },
                 "buzz": {
                     "type": "string",
                     "minLength": 10
                 }
             },
             "required": ["foo", "buzz"]
             }    
         ]
}

我需要同时验证1个输入,即" firstValue"或" secondValue"。我不想验证我的输入有" firstValue"反对" secondValue"架构。所以我用了#34; oneOf"但这也没有帮助我,它会检查" secondValue"如果" firstValue"架构输入有任何问题。

所以我尝试在我的Schema文件中使用Switch:

{
    "type": "object",
    "switch": [
        {
            "if": {"properties": {"foo": {"enum": ["firstValue"]}}},
            "then": {
                "properties": {
                    "bar": {
                        "type": "array",
                        "items": {"type": "number"}
                    }
                },
                "required": ["foo", "bar"]
            }
        },
        {
            "if": {"properties": {"foo": {"enum": ["secondValue"]}}},
            "then": {
                "properties": {
                    "buzz": {
                        "type": "string",
                        "minLength": 10
                    },
                    "required": ["foo", "buzz"]
                }
            }
        }
    ]
}

但这似乎根本不起作用。看起来它没有验证我的输入。

参考链接:
对于" oneOf":http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.29
对于"切换":https://github.com/json-schema-org/json-schema-spec/issues/64

0 个答案:

没有答案