Schema condizionale (oneOf)
Avanzato JSON Schema
Un JSON Schema che usa il discriminatore oneOf per la validazione polimorfa dei metodi di pagamento. Pronto da copiare e testare.
{
"$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"]
}
]
} Apri nello strumento
Strumenti correlati
Altri esempi
- schema
- conditional
- if-then-else
- advanced