json-具有 x 类型的附加属性和至少一个的模式

json-schema with additional properties of type x and at least one

问题:

有没有办法告诉 json-schema 我想要 x 类型的额外属性并且至少有一个?

简短说明(基于下面的代码块):

我想要一个包含一项必填项的 json 文件,ab 是可选的,我至少需要一项额外的项目。 附加项的名称必须灵活。 出于这个原因,我无法给它一个特定的名称并根据需要对其进行标记。 因为 b 是可选的,所以我无法使用 'minProperties'

类似于:(不存在)

"additionalProperties": {
    "type": "string",
    "minAdditionalProperties": 1
  }

未完成json-schema

{
 "$schema": "http://json-schema.org/draft-07/schema",
  "type": "object",
  "properties": {
    "a": {
        "type": "number"
    },
    "b": {
        "type": "number" 
    }
  },
  "required": ["a"],
  "additionalProperties": {
    "type": "string"
  }
}

预期结果:

b 总是可选的

invalid:
{
    "a": 1
}
----
{
    "a": 1,
    "b": 2
}

valid:
{
    "a": 1,
    "x": "2"
}
----
{
    "a": 1,
    "x1": "2",
    "x2": "4"  
}

这样的事情有可能吗?

And because b is optional, I'm not able to use 'minProperties'

为什么不呢? minProperties: 2 肯定会满足您的需求:'a' 是必需的,因此还需要一个额外的 属性。

如果除了可选的 属性 'b' 之外还需要一个额外的 属性,那么您可以使用 if/then/else:

  "if": {
    "required": [ "b" ]
  },
  "then": {
    "minProperties": 3
  }
  "else": {
    "minProperties": 2
  }