Json 模式将错误的实例验证为真

Json schema validating wrong instance as true

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

{
  "$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

我不明白它怎么能与其中两个定义相匹配。

有没有人遇到过类似的情况?

提前致谢

您的问题出在 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"
            }
        }
    }
]