如何使用 JSON 模式验证具有随机名称的子 属性 的 JSON 属性

How to use JSON Schema to validate a JSON property of a sub-property with a random name

我想知道如何使用随机名称验证子 属性 中的 "haha"?

{
  "shipping_address": {
      "randomName1":{
      "haha":"ddd"},
      "randomName2":{
      "haha":"ddd"},
      "randomName3":{
      "haha":"ddd"},
  }
}

我试过简单地使用 allOf,但我的不起作用:

{
  "$schema": "http://json-schema.org/draft-6/schema#",
  "type": "object",
  "properties": {
    "shipping_address": {
      "allOf": [
        { "properties":
          { "haha": { "type": "integer" } }
        }
      ]
    }
  }
}

你可以在这里试一试:https://www.jsonschemavalidator.net/

使用图案属性

 {
      "$schema": "http://json-schema.org/draft-6/schema#",
      "type": "object",
      "properties": {
        "shipping_address": {
          "patternProperties": {
            "^.*$": {          
                  "properties": {
                    "haha":{
                        "type":"integer"
                    }                    
                }      
            }
          }
        }
      }
    }

正如 vearutop 评论的那样,改进版本:

{
  "$schema": "http://json-schema.org/draft-6/schema#",
    "type": "object",
      "properties": {
        "shipping_address": {
          "additionalProperties":{
            "properties":{
              "haha":{
                "type":"integer"
              }
            }
          }
        }
      }          
}