验证架构中的嵌套JSON属性

时间:2019-10-14 11:06:33

标签: json jsonschema json-schema-validator

这是我的JSON模式和JSON,如下面底部所示,并使用ajv验证程序支持json规范草稿7。

默认情况下,“科学”对象必须表示为:

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

必须存在“规则”和“ sciencePattern”的位置。

但是,如果“ sciencePattern”即将包含其他属性(根据架构),则应执行以下验证:

  1. 如果存在默认的科学对象,则应要求“艺术”对象中的“得分”属性。
  2. 如果规则中存在NESTED“得分”数组属性,则为:
{ "type": "science", "rule":{"sciencePattern":{"marks":{"scored":[10]}}} }

然后就不需要“艺术”对象中的“得分”属性。换句话说,如果有人在“艺术”对象中指定“得分”属性,则模式验证应该抱怨,因为“科学”对象中存在“得分”属性。

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

1 个答案:

答案 0 :(得分:0)

{
  "$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"
        }
      }
    }
  }
}