62

I am trying to figure out how oneOf works by building a schema which validates two different object types. For example a person (firstname, lastname, sport) and vehicles (type, cost).

Here are some sample objects:

{"firstName":"John", "lastName":"Doe", "sport": "football"}

{"vehicle":"car", "price":20000}

The question is what have I done wrongly and how can I fix it. Here is the schema:

{
    "description": "schema validating people and vehicles", 
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "required": [ "oneOf" ],
    "properties": { "oneOf": [
        {
            "firstName": {"type": "string"}, 
            "lastName": {"type": "string"}, 
            "sport": {"type": "string"}
        }, 
        {
            "vehicle": {"type": "string"}, 
            "price":{"type": "integer"} 
        }
     ]
   }
}

When I try to validate it in this parser:

https://json-schema-validator.herokuapp.com/

I get the following error:

   [ {
  "level" : "fatal",
  "message" : "invalid JSON Schema, cannot continue\nSyntax errors:\n[ {\n  \"level\" : \"error\",\n  \"schema\" : {\n    \"loadingURI\" : \"#\",\n    \"pointer\" : \"/properties/oneOf\"\n  },\n  \"domain\" : \"syntax\",\n  \"message\" : \"JSON value is of type array, not a JSON Schema (expected an object)\",\n  \"found\" : \"array\"\n} ]",
  "info" : "other messages follow (if any)"
}, {
  "level" : "error",
  "schema" : {
    "loadingURI" : "#",
    "pointer" : "/properties/oneOf"
  },
  "domain" : "syntax",
  "message" : "JSON value is of type array, not a JSON Schema (expected an object)",
  "found" : "array"
} ]

2 Answers 2

85

Try this:

{
  "description": "schema validating people and vehicles",
  "type": "object",
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "sport": {
          "type": "string"
        }
      }
    },
    {
      "type": "object",
      "properties": {
        "vehicle": {
          "type": "string"
        },
        "price": {
          "type": "integer"
        }
      },
      "additionalProperties": false
    }
  ]
}
15
  • Does oneOf help in validating the request? I can't see any jackson annotation for oneOf during pojo conversion Commented May 3, 2017 at 8:50
  • 1
    @gursahib.singh.sahni, that is a jackson specific problem. It is not easy to translate oneOf semantics to some programming languages such as java. You need some construct like Disjoint sets, Discriminated Unions, Union Types... that other languages have. Commented May 3, 2017 at 9:47
  • @jruizaranguren there any library other than jackson that handles this case ? or else we would have to add custom validators in our codebase. Commented May 3, 2017 at 9:50
  • 4
    Is it required to have "type" : "object" on the root? It doesn't make much sense. Commented Dec 28, 2017 at 12:29
  • 1
    @jruizaranguren >"an empty object validates my schema, which is not probably what you want" - Well, your second schema has no required properties, so an empty object is valid against it. What if you added required properties? AFAIK, with oneOf, you create multiple object schema variants by adding the contents of a oneOf section to the surrounding schema node. All resulting schema variant must be valid schemas and for the object to be considered validated against the schema, it needs to be valid against exactly one variant.
    – Ark-kun
    Commented Jan 11, 2019 at 2:58
31

oneOf need to be used inside a schema to work.

Inside properties, it's like another property called "oneOf" without the effect you want.

3
  • 2
    I'm not saying your wrong, but can you provide a source for this information please? =] Commented Jan 16, 2015 at 10:44
  • 6
    Well it makes sense from deduction of what properties does, i.e. why would properties ever make a special case for keys named oneOf? Pretty much common sense it works this way now that I think of it. Thanks for the explanation.
    – davidtgq
    Commented Oct 19, 2017 at 0:17
  • 2
    This is what I found also. oneOf inside of properties does not work.
    – Moondoggy
    Commented Jun 9, 2021 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.