是否有可能对属性的名称不可知?
Is it possible to be agnostic on the properties' names?
假设我想要一个超级英雄漫画中角色的架构。我希望架构验证 json 个这样的对象:
{
"Name": "Roberta",
"Age": 15,
"Abilities": {
"Super_Strength": {
"Cost": 10,
"Effect": "+5 to Strength"
}
}
}
我的想法是这样做:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "characters_schema.json",
"title": "Characters",
"description": "One of the characters for my game",
"type": "object",
"properties": {
"Name": {
"type": "string"
},
"Age": {
"type": "integer"
},
"Abilities": {
"description": "what the character can do",
"type": "object"
}
},
"required": ["Name", "Age"]
}
并为能力使用第二个模式:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "abilities_schema.json",
"title": "Abilities",
"type": "object",
"properties": {
"Cost": {
"description": "how much mana the ability costs",
"type": "integer"
},
"Effect": {
"type": "string"
}
}
}
但我不知道如何合并角色中的能力。我可以很容易地调整架构,以便它验证格式如下的字符:
{
"Name": "Roberta",
"Age": 15,
"Abilities": [
{
"Name": "Super_Strength"
"Cost": 10,
"Effect": "+5 to Strength"
}
]
}
但是因为我需要将能力的名称用作密钥,所以我不知道该怎么做。
您需要使用 additionalProperties
关键字。
The behavior of this keyword depends on the presence and annotation
results of "properties" and "patternProperties" within the same schema
object. Validation with "additionalProperties" applies only to the
child values of instance names that do not appear in the annotation
results of either "properties" or "patternProperties".
https://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.10.3.2.3
通俗地说,如果您没有定义 properties
或 patternProperties
,additionalProperties
的架构值将应用于该实例位置的对象中的所有值。
通常只给 additionalProperties
一个 true
或 false
值,但请记住,布尔值是有效的架构值。
如果您对对象的键有限制,您可能希望使用 patternPoperties
后跟 additionalProperties: false
。
假设我想要一个超级英雄漫画中角色的架构。我希望架构验证 json 个这样的对象:
{
"Name": "Roberta",
"Age": 15,
"Abilities": {
"Super_Strength": {
"Cost": 10,
"Effect": "+5 to Strength"
}
}
}
我的想法是这样做:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "characters_schema.json",
"title": "Characters",
"description": "One of the characters for my game",
"type": "object",
"properties": {
"Name": {
"type": "string"
},
"Age": {
"type": "integer"
},
"Abilities": {
"description": "what the character can do",
"type": "object"
}
},
"required": ["Name", "Age"]
}
并为能力使用第二个模式:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "abilities_schema.json",
"title": "Abilities",
"type": "object",
"properties": {
"Cost": {
"description": "how much mana the ability costs",
"type": "integer"
},
"Effect": {
"type": "string"
}
}
}
但我不知道如何合并角色中的能力。我可以很容易地调整架构,以便它验证格式如下的字符:
{
"Name": "Roberta",
"Age": 15,
"Abilities": [
{
"Name": "Super_Strength"
"Cost": 10,
"Effect": "+5 to Strength"
}
]
}
但是因为我需要将能力的名称用作密钥,所以我不知道该怎么做。
您需要使用 additionalProperties
关键字。
The behavior of this keyword depends on the presence and annotation results of "properties" and "patternProperties" within the same schema object. Validation with "additionalProperties" applies only to the child values of instance names that do not appear in the annotation results of either "properties" or "patternProperties".
https://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.10.3.2.3
通俗地说,如果您没有定义 properties
或 patternProperties
,additionalProperties
的架构值将应用于该实例位置的对象中的所有值。
通常只给 additionalProperties
一个 true
或 false
值,但请记住,布尔值是有效的架构值。
如果您对对象的键有限制,您可能希望使用 patternPoperties
后跟 additionalProperties: false
。