I am making a JSON Schema using https://www.jsonschemavalidator.net/ and https://jsonschema.dev/ to validate them and both are giving me the same problem and idk what is wrong
Here is the JSON that I am using
{
"Cdtr": {
"Id": {
"PrvtId": {
"Othr": {
"Id": "123456"
"SchmeNm": {
"Cd": "NIDN"
}
}
}
}
}
}
and here is the JSON Schema
{
"allOf": [
{
"if": {
"properties": {
"Cdtr": {
"properties": {
"Id": {
"properties": {
"PrvtId": {
"properties": {
"Othr": {
"properties": {
"SchmeNm": {
"Cd": {
"enum": [
"NIDN",
"CCPT",
"ANRU"
]
}
}
}
}
}
}
}
}
}
}
}
},
"then": {
"properties": {
"Cdtr": {
"properties": {
"Id": {
"properties": {
"PrvtId": {
"properties": {
"Othr": {
"properties": {
"Id": {
"maxLength": 8,
"minLength": 6,
"type": "string"
}
}
}
}
}
}
}
}
}
}
}
},
{
"if": {
"properties": {
"Cdtr": {
"properties": {
"Id": {
"properties": {
"PrvtId": {
"properties": {
"Othr": {
"properties": {
"SchmeNm": {
"Cd": {
"const": "CUST"
}
}
}
}
}
}
}
}
}
}
}
},
"then": {
"properties": {
"Cdtr": {
"properties": {
"Id": {
"properties": {
"PrvtId": {
"properties": {
"Othr": {
"properties": {
"Id": {
"minLength": 6,
"type": "string"
}
}
}
}
}
}
}
}
}
}
}
},
{
"if": {
"properties": {
"Cdtr": {
"properties": {
"Id": {
"properties": {
"PrvtId": {
"properties": {
"Othr": {
"properties": {
"SchmeNm": {
"Cd": {
"const": "TXID"
}
}
}
}
}
}
}
}
}
}
}
},
"then": {
"properties": {
"Cdtr": {
"properties": {
"Id": {
"properties": {
"PrvtId": {
"properties": {
"Othr": {
"properties": {
"Id": {
"oneOf": [
{
"maxLength": 8,
"minLength": 8
},
{
"maxLength": 11,
"minLength": 11
}
],
"type": "string"
}
}
}
}
}
}
}
}
}
}
}
}
],
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "PERU C2C BANK",
"properties": {
"Cdtr": {
"properties": {
"Id": {
"properties": {
"PrvtId": {
"properties": {
"Othr": {
"properties": {
"Id": {
"pattern": "^[0-9]+$",
"type": "string"
},
"SchmeNm": {
"properties": {
"Cd": {
"enum": [
"NIDN",
"CCPT",
"TXID",
"ANRU",
"CUST"
],
"maxLength": 4,
"minLength": 4,
"type": "string"
}
},
"required": [
"Cd"
]
}
},
"required": [
"Id",
"SchmeNm"
]
}
},
"required": [
"Othr"
]
}
},
"required": [
"PrvtId"
]
}
},
"required": [
"Id"
]
}
},
"required": [
"Cdtr"
]
}
This case should be "valid" in case of the first if
schema but I get an error for the last validation
If you're interested in cleaning up the readability a bit, you can use definitions
to define your complex schemas and then use a oneOf
to reference each of them.
You can read more about different patterns used in JSON Schema authoring here
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://www.stackoverflow.com/schema/cdtr",
"description": "PERU C2C BANK",
"required": [
"Cdtr"
],
"properties": {
"Cdtr": {
"required": [
"Id"
],
"properties": {
"Id": {
"required": [
"PrvtId"
],
"properties": {
"PrvtId": {
"required": [
"Othr"
],
"properties": {
"Othr": {
"required": [
"Id",
"SchmeNm"
],
"properties": {
"Id": {
"pattern": "^[0-9]+$",
"type": "string"
},
"SchmeNm": {
"type": "object",
"required": ["Cd"],
"properties": {
"Cd": {
"type": "string"
}
}
}
},
"oneOf": [
{
"$ref": "#/definitions/SchmeNm_TXID"
},
{
"$ref": "#/definitions/SchmeNm_CUST"
},
{
"$ref": "#/definitions/SchmeNm_OTHER"
}
]
}
}
}
}
}
}
}
},
"definitions": {
"SchmeNm_TXID": {
"type": "object",
"properties": {
"Id": {
"oneOf": [
{
"minLength": 8,
"maxLength": 8
},
{
"minLength": 11,
"maxLength": 11
}
]
},
"SchmeNm": {
"properties": {
"Cd": {
"enum": [
"TXID"
]
}
}
}
}
},
"SchmeNm_CUST": {
"type": "object",
"properties": {
"Id": {
"minLength": 6
},
"SchmeNm": {
"properties": {
"Cd": {
"enum": [
"CUST"
]
}
}
}
}
},
"SchmeNm_OTHER": {
"type": "object",
"properties": {
"Id": {
"minLength": 6,
"maxLength": 8
},
"SchmeNm": {
"properties": {
"Cd": {
"enum": [
"NIDN",
"CCPT",
"ANRU"
]
}
}
}
}
}
}
}