2020-12 草案中用于枚举的 Json 架构

问题描述 投票:0回答:2

我有一个 JSON 模式,用于指定将唯一字符串名称映射到唯一数字的枚举。

这是架构,是的,我知道它不能保证上述要求,但目前我对此并不担心。

{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Module",
"description": "Schema for module definitions",
"type": "object",
"properties": {
    "enums": {
        "type": "object",
        "patternProperties": {
            "^.[^.\"]*$": {
                "$ref": "#/$defs/enum"
            }
        },
        "additionalProperties": false
    }
},
"additionalProperties": false,
"$defs": {
    "enum": {
        "type": "array",
        "items": {
            "type": "array",
            "items": [
                {
                    "type": "string"
                },
                {
                    "type": "integer"
                }
            ],
            "minItems": 2,
            "maxItems": 2
        }
    }
}
}

我正在从 Json.net 转换为 System.Text.Json & Jsonschema.net。下面是使用 json.net 验证的示例 JSON 对象,但未使用 Jsonschema.net 验证,可能是因为 "$schema": "https://json-schema.org/draft/2020-12/schema" 被占用更多认真对待后者。

{
"enums": {
    "Drive Types": [
        [
            "DOL", 1
        ],
        [
            "VSD", 2
        ],
        [
            "Servo", 3
        ]
    ],
    "Conveyor State": [
        [
            "Off", 0
        ],
        [
            "Faulted", 1
        ],
        [
            "Idle", 2
        ]
    ]
}
}

我想要做的是修改我的架构(和 JSON 对象),以便它能够根据草案 2020-12 进行严格验证。我怎样才能做到这一点?

json json.net jsonschema
2个回答
2
投票

2020-12 年,

items
不再具有数组形式。已替换为
prefixItems
。您的模式可以用
items
替换内部 2 元素数组
prefixItems
关键字,这应该是正确的。

参见:https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-01#name-keywords-for-applying-subschemahttps://json-schema.org/understanding-json-schema/reference/array


0
投票

似乎您想要这样的模式

定义

minItems
模式时,不需要
maxItems
prefixItems
。您可以通过设置
items: false

来防止任何其他项目
{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "title": "Module",
    "description": "Schema for module definitions",
    "type": "object",
    "properties": {
        "enums": {
            "type": "object",
            "patternProperties": {
                "^.[^.\"]*$": {
                    "$ref": "#/$defs/enum"
                }
            },
            "unevaluatedProperties": false
        }
    },
    "unevaluatedProperties": false,
    "$defs": {
        "enum": {
            "type": "array",
            "items": {
                "type": "array",
                "prefixItems": [
                    {
                        "type": "string"
                    },
                    {
                        "type": "integer"
                    }
                ],
                "items": false
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.