Javascript 我如何要求一个字段是(A和B),或(A),或(B),或(A和C),或(C)

Javascript 我如何要求一个字段是(A和B),或(A),或(B),或(A和C),或(C),javascript,json,jsonschema,json-schema-validator,Javascript,Json,Jsonschema,Json Schema Validator,我很难想出一个能够适应这些场景的模式: 此字段可以有A,和/或只有B或C中的一个 此字段只能有A 此字段只能有一个B或一个C A、 B和C具有相同的模式 基本上B和C不能同时存在 示例场景: // Valid { "exampleItem": { "A": [{ "key": "item A", "description"

我很难想出一个能够适应这些场景的模式:

  • 此字段可以有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"
                                    }

                                }
                            }                            
                        }
                    }
                }
            ]
        }
    }
    
}
我所尝试的:

// 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"
                                    }

                                }
                            }                            
                        }
                    }
                }
            ]
        }
    }
    
}

我现在使用的是中的一个,但我很确定这不是我想要的。我不能使用
anyOf
,因为我不希望B和C同时存在,并且
allOf
将针对A、B和C进行断言。

要断言属性不存在,可以执行以下操作:

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

您可以断言属性必须与
required
关键字一起存在

您可以将模式与
allOf
anyOf
oneOf
组合在一起

请记住,子模式不仅用于影响总体结果,还可作为另一个子模式进行评估的先决条件,并且子模式将构成总体评估结果的一部分。关键字
not
,以及
如果
/
/
否则
使用先决条件来决定如何进行评估。
not
if
中的条件仅用于确定真/假结果,这些子模式中断言的任何其他条件都不会用于最终结果

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


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

您知道JSON模式中的
not
关键字吗?您可以执行
而不是
所需操作:B、C
。抱歉,目前正在手机上。我可以稍后再作答复。