登录
首页 >  Golang >  Go问答

创建数组 JSON 模式来表示包含固定值和可能附加值的数组字符串

来源:stackoverflow

时间:2024-02-24 17:15:26 268浏览 收藏

学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《创建数组 JSON 模式来表示包含固定值和可能附加值的数组字符串》,以下内容主要包含等知识点,如果你正在学习或准备学习Golang,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!

问题内容

所以我有这个 json 架构:-

{
    "type": "object",
    "properties": {
        "campaignType": {
            "type": "string",
            "enum": [
                "export"
            ]
        },
        "clientid": {
            "type": "integer",
            "minimum": 1
        },
        "select": {
            "type": "object",
            "minProperties": 1,
            "anyOf": [
                {
                    "required": [
                        "list"
                    ]
                },
                {
                    "required": [
                        "segment"
                    ]
                }
            ],
            "properties": {
                "list": {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    }
                },
                "segment": {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    }
                }
            }
        },
        "attributes": {
            "type": "array",
            "minItems": 2,
            "items": { 
                "type": "string",
                "contains": ["fk", "uid"]
            }
        }
    },
    "required": [
        "campaignType",
        "clientid",
        "select",
        "attributes"
    ]
}

在这里,我希望属性字段的值“fk”、“uid”固定,并且必须允许其他字段值具有“fk”和“uid”。 使用以下代码,我在传递附加值时遇到错误:- { "campaigntype":"导出", “客户端id”:107311, “选择”:{ “段”:[30] }, “属性”:[“uid”,“fk”,“att1”] }

从 json 解组属性时出错:从 json 解组项目时出错:json:无法将对象解组为 []*jsonschema.schema 类型的 go 值 我该如何修复它?


正确答案


架构中 contains 的值必须是一个架构:

根据您的问题,也许将“属性”架构更改为:

"attributes": {
  "type": "array",
  "minItems": 2,
  "items": [ { "const": "fk" }, { "const": "uid" } ],
  "additionalItems": {
    "type": "string"
  }
}

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>