Json模式将错误实例验证为true

时间:2019-07-30 21:48:13

标签: json jsonschema

我具有以下架构,但是我很难理解它为什么不起作用。

{
  "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://yourdomain.com/schemas/myschema.json",
    "title": "Product",
    "description": "A product in the catalog",
    "type": "object",
    "properties": {
        "transitions" : {
            "description": "defines the transitions to another state",
            "type": "object",
            "patternProperties": { 
                "^[0-9]+$": {
                    "description": "defines a transition for an specific node",
                    "type":"object",
                    "properties": {
                        "next": {
                            "description": "id of the next transition",
                            "oneOf": [
                                {
                                    "type":"string"
                                }, 
                                {
                                    "type": "object",
                                    "patternProperties": {
                                        "^[\\<\\>\\=\\!\\=\\>\\<]\\_[0-9]+$" : {
                                            "type":"string"
                                        }
                                    }

                                },
                                {
                                    "type": "object",
                                    "patternProperties": {
                                        "^\\w+( +\\w+)*$" : {
                                            "type":"string"
                                        }
                                    }
                                }
                            ]
                        },
                        "action": {
                            "description": "id of the transitions action",
                            "type":"string"
                        }
                    }                    
                }
            },
            "additionalProperties": false
        }
    },
    "required": ["transitions"]
}

例如,如您在模式中所见,此特定的json实例应符合next属性定义之一

"transitions": {
    "1": {
      "action": "1_input",
      "next": {
        "test de una novedad": "2"
      }
    }
  }
}

财产图

{
    "type": "object",
    "patternProperties": {
        "^\\w+( +\\w+)*$" : {
            "type":"string"
        }
    }
}

但是我在在线验证器https://www.jsonschemavalidator.net/中收到了这些错误消息,

Message:
JSON is valid against more than one schema from 'oneOf'. Valid schema indexes: 1, 2.
Schema path:
http://yourdomain.com/schemas/myschema.json#/properties/transitions/patternProperties/^[0-9]+$/properties/next/oneOf
Message:
Invalid type. Expected String but got Object.
Schema path:
http://yourdomain.com/schemas/myschema.json#/properties/transitions/patternProperties/^[0-9]+$/properties/next/oneOf/0/type

我不知道如何将其与两个定义匹配。

有人有类似的情况吗?

预先感谢

1 个答案:

答案 0 :(得分:2)

您的问题在于oneOf

的定义
"oneOf": [
    {
        "type":"string"
    }, 
    {
        "type": "object",
        "patternProperties": {
            "^[\\<\\>\\=\\!\\=\\>\\<]\\_[0-9]+$" : {
                "type":"string"
            }
        }
    },
    {
        "type": "object",
        "patternProperties": {        
            "^\\w+( +\\w+)*$" : {
                "type":"string"
            }
        }
    }
]

对于"test de una novedad": "2",第二个和第三个元素都是不受属性限制的对象,因此它们都匹配。

要解决此问题,如果您不介意让一个对象同时使用这两个属性进行验证,可以尝试将两者合并:

"oneOf": [
    {
        "type":"string"
    }, 
    {
        "type": "object",
        "patternProperties": {
            "^[\\<\\>\\=\\!\\=\\>\\<]\\_[0-9]+$" : {
                "type":"string"
            },
            "^\\w+( +\\w+)*$" : {
                "type":"string"
            }
        }
    }
]

或者您可以在一个或两个中同时添加additionalProperties: false

"oneOf": [
    {
        "type":"string"
    }, 
    {
        "type": "object",
        "additionalProperties": false,
        "patternProperties": {
            "^[\\<\\>\\=\\!\\=\\>\\<]\\_[0-9]+$" : {
                "type":"string"
            }
        }
    },
    {
        "type": "object",
        "additionalProperties": false,
        "patternProperties": {        
            "^\\w+( +\\w+)*$" : {
                "type":"string"
            }
        }
    }
]