英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论