简体   繁体   English

具有未知属性名称的 JSON Schema

[英]JSON Schema with unknown property names

I want to have a JSON Schema with unknown property names in an array of objects.我想在对象数组中有一个带有未知属性名称的 JSON 模式。 A good example is the meta-data of a web page:一个很好的例子是网页的元数据:

      "meta": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "unknown-attribute-1": {
              "type": "string"
            },
            "unknown-attribute-2": {
              "type": "string"
            },
            ...
          }
        }
      }

Any ideas please, or other way to reach the same?请问有什么想法,或者其他方法可以达到同样的效果吗?

Use patternProperties instead of properties .使用patternProperties而不是properties In the example below, the pattern match regex .* accepts any property name and I am allowing types of string or null only by using "additionalProperties": false .在下面的示例中,模式匹配正则表达式.*接受任何属性名称,并且我仅通过使用"additionalProperties": false来允许stringnull类型。

  "patternProperties": {
    "^.*$": {
      "anyOf": [
        {"type": "string"},
        {"type": "null"}
      ]
    }
  },
  "additionalProperties": false

... or if you just want to allow a string in your "object" (like in the original question): ...或者如果您只想在“对象”中允许一个字符串(就像在原始问题中一样):

  "patternProperties": {
    "^.*$": {
        {"type": "string"},
    }
  },
  "additionalProperties": false

You can make constraints on properties not explicitly defined.您可以对未明确定义的属性进行约束。 The following schema enforces "meta" to be an array of objects whose properties are of type string:以下模式将“元”强制为属性为字符串类型的对象数组:

{
    "properties" : {
        "meta" : {
            "type" : "array",
            "items" : {
                "type" : "object",
                "additionalProperties" : {
                    "type" : "string"
                }
            }
        }
    }
}

In case you just want to have an array of strings, you may use the following schema:如果您只想拥有一个字符串数组,您可以使用以下模式:

{
    "properties" : {
        "meta" : {
            "type" : "array",
            "items" : {
                "type" : "string"
            }
        }
    }
}

The Solution of @jruizaranguren works for me. @jruizaranguren 的解决方案对我有用。 Though I am the same who defines the schema, i choosed another solution虽然我和定义架构的人一样,但我选择了另一种解决方案

"meta": {
        "type": "array",
        "items": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string"
              },
              "value": {
                "type": "string"
              }
            }
          }
        }
      }

I converted the object to an array of name-value objects An example of a valid JSON:我将对象转换为名称-值对象数组 一个有效 JSON 的示例:

"meta": [
    [
      {
        "name": "http-equiv",
        "value": "Content-Type"
      },
      {
        "name": "content",
        "value": "text/html; charset=UTF-8"
      }
    ],
    [
      {
        "name": "name",
        "value": "author"
      },
      {
        "name": "content",
        "value": "Astrid Florence Cassing"
      }
    ]
  ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM