JSON模式 – 子类型取决于父元素名称(使用Json.Net)

huangapple go评论52阅读模式
英文:

JSON Schema - Child type dependant on parent element name (with Json.Net)

问题

以下是翻译好的部分:

{
  "type": "object",
  "properties": {
    "SomeForms": {
      "additionalProperties": false,
      "properties": {
        "Forms": {
          "type": "object",
          "additionalProperties": false,
          "patternProperties": {
            "^(TestTestForm1|TestForm2)$": {
              "type": "object",
              "properties": {
                "Options": {
                  "type": "object",
                  "additionalProperties": false,
                  "patternProperties": {
                    "^(ExampleDropdown|ExampleCheckboxes)$": {
                      "type": "object",
                      "properties": {
                        "DefaultValue": {
                          "type": "string"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
{
  "SomeForms": {
    "Forms": {
      "TestTestForm1": {
        "Options": {
          "ExampleDropdown": {
            "DefaultValue": "Dropdown"
          }
        }
      }
    }
  }
}

你想要根据父级的名称来设置DefaultValue的不同类型,可以使用Json.NET库的JObject来处理JSON对象。以下是一个示例的C#代码,演示如何实现这一功能:

using Newtonsoft.Json.Linq;

// 将测试JSON解析为JObject
JObject testJson = JObject.Parse(yourTestJsonString);

// 获取父级表单的名称
string parentFormName = testJson["SomeForms"]["Forms"].First.Path;

// 根据父级表单名称设置DefaultValue的类型
if (parentFormName == "TestTestForm1")
{
    testJson["SomeForms"]["Forms"][parentFormName]["Options"]["ExampleDropdown"]["DefaultValue"] = new JValue(123); // 设置为数字类型
}
else if (parentFormName == "TestForm2")
{
    testJson["SomeForms"]["Forms"][parentFormName]["Options"]["ExampleDropdown"]["DefaultValue"] = new JValue("abc"); // 设置为字符串类型
}

// 将修改后的JSON转回字符串
string modifiedJsonString = testJson.ToString();

这段代码首先解析了测试JSON字符串为JObject,然后获取了父级表单的名称,并根据名称设置了DefaultValue的不同类型。最后,将修改后的JSON转换回字符串。

英文:

Having following JSON schema:

{
  "type": "object",
  "properties": {
    "SomeForms": {
      "additionalProperties": false,
      "properties": {
        "Forms": {
          "type": "object",
          "additionalProperties": false,
          "patternProperties": {
            "^(TestTestForm1|TestForm2)$": {
              "type": "object",
              "properties": {
                "Options": {
                  "type": "object",
                  "additionalProperties": false,
                  "patternProperties": {
                    "^(ExampleDropdown|ExampleCheckboxes)$": {
                      "type": "object",
                      "properties": {
                        "DefaultValue": {
                          "type": "string"
}}}}}}}}}}}}}

And following test JSON:

{
  "SomeForms": {
    "Forms": {
      "TestTestForm1": {
        "Options": {
          "ExampleDropdown": {
            "DefaultValue": "Dropdown"
}}}}}}

How I can make the DefaultValue have different type, depending on the parent "^(TestTestForm1|TestForm2)$"?
Example:
- if the parent is TestTestForm1, I want DefaultValue to be number
- if the parent is TestTestForm1, I want DefaultValue to be string

I am using Json.NET library to generate schemas, so the C# example would be best, but having JSON schema example would also be a clue for me.

Thanks in advance.

I tried:

"Forms": {
    "allOf": [
	{ 
            "if": { "patternProperties": { "const": "TestTestForm1" } }, 
            "then": { "patternProperties" : { "TestTestForm1": { "properties": { "Options": { "patternProperties": { "properties": { "DefaultValue": { "type": "number" } } } } } } } } }
],

But I does not work.
Main thing I don't know, is how to make "if" check on the parent name, not the value (above I am using "const", which I guess is correct only for checking the "value" of property, not it's name).

答案1

得分: 0

你不能在JSON Schema中"查找"模式树。因此,你需要像你做的那样将条件放在Forms对象的级别上。但这会导致一个相当冗长的解决方案,如下所示:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "description": "使用 JSONBuddy 生成的 JSON 模式 https://www.json-buddy.com",
  "type": "object",
  "properties": {
    "SomeForms": {
      "additionalProperties": false,
      "properties": {
        "Forms": {
          "type": "object",
          "additionalProperties": false,
          "patternProperties": {
            "^(TestTestForm1|TestForm2)$": {
              "type": "object",
              "properties": {
                "foo": {},
                "bar": {}
              }
            }
          },
          "allOf": [
            {
              "if": {
                "properties": { "TestTestForm1": true }
              },
              "then": {
                "properties": {
                  "TestTestForm1": {
                    "type": "object",
                    "properties": {
                      "Options": {
                        "type": "object",
                        "patternProperties": {
                          "^(ExampleDropdown|ExampleCheckboxes)$": {
                            "type": "object",
                            "properties": {
                              "DefaultValue": {
                                "type": "string"
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            },
            {
              "if": {
                "properties": { "TestForm2": true }
              },
              "then": {
                "properties": {
                  "TestForm2": {
                    "type": "object",
                    "properties": {
                      "Options": {
                        "type": "object",
                        "patternProperties": {
                          "^(ExampleDropdown|ExampleCheckboxes)$": {
                            "type": "object",
                            "properties": {
                              "DefaultValue": {
                                "type": "number"
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

在你可以添加通用模式部分的地方,我添加了"foo"和"bar"键的空定义。

我想不出任何更短的版本来解决这个问题。

英文:

You can't "look up" the schema tree in JSON Schema. So you need to put the condition the level of the Forms object as you did. But this results in a quite verbose solution like this:

{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "JSON schema generated with JSONBuddy https://www.json-buddy.com",
"type": "object",
"properties": {
"SomeForms": {
"additionalProperties": false,
"properties": {
"Forms": {
"type": "object",
"additionalProperties": false,
"patternProperties": {
"^(TestTestForm1|TestForm2)$": {
"type": "object",
"properties": {
"foo": { },
"bar": { }
}
}
},
"allOf": [
{
"if": {
"properties": { "TestTestForm1": true }
},
"then": {
"properties": {
"TestTestForm1": {
"type": "object",
"properties": {
"Options": {
"type": "object",
"patternProperties": {
"^(ExampleDropdown|ExampleCheckboxes)$": {
"type": "object",
"properties": {
"DefaultValue": {
"type": "string"
}
}
}
}
}
}
}
}
}
},
{
"if": {
"properties": { "TestForm2": true }
},
"then": {
"properties": {
"TestForm2": {
"type": "object",
"properties": {
"Options": {
"type": "object",
"patternProperties": {
"^(ExampleDropdown|ExampleCheckboxes)$": {
"type": "object",
"properties": {
"DefaultValue": {
"type": "number"
}
}
}
}
}
}
}
}
}
}
]
}
}
}
}
}

I added empty definitions for "foo" and "bar" keys at the place where you could add parts of the schema which are in common.

I can't think of any shorter version to solve this.

huangapple
  • 本文由 发表于 2023年6月22日 17:15:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定