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

Issue

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?

Solution

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

keyword contains error message

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

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

Answered By – Clemens

Answer Checked By – Cary Denson (GoLangFix Admin)

Leave a Reply

Your email address will not be published.