【问题标题】:Using enums with AJV's "select/selectCases" keyword使用带有 AJV 的“select/selectCases”关键字的枚举
【发布时间】:2019-04-03 11:00:34
【问题描述】:

是否可以给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,您必须单独说明每种情况。我的问题更多是关于是否可以匹配多个案例并使用单个定义。

【问题讨论】:

  • 您能否指导我查看 ajv 自定义关键字 selectIfselectThen 的文档?我找不到它。
  • 嗨@Relequestual,这些关键字不存在——它是伪代码。我应该明确表示。唯一受支持的关键字是selectselectCasesselectDefault。我想匹配selectCases 中定义的多个值,而不是在每个匹配值的情况下重复或设置相同的引用。当我有 50 个可以匹配的案例时,架构会变得有点庞大。
  • 我认为您想要做的是将您的逻辑包装在 if/then 关键字集中。您之前是否使用过if 并了解它如何与thenelse 的适用性一起工作?
  • 感谢您的快速回复! if/then 是否匹配数据对象本身而不是模式?还是两者兼而有之?
  • 我不熟悉select 的工作原理。 if 的工作原理是,如果 if 的架构对象值成功验证,则应用 then 的架构对象值,否则应用 else(如果提供)。有关示例,请参阅json-schema.org/understanding-json-schema/reference/…

标签: jsonschema ajv


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多