【问题标题】:In JSON schema, define and reference a reusable enum type?在 JSON 模式中,定义和引用可重用的枚举类型?
【发布时间】:2021-04-29 13:30:10
【问题描述】:

我注意到以下内容:Reusable enum types in json schema,它谈到了在 JSON 模式中定义可重用的枚举类型。

我会假设使用这种可重用的枚举类型是微不足道的,只需为“类型”值指定(在这种情况下)“MyEnum”的值。

我不知道 Oxygen XML 的结果是否权威,但我尝试了类似以下的方法:

{
    "$schema": "https://json-schema.org/draft/2019-09/schema#",
    "type": "object",
    "properties": {
        "content": {"$ref": "#/definitions/content_type"}
    },
    "additionalProperties": false,
    "definitions": {
        "costCategory_type": {
            "type": "object",
            "enum": ["VH", "H", "M", "L"]  
        },
        "allowedDevices_type": {
            "type": "object",
            "properties": {
                "costCategory": {
                    "type": "costCategory_type"
                },

在靠近底部的那一行,我引用了“costCategory_type”,Oxygen 给了我一个语法错误,说

#/definitions/allowedDevices_type/properties/costCategory/type:未知类型:[costCategory_type]

我错过了什么?

【问题讨论】:

    标签: jsonschema


    【解决方案1】:

    是的,type 关键字只能具有列表 nullbooleanobjectarraystringnumberinteger 中的值。您可以使用 $ref 关键字引用定义:

    ...
    "properties": {
      "costCategory": {
        "$ref": "#/definitions/costCategory_type",
      }
    }
    

    (顺便说一句,您的定义永远不会成功评估,因为您将其定义为“对象”类型,但枚举中的值列表都是字符串。)

    【讨论】:

    • 我认为,早在 draft-02 或 03 中,您可能已经能够定义可重用的 type 值,但自(至少)draft-04 以来就没有
    猜你喜欢
    • 2016-10-08
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多