JSON 架构:枚举在对象数组中不起作用
JSON Schema: Enum not working in an array of objects
枚举似乎不适用于对象数组。我正在使用 2020-12 草案,这是我的 JSON 结构如下:
{
"main": {
"items": [
{
"attribute1": "Option 1"
},
{
"attribute1": "Option 2",
"attribute2": "some random string"
}
]
}
}
属性 1 是强制性的,它必须是三个值之一 - “选项 1”、“选项 2”或“选项 3”。
我尝试了几个 JSON 模式的变体,但似乎都没有满足要求。
变体 1:作为 $ref
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schema.json",
"$defs": {
"my_shiny_object": {
"type": "object",
"required": [
"attribute1"
],
"properties": {
"attribute1": {
"description": "This is mandatory attribute 1 with an enum",
"type": "string",
"enum": [
"Option 1",
"Option 2",
"Option 3"
]
},
"attribute2": {
"type": "string",
"description": "This is an optional attribute 2"
}
}
}
},
"title": "root",
"description": "Root Element.",
"type": "object",
"required": [
"main"
],
"properties": {
"main": {
"type": "object",
"title": "main",
"description": "Main is the top level field.",
"required": [
"items"
],
"properties": {
"date_time": {
"type": "string",
"format": "date-time",
"title": "Date/time optional",
"description": "Date time"
}
},
"items": {
"type": "array",
"description": "Items included in main",
"items": {
"$ref": "#/$defs/my_shiny_object"
}
}
}
}
}
变体 2:内联定义
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schema.json",
"title": "root",
"description": "Root Element.",
"type": "object",
"required": [
"main"
],
"properties": {
"main": {
"type": "object",
"title": "main",
"description": "Main is the top level field.",
"required": [
"items"
],
"properties": {
"date_time": {
"type": "string",
"format": "date-time",
"title": "Date/time optional",
"description": "Date time"
}
},
"items": {
"type": "array",
"description": "Items included in main",
"items": {
"type": "object",
"required": [
"attribute1"
],
"properties": {
"attribute1": {
"description": "This is mandatory attribute 1 with an enum",
"type": "string",
"enum": [
"Option 1",
"Option 2",
"Option 3"
]
},
"attribute2": {
"type": "string",
"description": "This is an optional attribute 2"
}
}
}
}
}
}
}
为 attribute1 字段提供不正确的值甚至排除它都不会触发任何验证错误。我尝试使用 Hyperjump JSV and jschon.dev.
您没有在正确的位置定义您的“项目”属性。 /properties/main/items
的架构需要移动到 /properties/main/properties/items
。然后它将按照您的预期进行评估。
枚举似乎不适用于对象数组。我正在使用 2020-12 草案,这是我的 JSON 结构如下:
{
"main": {
"items": [
{
"attribute1": "Option 1"
},
{
"attribute1": "Option 2",
"attribute2": "some random string"
}
]
}
}
属性 1 是强制性的,它必须是三个值之一 - “选项 1”、“选项 2”或“选项 3”。
我尝试了几个 JSON 模式的变体,但似乎都没有满足要求。
变体 1:作为 $ref
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schema.json",
"$defs": {
"my_shiny_object": {
"type": "object",
"required": [
"attribute1"
],
"properties": {
"attribute1": {
"description": "This is mandatory attribute 1 with an enum",
"type": "string",
"enum": [
"Option 1",
"Option 2",
"Option 3"
]
},
"attribute2": {
"type": "string",
"description": "This is an optional attribute 2"
}
}
}
},
"title": "root",
"description": "Root Element.",
"type": "object",
"required": [
"main"
],
"properties": {
"main": {
"type": "object",
"title": "main",
"description": "Main is the top level field.",
"required": [
"items"
],
"properties": {
"date_time": {
"type": "string",
"format": "date-time",
"title": "Date/time optional",
"description": "Date time"
}
},
"items": {
"type": "array",
"description": "Items included in main",
"items": {
"$ref": "#/$defs/my_shiny_object"
}
}
}
}
}
变体 2:内联定义
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schema.json",
"title": "root",
"description": "Root Element.",
"type": "object",
"required": [
"main"
],
"properties": {
"main": {
"type": "object",
"title": "main",
"description": "Main is the top level field.",
"required": [
"items"
],
"properties": {
"date_time": {
"type": "string",
"format": "date-time",
"title": "Date/time optional",
"description": "Date time"
}
},
"items": {
"type": "array",
"description": "Items included in main",
"items": {
"type": "object",
"required": [
"attribute1"
],
"properties": {
"attribute1": {
"description": "This is mandatory attribute 1 with an enum",
"type": "string",
"enum": [
"Option 1",
"Option 2",
"Option 3"
]
},
"attribute2": {
"type": "string",
"description": "This is an optional attribute 2"
}
}
}
}
}
}
}
为 attribute1 字段提供不正确的值甚至排除它都不会触发任何验证错误。我尝试使用 Hyperjump JSV and jschon.dev.
您没有在正确的位置定义您的“项目”属性。 /properties/main/items
的架构需要移动到 /properties/main/properties/items
。然后它将按照您的预期进行评估。