Использование перечислений с ключевым словом select / selectCases в AJV - PullRequest
0 голосов
/ 03 апреля 2019

Можно ли дать select схему для сопоставления с регистром?

У меня есть вариант использования, который я хочу выбрать и сопоставить в списке принятых значений:

{
  "type": "object",
  "properties": {
    "addressCountry": {
      "type": ["string", "null"]
    }
  },
  "select": {
    "$data": "0/addressCountry"
  },
  "selectIf": {
    "type": "string",
    "enum": ["FR", "JP", "US", "NZ", "DE"]
  },
  "selectThen": {
    "addressCountry": {
      "type": "string"
    }
  }
}

РЕДАКТИРОВАТЬ: обратите внимание, что selectIf и selectThen не являются ключевыми словами, поддерживаемыми AJV.Он поддерживает selectCases и selectDefault, однако с selectCases вы должны указывать каждый случай индивидуально.Мой вопрос больше о том, возможно ли сопоставить несколько случаев и использовать одно определение.

1 Ответ

0 голосов
/ 04 апреля 2019

@ Relequestual помог мне решить эту проблему вне SO.Мне вообще не нужно было использовать select, я мог просто использовать if/then/else примерно так:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "properties": {
    "contactByPost": {
      "type": "boolean"
    },
    "streetAddress1": {
      "type": ["string", "null"]
    },
    "streetAddress2": {
      "type": ["string", "null"]
    },
    "streetAddress3": {
      "type": ["string", "null"]
    },
    "locality": {
      "type": ["string", "null"]
    },
    "region": {
      "type": ["string", "null"]
    },
    "postCode": {
      "type": ["string", "number", "null"]
    },
    "country": {
      "type": ["string", "null"]
    }
  },
  "if": {
    "type": "object",
    "properties": {
      "contactByPost": {
        "type": "boolean",
        "const": true
      }
    }
  },
  "then": {
    "if": {
      "type": "object",
      "properties": {
        "country": {
          "type": "string",
          "enum": ["JP", "US"]
        }
      }
    },
    "then": {
      "type": "object",
      "properties": {
        "contactByPost": { "type": "boolean" },
        "streetAddress1": { "type": "string" },
        "locality": { "type": "string" },
        "region": { "type": "string" },
        "postCode": { "type": ["string", "number"] },
        "country": { "type": "string" }
      },
      "required": ["streetAddress1", "locality", "region", "postCode", "country"]
    },
    "else": {
      "type": "object",
      "properties": {
        "contactByPost": { "type": "boolean" },
        "streetAddress1": { "type": "string" },
        "locality": { "type": "string" },
        "region": { "type": ["string", "null"] },
        "country": { "type": "string" }
      },
      "required": ["streetAddress1", "locality", "country"]
    }
  }
}

Даст следующие результаты:

// Valid
{
  "contactByPost": false
}

{
  "contactByPost": true,
  "streetAddress1": "123 Alphabet St",
  "streetAddress2": null,
  "streetAddress3": null,
  "locality": "City",
  "region": "State",
  "postCode": "12345",
  "country": "JP"
}

{
  "contactByPost": true,
  "streetAddress1": "123 Alphabet St",
  "streetAddress2": null,
  "streetAddress3": null,
  "locality": "City",
  "region": "State",
  "postCode": "12345",
  "country": "US"
}

{
  "contactByPost": true,
  "streetAddress1": "123 Alphabet St",
  "streetAddress2": null,
  "streetAddress3": null,
  "locality": "City",
  "region": "State",
  "postCode": "12345",
  "country": "NZ"
}

{
  "contactByPost": true,
  "streetAddress1": "123 Alphabet St",
  "streetAddress2": null,
  "streetAddress3": null,
  "locality": "City",
  "region": null,
  "postCode": null,
  "country": "NZ"
}

// Invalid
{
  "contactByPost": true
}

{
  "contactByPost": true,
  "streetAddress1": "123 Alphabet St",
  "streetAddress2": null,
  "streetAddress3": null,
  "locality": "City",
  "region": null,
  "postCode": null,
  "country": "JP"
}

{
  "contactByPost": true,
  "streetAddress1": "123 Alphabet St",
  "streetAddress2": null,
  "streetAddress3": null,
  "locality": "City",
  "region": null,
  "postCode": null,
  "country": "US"
}
...