基于根级字段的JSON SCHEMA嵌套数组字段验证

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

以下是我的 JSON 架构,我的要求是如果 MID = SpecificValue1 则“调查请求会议原因代码”此字段应该是必填的我还添加了我的参考输入 JSON,其中我没有添加“调查请求会议原因代码”字段,因此我预计它会抛出错误。

**JSON 架构:**


{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "MID": {
      "type": "string",
      "description": "Unique identifier for payment message",
      "readOnly": false
    },
    "Instr ID X": {
      "pattern": "^([a-zA-Z0-9 /\\-?:\\()\\.,']{1,16})$",
      "type": "string",
      "readOnly": false
    },
    "Instd Ccy": {
      "maxLength": 3,
      "type": "string",
      "default": "USD",
      "readOnly": false
    },
    "test": {
      "type": "string",
      "readOnly": false
    },
    "test1": {
      "type": "string",
      "readOnly": false
    },
    "Investigation Request Data": {
      "type": [
        "array",
        "null"
      ],
      "items": {
        "$ref": "#/definitions/InvReqData"
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "MID": {
            "const": "specificValue"
          }
        }
      },
      "then": {
        "properties": {
          "Investigation Request Data": {
            "items": {
              "required": [
                "Investigation Request Reason code",
                "Compensation Reason"
              ],
              "not": {
                "anyOf": [
                  {
                    "required": [
                      "Investigation Request Reason Type"
                    ]
                  },
                  {
                    "required": [
                      "Investigation Request Reason Description"
                    ]
                  }
                ]
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "MID": {
            "const": "specificValue1"
          }
        }
      },
      "then": {
        "properties": {
          "Investigation Request Data": {
            "items": {
              "properties": {
                "Investigation Request Conf": {
                  "items": {
                    "required": [
                      "Investigation Request Conf Reason code"
                    ]
                  }
                }
              }
            }
          }
        }
      }
    }
  ],
  "definitions": {
    "InvReqData": {
      "type": "object",
      "properties": {
        "Investigation Request Reason code": {
          "maxLength": 4,
          "minLength": 1,
          "type": [
            "string"
          ],
          "readOnly": true
        },
        "Compensation Reason": {
          "maxLength": 140,
          "minLength": 1,
          "type": [
            "string"
          ],
          "readOnly": true
        },
        "Investigation Request Reason Type": {
          "maxLength": 4,
          "minLength": 1,
          "type": [
            "string"
          ],
          "readOnly": true
        },
        "Investigation Request Reason Description": {
          "maxLength": 4,
          "minLength": 1,
          "type": [
            "string"
          ],
          "readOnly": true
        },
        "Investigation Request Code": {
          "type": [
            "string"
          ],
          "readOnly": true
        },
        "Investigation Request Conf": {
          "type": [
            "array",
            "null"
          ],
          "items": {
            "$ref": "#/definitions/InvReqConf"
          }
        }
      }
    },
    "InvReqConf": {
      "type": "object",
      "properties": {
        "Investigation Request Conf Reason code": {
          "maxLength": 4,
          "minLength": 1,
          "type": [
            "string"
          ],
          "readOnly": true
        }
      }
    }
  }
}

*输入 JSON:*

{
  "MID": "specificValue1",
  "Instr ID X": "aaa",
  "test": "sd",
  "Investigation Request Data": [
    {
      "Investigation Request Reason Type": "as"
    },
    {
      "Investigation Request Reason Type": "as"
    }
  ]
}

我已尝试使用给定的 JSON 架构和输入,但不起作用,如果我犯了任何错误,请告诉我。

json jsonschema json-schema-validator
1个回答
0
投票

看起来您有调查请求数据:类型为 Array,然后 InvRegData 作为对象。 “定义”:{ “InvReqData”:{ “类型”:“对象”, “特性”: { “调查请求原因代码”:{ “最大长度”:4, 尝试:

“InvReqData”:{ “类型”:“数组”,

© www.soinside.com 2019 - 2024. All rights reserved.