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

Fo.*_*Fo. 7 validation json jsonschema

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

{
    "items": [
        {
            "name": "foo",
            "otherProperty": "bar"
        },
        {
            "name": "foo2",
            "otherProperty2": "baz",
            "otherProperty3": "baz2"
        },
        {
            "name": "imInvalid"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想说

  1. items可以包含anyOf对象,其中name可以是"foo"或"foo2"
  2. 如果它是"foo"那么唯一有效的其他属性(必需)是"otherProperty"
  3. 如果名称是"foo2"那么唯一有效的其他属性是"otherProperty2"和"otherProperty3"都需要
  4. "name"除"foo"和"foo2"之外没有其他值有效
  5. 对象本身在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" ] }
                            }
                        }
                    ]
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 11

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

  • 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" ] }
                    }
                }
            ]
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

  • 是否有可能在两个“anyOf”模式中不重复“name”属性? (6认同)