Hello guys I am making a JSON schema I am using https://www.jsonschemavalidator.net/ and https://jsonschema.dev/ to validate them and both mark 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"
}
}
}
}
}
}
}
}
}
}
}
}
],
"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 base of the first IF but I get an error for the last validation
Edit:
Schema version:
"$schema": "http://json-schema.org/draft-07/schema#",
2
Answers
You missed a few
properties
definitions in yourif
statements where you definedSchmeNm
andCd
After fixing that, your instance passes
If you’re interested in cleaning up the readability a bit, you can use
definitions
to define your complex schemas and then use aoneOf
to reference each of them.You can read more about different patterns used in JSON Schema authoring here