jsonschema 枚举值以另一个枚举值为条件

jsonschema enum values conditional on another enum value

我的应用程序有 table 个 acceptable 输入组合:

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

没有其他值组合被接受table。

如何在 JSON 架构中对其进行编码? "rest of the schema" 看起来像这样:

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

目前我的应用程序正在使用 Draft 4,因为它是 jsonschema package.

的最后 stable 版本所支持的

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

{
  "$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 ...
    ]
  }
}