跳转到主要内容
CuteJSON
↑↓ 导航 Enter 选择 Esc 关闭

条件 Schema (oneOf)

高级 JSON Schema

使用 oneOf 判别器进行多态支付方式验证的 JSON Schema。 一个真实、可直接使用的示例,你可以复制、调整,或用来测试自己的代码。

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "PaymentMethod",
  "type": "object",
  "discriminator": { "propertyName": "type" },
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "type": { "const": "credit_card" },
        "cardNumber": { "type": "string", "pattern": "^[0-9]{16}$" },
        "expMonth": { "type": "integer", "minimum": 1, "maximum": 12 },
        "expYear": { "type": "integer", "minimum": 2024 }
      },
      "required": ["type", "cardNumber", "expMonth", "expYear"]
    },
    {
      "type": "object",
      "properties": {
        "type": { "const": "paypal" },
        "email": { "type": "string", "format": "email" }
      },
      "required": ["type", "email"]
    },
    {
      "type": "object",
      "properties": {
        "type": { "const": "bank_transfer" },
        "iban": { "type": "string" },
        "bic": { "type": "string" }
      },
      "required": ["type", "iban"]
    }
  ]
}
JSON 示例