验证架构中的嵌套 JSON 属性

Validating nested JSON attribute in schema

这是我的 JSON 架构和 JSON 如下图底部所示,并使用 ajv 验证器支持 json 规范草案 7。

默认情况下,'science' 对象必须表示为:

//Default science object
{"type": "science", "rule": {"sciencePattern": {}}}

'rule' 和 'sciencePattern' 必须在那里。

但是,如果 'sciencePattern' 将包含其他属性(根据架构),则应启动以下验证:

  1. 如果存在默认科学对象,则 "arts" 对象中的 "scored" 属性应该是必需的。
  2. 如果 NESTED "scored" 数组属性存在于规则中:
{ "type": "science", "rule":{"sciencePattern":{"marks":{"scored":[10]}}} }

那么 "arts" 对象中的 "scored" 属性不应该是必需的。换句话说,如果有人在 "arts" 对象中指定 "scored" 属性,模式验证应该会报错,因为 "science" 对象中有一个 "scored" 属性可用。

//JSON 架构

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": [
    "exam"
  ],
  "properties": {
    "exam": {
      "type": "array",
      "minItems": 1,
      "items": {
        "anyOf": [
          {
            "$ref": "#/definitions/science"
          },
          {
            "$ref": "#/definitions/arts"
          }
        ]
      }
    },
    "if": {
      "type": "object",
      "required": [
        "type",
        "rule"
      ],
      "properties": {
        "type": {
          "const": "science"
        },
        "rule": {
          "type": "object",
          "required": [
            "sciencePattern"
          ],
          "properties": {
            "sciencePattern": {
              "$ref": "#/definitions/sciencePattern"
            }
          }
        }
      }
    },
    "then": {
      "type": "object",
      "required": [
        "type"
      ],
      "properties": {
        "type": {
          "const": "arts"
        },
        "not": {
          "required": [
            "scored"
          ]
        }
      }
    }
  },
  "definitions": {
    "sciencePattern": {
      "type": "object",
      "required": [
        "marks"
      ],
      "properties": {
        "marks": {
          "type": "object",
          "required": [
            "scored"
          ],
          "properties": {
            "scored": {
              "type": "array"
            }
          }
        }
      }
    },
    "science": {
      "type": "object",
      "properties": {
        "type": {
          "const": "science"
        },
        "rule": {
          "required": [
            "sciencePattern"
          ],
          "properties": {
            "sciencePattern": {
              "$ref": "#/definitions/sciencePattern"
            }
          }
        }
      }
    },
    "arts": {
      "required": [
        "scored"
      ],
      "properties": {
        "type": {
          "const": "arts"
        },
        "scored": {
          "type": "number"
        },
        "remarks": {
          "type": "string"
        }
      }
    }
  }
}

和我的JSON

{
  "exam": [
    {
      "type": "science",
      "rule": {
        "sciencePattern": {
          "marks": {
            "scored": [10]
          }
        }
      }
    },
    {
      "type": "arts",
      "scored": 10 //This should complain as 'scored' is available above in science
    }
  ]
}
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": [
    "exam"
  ],
  "properties": {
    "exam": {
      "type": "array",     

      "allOf":[      
      {
        "if": {
          "contains": {
            "type": "object",
            "required": [
              "type"
            ],
            "properties": {
              "type": {
                "const": "science"
              }            
            }
          }
        },
        "then":{
          "required":["rule", "sciencePattern"]
        }
      },
      {      
        "if": {
          "contains": {
            "type": "object",
            "required":["type","rule", "sciencePattern"],
            "properties": {
              "type": {
                "const": "science"
              }            
            }
          }
        },
        "then":{
          "required":["scored"]
        }
      },
      { 

        "if": {
          "contains": {
            "type": "object",
            "required": [
              "type",
              "rule"
            ],
            "properties": {
              "type": {
                "const": "science"
              },
              "rule": {
                "type": "object",
                "required": [
                  "sciencePattern"
                ],
                "properties": {
                  "sciencePattern": {
                    "$ref": "#/definitions/emptySciencePattern"
                  }
                }
              }
            }
          }
        },
        "then": {
          "contains": {
            "type": "object",
            "required": ["type", "scored"],
            "properties": {
              "type": {
                "const": "arts"
              }
            }
          }
        },
        "else": {
          "if": {
            "contains": {
              "type": "object",
              "required": [
                "type",
                "rule"
              ],
              "properties": {
                "type": {
                  "const": "science"
                },
                "rule": {
                  "type": "object",
                  "required": [
                    "sciencePattern"
                  ],
                  "properties": {
                    "sciencePattern": {
                      "$ref": "#/definitions/sciencePattern"
                    }
                  }
                }
              }
            }
          },
          "then": {
            "contains": {
              "required": [
                "type"
              ],
              "properties": {
                "type": {
                  "const": "arts"
                }
              },
              "not": {
                "required": ["scored"]
              }
            }
          }
        }
      }],
      "minItems": 1,
      "items": {
        "anyOf": [{
          "$ref": "#/definitions/science"
        }, {
          "$ref": "#/definitions/arts"
        }]
      }
    }
  },
  "definitions": {
    "rule": {
        "type": "object",
        "required": ["sciencePattern"],
        "properties": {
            "sciencePattern": {
                "$ref": "#/definitions/sciencePattern"
            }
        }
    },
    "emptySciencePattern": {
      "type": "object",
      "maxProperties": 0,
      "additionalProperties": false,
      "properties": {}
    },
    "sciencePattern": {
      "type": "object",
      "properties": {
        "marks": {
          "type": "object",
          "properties": {
            "scored": {
              "type": "array"
            }
          }
        }
      }
    },
    "science": {
      "type": "object",
      "required": ["rule"],
      "properties": {
        "type": {
          "const": "science"
        },
        "rule": {
          "$ref":"#/definitions/rule"
        }
      } 
    },
    "arts": {
      "properties": {
        "type": {
          "const": "arts"
        },
        "scored": {
          "type": "number"
        },
        "remarks": {
          "type": "string"
        }
      }
    }
  }
}