Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json Schema example for oneOf objects

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" } ] 
like image 853
Stanimirovv Avatar asked Jul 29 '14 11:07

Stanimirovv


People also ask

What is oneOf in JSON schema?

Here oneOf is a keyword construct in the JSON Schema, which is used to provide an array of criteria where, if exactly one of them is valid, the whole block is valid. As per the exampe above, objects having ( "email" AND "password" ) OR ( "username" AND "password" ) attributes are considered valid.

What does allOf mean in JSON schema?

By the definition of this keyword, it meant that the instance had to be valid against the current schema and all schemas specified in extends ; basically, draft v4's allOf is draft v3's extends .

What is JSON schema additionalProperties?

The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.

What is the difference between oneOf and anyOf?

oneOf matches exactly one subschema, and anyOf can match one or more subschemas.


1 Answers

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      } ] } 
like image 169
jruizaranguren Avatar answered Sep 30 '22 07:09

jruizaranguren