【问题标题】:jsonschema enum values conditional on another enum valuejsonschema 枚举值以另一个枚举值为条件
【发布时间】:2019-01-29 00:51:37
【问题描述】:

我有一张我的应用程序可接受的输入组合表:

noises   appearance
------   ----------
squeaks  fluffy
purrs    fluffy
hisses   fluffy
peeps    feathers
chirps   feathers
squeaks  feathers
hisses   scaly

不接受其他值组合。

如何在 JSON Schema 中对其进行编码? “架构的其余部分”看起来像这样:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "required": ["noise", "appearance"]
    "properties": {
      "noise": ...,
      "appearance": ...
    }
  }

目前我的应用程序正在使用 Draft 4,因为它是 jsonschema package 的最新稳定版本所支持的。

【问题讨论】:

    标签: jsonschema


    【解决方案1】:

    鉴于选项数量很少且数量固定,我认为最好的办法是列举所有选项。架构将比替代方案更易于阅读和维护。

    {
      "$schema": "http://json-schema.org/draft-04/schema#",
      "type": "array",
      "items": {
        "type": "object",
        "required": ["noise", "appearance"],
        "properties": {
          ... any common properties ...
        },
        "anyOf": [
          {
            "properties": {
              "noise": { "enum": ["squeaks"] },
              "appearance": { "enum": ["fluffy"] }
            }
          },
          {
            "properties": {
              "noise": { "enum": ["purrs"] },
              "appearance": { "enum": ["fluffy"] }
            }
          },
          ... other combinations ...
        ]
      }
    }
    

    【讨论】:

    猜你喜欢
    • 2021-10-21
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 2012-08-31
    相关资源
    最近更新 更多