是否可以在 JSON 模式中有一个动态大小的数组,其大小基于另一个值
Is it possible to have a dynamically sized array in a JSON Schema whose size is based on another value
我希望在 JSON 中存储计算器(针对现有游戏)的一些建筑数据。问题是有些可以升级,而有些则不能。虽然它们是同一类型的建筑。有没有一种方法可以根据最大级别的值动态设置数组大小,或者我对 JSON 的期望过高?我打算开源该工具,并希望有一个模式可以验证向其中添加 JSON 文件的任何人。使用以下代码,Visual Studio 代码警告我 maxItems 如何期望整数。
{
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"$schema": {
"type":"string"
},
"maxLevels": {
"description": "The maximum level that this building can be upgraded to",
"type":"integer",
"enum": [
1,
5
]
},
"goldCapacity": {
"description": "The maximum amount of gold that the building can hold.",
"type":"array",
"minItems": 1,
"maxItems": {"$ref": "#/properties/maxLevels"},
"items": {
"type":"integer",
"uniqueItems": true
}
}
}
}
JSON(和 JSON 架构)基本上是一组 key/value 对,因此 JSON 本身并没有真正的支持来做你想做的事情。
要完成您想要的,请使用 maxItems
的默认值(例如 0)构造 JSON,获取对您的 JSON 对象的引用,然后在之后更新值您使用 JavaScript:
获得动态值
jsonObj['maxItems'] = yourCalculatedValue;
the proposal for $data reference 允许使用数据中的值作为某些模式关键字的值。使用 $data 参考你可以:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"maxLevel": {
"description": "The maximum level that this building can be upgraded to",
"type":"integer",
"minimum": 1,
"maximum": 5
},
"goldCapacity": {
"description": "The maximum amount of gold that the building can hold.",
"type":"array",
"minItems": 1,
"maxItems": {"$data": "1/maxLevel"},
"items": {
"type":"integer",
"uniqueItems": true
}
}
}
}
这样,属性 "maxLevel" 的值(应该是 >= 1 且 <=5)决定了数组 "goldCapacity" 可以容纳的最大项目数。
目前只有ajv(JavaScript)实现了$data引用,据我所知,正在考虑将其包含在下一版本的规范中(欢迎投票为此)。
我希望在 JSON 中存储计算器(针对现有游戏)的一些建筑数据。问题是有些可以升级,而有些则不能。虽然它们是同一类型的建筑。有没有一种方法可以根据最大级别的值动态设置数组大小,或者我对 JSON 的期望过高?我打算开源该工具,并希望有一个模式可以验证向其中添加 JSON 文件的任何人。使用以下代码,Visual Studio 代码警告我 maxItems 如何期望整数。
{
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"$schema": {
"type":"string"
},
"maxLevels": {
"description": "The maximum level that this building can be upgraded to",
"type":"integer",
"enum": [
1,
5
]
},
"goldCapacity": {
"description": "The maximum amount of gold that the building can hold.",
"type":"array",
"minItems": 1,
"maxItems": {"$ref": "#/properties/maxLevels"},
"items": {
"type":"integer",
"uniqueItems": true
}
}
}
}
JSON(和 JSON 架构)基本上是一组 key/value 对,因此 JSON 本身并没有真正的支持来做你想做的事情。
要完成您想要的,请使用 maxItems
的默认值(例如 0)构造 JSON,获取对您的 JSON 对象的引用,然后在之后更新值您使用 JavaScript:
jsonObj['maxItems'] = yourCalculatedValue;
the proposal for $data reference 允许使用数据中的值作为某些模式关键字的值。使用 $data 参考你可以:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"maxLevel": {
"description": "The maximum level that this building can be upgraded to",
"type":"integer",
"minimum": 1,
"maximum": 5
},
"goldCapacity": {
"description": "The maximum amount of gold that the building can hold.",
"type":"array",
"minItems": 1,
"maxItems": {"$data": "1/maxLevel"},
"items": {
"type":"integer",
"uniqueItems": true
}
}
}
}
这样,属性 "maxLevel" 的值(应该是 >= 1 且 <=5)决定了数组 "goldCapacity" 可以容纳的最大项目数。
目前只有ajv(JavaScript)实现了$data引用,据我所知,正在考虑将其包含在下一版本的规范中(欢迎投票为此)。