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

JSON Schema多条件校验求助:'My Self'旅行者姓名需匹配投保人

Solution for Your JSON Schema Validation Requirements

Let's fix your JSON Schema to properly enforce the two validation rules you need. Your original schema covers basic structure, but we need to add targeted checks for the traveller array conditions.

Key Gaps in Your Original Schema

  • It doesn’t ensure at least one traveller has relationship: "My Self"
  • It incorrectly links all travellers’ fullName to the policyHolder’s, which isn’t your requirement (only the "My Self" traveller needs this match)

Corrected JSON Schema

{
  "type": "object",
  "required": ["policyHolder", "traveller"],
  "properties": {
    "policyHolder": {
      "type": "object",
      "required": ["fullName"],
      "properties": {
        "fullName": { "type": "string" }
      }
    },
    "traveller": {
      "type": "array",
      "minItems": 1,
      // Rule 1: Guarantee at least one traveller has relationship "My Self"
      "contains": {
        "properties": {
          "relationship": { "const": "My Self" }
        },
        "required": ["relationship"]
      },
      "items": {
        "type": "object",
        "properties": {
          "fullName": { "type": "string" },
          "relationship": { "type": "string" }
        },
        "required": ["fullName", "relationship"],
        // Rule 2: Match fullName to policyHolder if relationship is "My Self"
        "if": {
          "properties": {
            "relationship": { "const": "My Self" }
          },
          "required": ["relationship"]
        },
        "then": {
          "properties": {
            "fullName": { "$data": "/policyHolder/fullName" }
          }
        }
      }
    }
  }
}

Breakdown of the Changes

  1. Rule 1 Enforcement:

    • We added the contains keyword to the traveller array. This checks that at least one element in the array meets the nested condition (where relationship is exactly "My Self").
  2. Rule 2 Enforcement:

    • Inside the items schema for travellers, we used an if/then block.
    • The if clause triggers when a traveller’s relationship is "My Self".
    • The then clause uses the $data keyword to reference the policyHolder.fullName value, ensuring the traveller’s fullName matches exactly. Note: $data works in JSON Schema draft-06 and later—make sure your validator supports this version.

Test Scenarios

Your provided JSON instance:

{
  "policyHolder": { "fullName": "A" },
  "traveller": [
    { "fullName": "B", "relationship": "Spouse" },
    { "fullName": "A", "relationship": "My Self" }
  ]
}
  • This will pass validation: it includes a "My Self" traveller, and their fullName matches the policyHolder’s.

Invalid Cases That Fail

  • No "My Self" traveller: Violates Rule 1
  • "My Self" traveller has fullName "C" (when policyHolder is "A"): Violates Rule 2
  • Missing policyHolder or traveller: Fails top-level required checks

内容的提问来源于stack exchange,提问作者rajya vardhan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 04:40:55