How to create array json schema for an array string which contains some fixed values and may have other additonal values

huangapple go评论85阅读模式
英文:

How to create array json schema for an array string which contains some fixed values and may have other additonal values

问题

所以我有这个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"
    ]
}

在这里,我希望attributes字段的值为"fk","uid",并允许其他字段值为"fk"和"uid"。
使用以下代码时,我在传递附加值时遇到错误:-

{
    "campaignType":"export",
    "clientid":107311,
    "select":{ 
                "segment":[30]
            },
    "attributes":["uid","fk", "att1"]
}

错误信息为:无法从JSON解组属性:无法从JSON解组项:json:无法将对象解组为类型为[]*jsonschema.Schema的Go值
我该如何解决这个问题?

英文:

So I have this json schema:-

{
    "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"
    ]
}

Here I want to have attributes field to have value "fk", "uid" fixed and must allow other field values with "fk" and "uid".
with this following code I am getting error while passing additonal values:-
{
"campaignType":"export",
"clientid":107311,
"select":{
"segment":[30]
},
"attributes":["uid","fk", "att1"]
}

error unmarshaling properties from json: error unmarshaling items from json: json: cannot unmarshal object into Go value of type []*jsonschema.Schema
how do I fix it?

答案1

得分: 2

你的模式中的contains的值必须是一个模式:

根据你的问题,也许将"attributes"模式更改为:

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

The value of contains in your schema must be a schema:

How to create array json schema for an array string which contains some fixed values and may have other additonal values

According to your question, maybe change the "attributes" schema to:

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

huangapple
  • 本文由 发表于 2022年6月17日 14:58:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/72655447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定