{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/complex",
  "title": "PaymentMethod",
  "type": "object",
  "required": [
    "kind"
  ],
  "oneOf": [
    {
      "$ref": "#/$defs/Card"
    },
    {
      "$ref": "#/$defs/BankTransfer"
    },
    {
      "$ref": "#/$defs/Wallet"
    }
  ],
  "$defs": {
    "Card": {
      "type": "object",
      "properties": {
        "kind": {
          "const": "card"
        },
        "last4": {
          "type": "string",
          "pattern": "^[0-9]{4}$"
        },
        "brand": {
          "type": "string",
          "enum": [
            "visa",
            "mc",
            "amex"
          ]
        },
        "expires": {
          "type": "string",
          "format": "date"
        }
      },
      "required": [
        "kind",
        "last4",
        "brand",
        "expires"
      ]
    },
    "BankTransfer": {
      "type": "object",
      "properties": {
        "kind": {
          "const": "bank"
        },
        "iban": {
          "type": "string"
        },
        "bic": {
          "type": "string"
        }
      },
      "required": [
        "kind",
        "iban"
      ]
    },
    "Wallet": {
      "type": "object",
      "properties": {
        "kind": {
          "const": "wallet"
        },
        "provider": {
          "type": "string",
          "enum": [
            "apple",
            "google",
            "paypal"
          ]
        },
        "accountId": {
          "type": "string"
        }
      },
      "required": [
        "kind",
        "provider",
        "accountId"
      ]
    }
  }
}