Требуется условная схема JSON в зависимости от значения - PullRequest
1 голос
/ 27 июня 2019

У меня проблемы с проверками JSON.У меня есть 3 связанных окна выбора на моей странице, и мне нужна проверка, чтобы отразить то, что показывает пользовательский интерфейс.Доступны следующие 3 значения: - Область действия: может быть ScopeNational, ScopeRegional или ScopeInternational - Страна: список стран - Регион: список регионов ("Европа", "Азия" и т. Д.)

В схеме выборки являются объектами с двумя свойствами: «ключ» и «текст», оба являются строками.

Если область действия - «Область действия», то требуются «Страна» и «Регион».Если область действия - «ScopeRegional», то требуется только «Регион».Наконец, если область действия - «ScopeInternational», ни одна из «Страна» или «Регион» не требуется.

Я перепробовал множество настроек с anyOf, oneOf и if-then-else, но я не смог этого добиться. Вотпоследняя схема, которую я попробовал, но безуспешно:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://example.com/root.json",
    "type": "object",
    "title": "Linked scope",
    "default": null,
    "properties": {

      "scope": {
        "$id": "#/properties/scope",
        "title": "Project scope",
        "$ref": "#/definitions/Scope"
      },
      "country": {
        "$id": "#/properties/country",
        "title": "Country",
        "$ref": "#/definitions/Choice"
      },
      "region": {
        "$id": "#/properties/region",
        "title": "Region",
        "$ref": "#/definitions/Choice"
      }
    },
    "oneOf": [
      {
        "properties": {
          "scope": {
            "properties": {
              "key": {
                "const": "ScopeNational"
              }
            }
          },
          "country": {
            "required": [
              "key",
              "text"
            ]
          },
          "region": {
            "required": [
              "key",
              "text"
            ]
          }
        }
      },
      {
        "properties": {
          "scope": {
            "properties": {
              "key": {
                "const": "ScopeRegional"
              }
            }
          },
          "region": {
            "required": [
              "key",
              "text"
            ]
          }
        }
      },
      {
        "properties": {
          "scope": {
            "properties": {
              "key": {
                "const": "ScopeInternational"
              }
            }
          }
        }
      }
    ],
    "required": [
      "scope"
    ],
    "definitions": {
      "Choice": {
        "type": "object",
        "properties": {
          "key": {
            "type": "string"
          },
          "text": {
            "type": "string"
          }
        },
        "required": [
          "key",
          "text"
        ]
      },
      "Scope": {
        "type": "object",
        "properties": {
          "key": {
            "type": "string",
            "enum": [
              "ScopeNational",
              "ScopeRegional",
              "ScopeInternational"
            ]
          },
          "text": {
            "type": "string"
          }
        },
        "required": [
          "key",
          "text"
        ]
      }
    }
  }

Спасибо!

1 Ответ

0 голосов
/ 27 июня 2019

Я немного изменил вашу схему, как показано ниже.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/root.json",
  "type": "object",
  "title": "Linked scope",
  "properties": {
    "scope": {
      "$id": "#/properties/scope",
      "title": "Project scope",
      "$ref": "#/definitions/Scope"
    }
  },
  "oneOf": [
    {
      "properties": {
        "scope": {
          "properties": {
            "key": {
              "const": "ScopeNational"
            }
          }
        },
        "region": {
          "$ref": "#/definitions/Choice"
        },
        "country": {
          "$ref": "#/definitions/Choice"
        }
      }
    },
    {
      "properties": {
        "scope": {
          "properties": {
            "key": {
              "const": "ScopeRegional"
            }
          }
        },
        "region": {
          "$ref": "#/definitions/Choice"
        },
        "country": {
          "$ref": "#/definitions/NullableChoice"
        }
      }
    },
    {
      "properties": {
        "scope": {
          "properties": {
            "key": {
              "const": "ScopeInternational"
            }
          }
        },
        "region": {
          "$ref": "#/definitions/NullableChoice"
        },
        "country": {
          "$ref": "#/definitions/NullableChoice"
        }
      }
    }
  ],
  "required": [
    "scope",
    "country",
    "region"
  ],
  "definitions": {
    "Choice": {
      "type": "object",
      "properties": {
        "key": {
          "type": "string"
        },
        "text": {
          "type": "string"
        }
      },
      "required": [
        "key",
        "text"
      ]
    },
    "NullableChoice": {
      "type": "object",
      "properties": {
        "key": {
          "type": ["string", "null"]
        },
        "text": {
          "type": ["string", "null"]
        }
      },
      "required": [
        "key",
        "text"
      ]
    },
    "Scope": {
      "type": "object",
      "properties": {
        "key": {
          "type": "string",
          "enum": [
            "ScopeNational",
            "ScopeRegional",
            "ScopeInternational"
          ]
        },
        "text": {
          "type": "string"
        }
      },
      "required": [
        "key",
        "text"
      ]
    }
  }
}

Теперь все свойства являются обязательными, и в схему добавлено новое определение NullableChoice.Обратите внимание, что null - это тип, подобный string и number в схеме JSON.

...