Aleh Pliats
Aleh Pliats

Reputation: 51

json schema to validate multiple types and values for one parameter

Please help me with this: I try to write a json schema to validate the following object:

json object:

{"param":value}

Possible values: 'all', [array of any integers]

So it is a simple json object which contain one variable that could be string 'all', or array of any integers [].

I tried this but it doesn't work in json schema validator:

 { 
     "type": ["string","array"], 
     "items": { "oneOf":  [  
      "all", 
      { "type": "integer" } 
        ] 
     }
}

Thank you.

Upvotes: 5

Views: 4799

Answers (1)

NikolaS
NikolaS

Reputation: 111

For draft4 this schema should work

{
  "type": "object",
  "properties": {
    "param": {
      "oneOf": [
        {
          "enum": ["all"]
        },
        {
          "type": "array",
          "items": {"type": "integer"}
        }
      ]
    }
  },
  "additionalProperties": false,
  "required": ["param"]
}

Value of oneOf should be list of objects and keyword enum allows to compare with the value.

Upvotes: 5

Related Questions