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.
Looks like you're only missing a few things in your conditional statements.
requiredkeyword in yourthenconditional statementsminItemskeyword to require at least one array element is presentadditionalPropertieskeyword to thespecificValue1schemaIf you don't have the
requiredinthen, the schema will accept any property or no properties as valid. I addedminItems: 1to theInvestigation Request DataandInvestigation Request Confschemas, otherwise your array will evaluatetruewith an empty array.The last thing is adding
additionalProperties: falseto theif, thenforspecificValue1schema. 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
trueand will never evaluate theif, thenstatement because you don'trequireInvestigation Request Datain thethenstatement.Updated schema
passing instances