首页 文章

JSON模式anyOf基于其中一个属性进行验证

提问于
浏览
5

我很难弄清楚如何根据其中一个属性的值来验证对象数组 . 所以我有一个JSON对象,如:

{
    "items": [
        {
            "name": "foo",
            "otherProperty": "bar"
        },
        {
            "name": "foo2",
            "otherProperty2": "baz",
            "otherProperty3": "baz2"
        },
        {
            "name": "imInvalid"
        }
    ]
}

我想说

  • items可以包含anyOf对象,其中name可以是"foo"或"foo2"

  • 如果是"foo"那么唯一有效的其他 property (必填)是"otherProperty"

  • 如果名称是"foo2"那么唯一有效的其他属性是"otherProperty2"和"otherProperty3"都需要

  • "name"没有"foo"和"foo2"的其他值有效

  • 对象本身在items数组中是可选的,有些可能会重复 .

我尝试了各种各样的东西,但是当我验证时,我似乎无法失败 . 例如,名称“imInvalid”应该导致验证错误 . 这是我最新的架构迭代 . 我错过了什么?

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "required": ["items"],
    "properties": {
        "items": {
            "type": "array",
            "minItems": 1,
            "additionalProperties": false,
            "properties": {
                "name": {
                    "anyOf": [
                        {
                            "type": "object",
                            "required": ["name", "otherProperty"],
                            "additionalProperties": false,
                            "properties": {
                                "otherProperty": { "type": "string" },
                                "name": { "enum": [ "foo" ] }
                            }
                        },{
                            "type": "object",
                            "required": ["name", "otherProperty2", "otherProperty3" ],
                            "additionalProperties": false,
                            "properties": {
                                "otherProperty2": { "type": "string" },
                                "otherProperty3": { "type": "string" },
                                "name": { "enum": [ "foo2" ] }
                            }
                        }
                    ]
                }
            }
        }
    }
}

1 回答

  • 6

    你已经有了使用枚举来分离匹配内容的基本思想,但这里有一些错误:

    • Json模式数组没有属性,它们有项目 .

    • 在该属性中,您将名称定义为一个属性,然后保存其他json架构对象 .

    • 在您的模式的第二个分支中,您定义了 otherProperty3 但在您的示例中该属性被称为 anotherProperty3

    试试这个略微修改的版本:

    {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "required": ["items"],
    "properties": {
        "items": {
            "type": "array",
            "minItems": 1,
            "additionalProperties": false,
            "items": {
                "anyOf": [
                    {
                        "type": "object",
                        "required": ["name", "otherProperty"],
                        "additionalProperties": false,
                        "properties": {
                            "otherProperty": { "type": "string" },
                            "name": { "enum": [ "foo" ] }
                        }
                    },{
                        "type": "object",
                        "required": ["name", "otherProperty2", "anotherProperty3" ],
                        "additionalProperties": false,
                        "properties": {
                            "otherProperty2": { "type": "string" },
                            "anotherProperty3": { "type": "string" },
                            "name": { "enum": [ "foo2" ] }
                        }
                    }
                ]
            }
        }
    }
    }
    

相关问题