将枚举与AJV的“ select / selectCases”关键字一起使用

时间:2019-04-03 11:00:35

标签: jsonschema ajv

是否可以提供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"
    }
  }
}

编辑:请注意,selectIfselectThen不是AJV支持的关键字。它确实支持selectCasesselectDefault,但是对于selectCases,您必须分别说明每种情况。我的问题更多是关于是否可以匹配多个案例并使用一个定义。

1 个答案:

答案 0 :(得分:0)

@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"
}