Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a property as null or a reference?

Tags:

jsonschema

I have a json document in which a part can be either null or a subobject, like this:

[{     "owner":null     }, {     "owner":{         "id":1     }    }] 

The question is if its possible to model this in json schema draft v4 using ref?

What I would like is something like this

{     "type":"object",     "properties":{         "owner":{             "type":["null", "object"],             "$ref":"#/definitions/id"         }     },     "definitions":{         "id":{             "type":"object",             "properties":{                 "id":{                     "type":"number"                 }             }         }      } } 
like image 806
viblo Avatar asked May 19 '14 04:05

viblo


2 Answers

What you've posted should work, if you remove the "type":"object" from the definition.

However, a neater, more explicit way to specify alternatives is to use oneOf. You can keep your "id" definition untouched, and just use:

    "owner":{         "oneOf": [             {"type": "null"},             {"$ref":"#/definitions/id"}         ]     } 
like image 117
cloudfeet Avatar answered Sep 30 '22 17:09

cloudfeet


nullable field will be supported in OpenApi (aka Swagger) Specification v3.0.0

So with this new spec your definition would look like:

"properties":{     "owner":{         "nullable": true,          ...     } }, 
like image 28
Naz Avatar answered Sep 30 '22 17:09

Naz