【问题标题】:How do I require one field to be (A and B), or ( A), or (B), or (A and C), or (C)我如何要求一个字段为(A 和 B),或(A),或(B),或(A 和 C),或(C)
【发布时间】:2020-11-03 06:39:01
【问题描述】:

我在想出一个可以适应这些场景的架构时遇到了麻烦:

  • 此字段可以有 A,和/或只有 B 或 C 之一
  • 此字段只能有 A
  • 此字段只能包含 B 之一或 C 之一

A、B 和 C 具有相同的架构。

基本上B和C不能同时存在。

示例场景:

// Valid
{
    "exampleItem": {
        "A": [{
            "key": "item A",
            "description": "test desc"
        }],
        "B": [{
            "key": "item B",
            "description": "test desc"
        }]
    }
}

// Invalid
{
    "exampleItem": {
        "B": [{
            "key": "item B",
            "description": "test desc"
        }],
        "C": [{
            "key": "item C",
            "description": "test desc"
        }]
    }
}

我的尝试:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "exampleItemSchema": {
            "type": "object",
            "oneOf": [
                {
                    "type": "object",
                    "properties": { 
                        "A": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "required": [ "key", "description" ],
                                "properties": {
                                    "key": {
                                        "type": "string"
                                    },
                                    "description": {
                                        "type": "string"
                                    }

                                }
                            }                            
                        },
                        "B": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "required": [ "key", "description" ],
                                "properties": {
                                    "key": {
                                        "type": "string"
                                    },
                                    "description": {
                                        "type": "string"
                                    }

                                }
                            }                            
                        }
                    }
                },
                {
                    "type": "object",
                    "properties": { 
                        "A": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "required": [ "key", "description" ],
                                "properties": {
                                    "key": {
                                        "type": "string"
                                    },
                                    "description": {
                                        "type": "string"
                                    }

                                }
                            }                            
                        },
                        "C": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "required": [ "key", "description" ],
                                "properties": {
                                    "key": {
                                        "type": "string"
                                    },
                                    "description": {
                                        "type": "string"
                                    }

                                }
                            }                            
                        }
                    }
                }
            ]
        }
    }
    
}

我现在正在使用oneOf,但我很确定这不是我想要的。我不能使用anyOf,因为我不希望 B 和 C 同时存在,而allOf 会针对 A、B 和 C 断言。

【问题讨论】:

  • 您知道 JSON Schema 中的 not 关键字吗?你可以做not > required: B,C。抱歉目前在手机上。可以稍后形成答案。

标签: javascript json jsonschema json-schema-validator


【解决方案1】:

要断言属性可以存在,您可以这样做:

{
  "not": {
    "required": [
      "property name"
    ]
  }
}

或:

{
  "properties": {
    "property name": false
  }
}

您可以使用required 关键字断言属性必须存在。

您可以将架构与allOfanyOfoneOf 组合在一起。

请记住,子模式不仅用于影响整体结果,还可以用作要评估的另一个子模式的先决条件,并且那个子模式将构成整体评估结果的一部分。关键字notif/then/else 使用前置条件来决定它们将如何评估。 notif 中的条件仅用于确定真/假结果,在这些子模式中断言的任何其他内容都不会用于最终结果。

这应该是您需要的工具箱中的所有工具。

(为了便于阅读,我会将所有 properties 关键字(定义 A、B 和 C 应该是什么样子,如果它们存在并且被允许的话)保留在顶层,然后使用带有 if/ 的子断言then/else 而不仅仅是说明哪些属性必须存在或不应该存在,以及以何种组合形式存在。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 2020-07-10
    • 2015-11-28
    相关资源
    最近更新 更多