The following is a valid JSON schema according to http://jsonlint.com/ and http://jsonschemalint.com/draft4/#.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["results"],
"additionalProperties": false,
"properties": {
"results": {
"type": "string",
"oneOf": [
{ "result": "1" },
{ "result": "2" },
{ "result": "3" },
{ "result": "4" }
]
}
}
}
The following JSON reports an error (results is the wrong type
) when validated against the above schema:
{
"results" : {
"result": "1"
}
}
Can anyone suggest how I might resolve this error?
It looks like what you want in this case is
enum
rather thanoneOf
. Here is how you would define your schema.But, the question was how to use
oneOf
properly. TheoneOf
keyword should be an array of schemas, not values as you have used in your example. One and only one of the schemas inoneOf
must validate against the data for theoneOf
clause to validate. I have to modify your example a little to illustrate how to useoneOf
. This example allowsresult
to be a string or an integer.results
is a type ofobject
as per you schema definition but you mentioned type asString
. If I change the type asobject
, It just works fine.