JSON Schema with unknown property names

后端 未结 3 1847
太阳男子
太阳男子 2021-02-06 21:41

I want to have a JSON Schema with unknown property names in an array of objects. A good example is the meta-data of a web page:

      \"meta\": {
        \"typ         


        
相关标签:
3条回答
  • 2021-02-06 22:02

    Use patternProperties instead of 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.

      "patternProperties": {
        "^.*$": {
          "anyOf": [
            {"type": "string"},
            {"type": "null"}
          ]
        }
      },
      "additionalProperties": false
    
    0 讨论(0)
  • 2021-02-06 22:04

    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"
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-06 22:04

    The Solution of @jruizaranguren works for me. 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:

    "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"
          }
        ]
      ]
    
    0 讨论(0)
提交回复
热议问题