skip to Main Content

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
Error example

Edit:
Schema version:
"$schema": "http://json-schema.org/draft-07/schema#",

2

Answers


  1. You missed a few properties definitions in your if statements where you defined SchmeNm and Cd

    {
                "if": {
                    "properties": {
                        "Cdtr": {
                            "properties": {
                                "Id": {
                                    "properties": {
                                        "PrvtId": {
                                            "properties": {
                                                "Othr": {
                                                    "properties": {
                                                        "SchmeNm": {
                                                            "properties": {  <<--- this was missing on all of your conditional statements. 
                                                                "Cd": {
                                                                    "const": "TXID"
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
    

    After fixing that, your instance passes

    {
      keyword: 'https://json-schema.org/evaluation/validate',
      absoluteKeywordLocation: 'https://www.stackoverflow.com/schema/cdtr#',
      instanceLocation: '#',
      valid: true,
      errors: []
    }
    
    Login or Signup to reply.
  2. 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"
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search