具有未知属性名称的JSON模式

Tha*_*afa 30 json jsonschema

我想在对象数组中使用具有未知属性名称的JSON模式.一个很好的例子是网页的元数据:

      "meta": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "unknown-attribute-1": {
              "type": "string"
            },
            "unknown-attribute-2": {
              "type": "string"
            },
            ...
          }
        }
      }
Run Code Online (Sandbox Code Playgroud)

请问任何想法,或以其他方式达到相同的目的?

pin*_*man 54

patternProperties而不是properties.在下面的示例中,模式匹配正则表达式.*接受任何属性名称,我允许类型stringnull仅使用"additionalProperties": false.

  "patternProperties": {
    "^.*$": {
      "anyOf": [
        {"type": "string"},
        {"type": "null"}
      ]
    }
  },
  "additionalProperties": false
Run Code Online (Sandbox Code Playgroud)


jru*_*ren 8

您可以对未明确定义的属性进行约束.以下模式将"meta"强制为属性为string类型的对象数组:

{
    "properties" : {
        "meta" : {
            "type" : "array",
            "items" : {
                "type" : "object",
                "additionalProperties" : {
                    "type" : "string"
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您只想拥有一个字符串数组,可以使用以下模式:

{
    "properties" : {
        "meta" : {
            "type" : "array",
            "items" : {
                "type" : "string"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)