验证架构中的嵌套JSON属性

问题描述 投票:0回答:1

这是我的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
    }
  ]
}
json jsonschema json-schema-validator
1个回答
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"
        }
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.