You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSON Schema中allOf嵌套if-then条件验证异常求助

JSON Schema验证异常排查

问题描述

使用JSON Schema验证工具时,测试JSON数据本应匹配第一个if-then规则,但却触发了最后一个if-then规则的错误提示。

测试JSON数据(修正语法错误后)

{
  "Cdtr": {
    "Id": {
      "PrvtId": {
        "Othr": {
          "Id": "123456",
          "SchmeNm": {
            "Cd": "NIDN"
          }
        }
      }
    }
  }
}

原JSON Schema问题分析

原Schema中的if条件仅通过嵌套properties定义了属性约束,但未在每个层级添加"type": "object"和"required"关键字,导致if条件的匹配逻辑不严谨:

  • 未强制要求Cdtr、Id、PrvtId、Othr、SchmeNm、Cd这些路径必须存在
  • 仅定义了属性值的约束,但未明确要求属性必须存在且符合值,导致if条件可能被误判为满足

例如,最后一个if条件的schema未严格约束SchmeNm.Cd必须等于TXID,当实例中Cd为NIDN时,工具可能误判if条件满足,进而应用then中的约束(要求Id长度为8或11位),导致错误。

修正后的JSON Schema

为每个if条件的嵌套层级添加"type": "object"和"required",确保只有当完整路径存在且Cd值匹配时,才会触发对应的then规则:

{
  "allOf": [
    {
      "if": {
        "type": "object",
        "required": ["Cdtr"],
        "properties": {
          "Cdtr": {
            "type": "object",
            "required": ["Id"],
            "properties": {
              "Id": {
                "type": "object",
                "required": ["PrvtId"],
                "properties": {
                  "PrvtId": {
                    "type": "object",
                    "required": ["Othr"],
                    "properties": {
                      "Othr": {
                        "type": "object",
                        "required": ["SchmeNm"],
                        "properties": {
                          "SchmeNm": {
                            "type": "object",
                            "required": ["Cd"],
                            "properties": {
                              "Cd": {
                                "enum": ["NIDN", "CCPT", "ANRU"]
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "Cdtr": {
            "properties": {
              "Id": {
                "properties": {
                  "PrvtId": {
                    "properties": {
                      "Othr": {
                        "properties": {
                          "Id": {
                            "maxLength": 8,
                            "minLength": 6,
                            "type": "string"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "type": "object",
        "required": ["Cdtr"],
        "properties": {
          "Cdtr": {
            "type": "object",
            "required": ["Id"],
            "properties": {
              "Id": {
                "type": "object",
                "required": ["PrvtId"],
                "properties": {
                  "PrvtId": {
                    "type": "object",
                    "required": ["Othr"],
                    "properties": {
                      "Othr": {
                        "type": "object",
                        "required": ["SchmeNm"],
                        "properties": {
                          "SchmeNm": {
                            "type": "object",
                            "required": ["Cd"],
                            "properties": {
                              "Cd": {
                                "const": "CUST"
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "Cdtr": {
            "properties": {
              "Id": {
                "properties": {
                  "PrvtId": {
                    "properties": {
                      "Othr": {
                        "properties": {
                          "Id": {
                            "minLength": 6,
                            "type": "string"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "type": "object",
        "required": ["Cdtr"],
        "properties": {
          "Cdtr": {
            "type": "object",
            "required": ["Id"],
            "properties": {
              "Id": {
                "type": "object",
                "required": ["PrvtId"],
                "properties": {
                  "PrvtId": {
                    "type": "object",
                    "required": ["Othr"],
                    "properties": {
                      "Othr": {
                        "type": "object",
                        "required": ["SchmeNm"],
                        "properties": {
                          "SchmeNm": {
                            "type": "object",
                            "required": ["Cd"],
                            "properties": {
                              "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"]
}

修正说明

  1. 为每个if条件的嵌套层级添加"type": "object",确保每个层级必须是对象类型
  2. 在每个层级添加"required",强制要求路径中的属性必须存在
  3. 修正测试JSON中的语法错误(添加"Id": "123456"后的逗号)

这样修改后,if条件会严格匹配SchmeNm.Cd的值,只有当值完全匹配时才会触发对应的then规则,避免误判导致的验证错误。

内容的提问来源于stack exchange,提问作者Julian Camits

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 11:32:04