jsonjsonschemajson-schema-validator

JSON SCHEMA Nested Array Fields Validation based on Root level Field


Following is my JSON Schema , My requirement is if MID = specificValue1 then "Investigation Request Conf Reason code" this field should be mandatory I have also added my reference Input JSON where I haven't added "Investigation Request Conf Reason code" field so I expected it should throw the error.

**JSON SCHEMA : **


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

*INPUT JSON : *

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

I Have tried with given JSON SCHEMA AND INPUT but not working let me know If I have did any mistake.


Solution

  • Looks like you're only missing a few things in your conditional statements.

    If you don't have the required in then, the schema will accept any property or no properties as valid. I added minItems: 1 to the Investigation Request Data and Investigation Request Conf schemas, otherwise your array will evaluate true with an empty array.

    The last thing is adding additionalProperties: false to the if, then for specificValue1 schema. It seems like you want to constrain additional properties in that schema, if that's not true, feel free to remove that constraint.

    An example of your schema not having enough constraints is this instance evaluates to true and will never evaluate the if, then statement because you don't require Investigation Request Data in the then statement.

    {
        "MID": "specificValue",
        "Instr ID X": "aaa",
        "test": "sd"
    }
    

    Updated schema

    {
        "$schema": "http://json-schema.org/draft-07/schema",
        "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"
                ],
                "minItems": 1,
                "items": {
                    "$ref": "#/definitions/InvReqData"
                }
            }
        },
        "allOf": [
            {
                "if": {
                    "properties": {
                        "MID": {
                            "const": "specificValue"
                        }
                    }
                },
                "then": {
                    "required": [
                        "Investigation Request Data"
                    ],
                    "properties": {
                        "Investigation Request Data": {
                            "items": {
                                "required": [
                                    "Investigation Request Conf Reason code",
                                    "Compensation Reason"
                                ],
                                "not": {
                                    "anyOf": [
                                        {
                                            "required": [
                                                "Investigation Request Reason Type"
                                            ]
                                        },
                                        {
                                            "required": [
                                                "Investigation Request Reason Description"
                                            ]
                                        }
                                    ]
                                }
                            }
                        }
                    }
                }
            },
            {
                "if": {
                    "properties": {
                        "MID": {
                            "const": "specificValue1"
                        }
                    }
                },
                "then": {
                    "required": [
                        "Investigation Request Data"
                    ],
                    "properties": {
                        "Investigation Request Data": {
                            "items": {
                                "required": [
                                    "Investigation Request Conf"
                                ],
                                "additionalProperties": false,
                                "properties": {
                                    "Investigation Request Conf": {
                                        "minItems": 1,
                                        "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
                    }
                }
            }
        }
    }
    
    

    passing instances

    {
        "MID": "specificValue",
        "Instr ID X": "aaa",
        "test": "sd",
        "Investigation Request Data": [
            {
                "Investigation Request Conf Reason code": "1",
                "Compensation Reason": "2"
            }
        ]
    }
    
    {
        "MID": "specificValue1",
        "Instr ID X": "aaa",
        "test": "sd",
        "Investigation Request Data": [
            {
                "Investigation Request Conf": [
                    {
                        "Investigation Request Conf Reason code": "1"
                    }
                ]
            }
        ]
    }