Да, вы можете. Идея состоит в том, чтобы использовать oneOf
ключевое слово и $ref
для повторного использования определений. Эта схема JSON проверяет одно или другое (я не указал все свойства CustomerInfo, но вы поняли идею, поэтому, пожалуйста, заполните пробелы)
- РЕДАКТИРОВАТЬ 2018-09-01 -
Схема JSON проекта-04:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "JSON schema of Credentiales and CustomerInfo",
"definitions": {
"Credentiales": {
"type": "object",
"properties": {
"Name": {"type": "string"},
"Password": {"type": "string"}
}
},
"Reference": {
"type": "string"
},
"CustomerInfo": {
"type": "object",
"properties": {
"Reference": {"$ref": "#/definitions/Reference"},
"Consumer": {"type": "string"},
"FirstName": {"type": "string"}
!!!! FILL IN THE BLANKS: ADD THE OTHER PROPERTIES HERE AND REMOVE THIS COMMENT !!!
},
"additionalProperties": {"not": {}}
}
},
"oneOf": [
{
"type": "object",
"properties": {
"Credentiales": {
"$ref": "#/definitions/Credentiales"
},
"Reference": {
"$ref": "#/definitions/Reference"
},
"additionalProperties": {"not": {}}
}
},
{"$ref":"#/definitions/CustomerInfo"}
]
}
- РЕДАКТИРОВАТЬ 2018-08-31 -
Версия схемы JSON Draft-06:
{
"$schema": "http://json-schema.org/draft-06/schema",
"$id": "http://example.com/root.json",
"title": "JSON schema of Credentiales and CustomerInfo",
"definitions": {
"Credentiales": {
"type": "object",
"properties": {
"Name": {"type": "string"},
"Password": {"type": "string"}
}
},
"Reference": {
"type": "string"
},
"CustomerInfo": {
"type": "object",
"properties": {
"Reference": {"$ref": "#/definitions/Reference"},
"Consumer": {"type": "string"},
"FirstName": {"type": "string"}
!!!! FILL IN THE BLANKS: ADD THE OTHER PROPERTIES HERE AND REMOVE THIS COMMENT !!!
},
"additionalProperties": false
}
},
"oneOf": [
{
"type": "object",
"properties": {
"Credentiales": {
"$ref": "#/definitions/Credentiales"
},
"Reference": {
"$ref": "#/definitions/Reference"
},
"additionalProperties": false
}
},
{"$ref":"#/definitions/CustomerInfo"}
]
}