How to allow None (null) for fields in JSON Schema?

85,066 views
Skip to first unread message

Hooman Korasani

unread,
Jun 7, 2013, 12:03:33 PM6/7/13
to jsons...@googlegroups.com
Hey guys,


I have created a schema like this:

  schema = {         
          "type" : "object",
          "properties" : {
                          "email" : {"type" : "string"},
                          "active" : {"type" : "boolean"},
                          "password_hash" : {"type" : "string"},
                          "fb_id" : {"type" : "string"},                         
                          },
          "required": ["email", "password_hash"]
          }

however if my fb_id is set to None, the validation fails.
This is strange because I haven't set this field to be required. So why is it failing?

Many Thanks,
Hooman

Chase Sterling

unread,
Jun 7, 2013, 12:09:52 PM6/7/13
to Hooman Korasani, jsons...@googlegroups.com
Hey Hooman. This is because you require fb_id be a string, and null is not a string. If you also want to allow null values for fb_id, you can set its schema to {"type": ["string", "null"]}


--
You received this message because you are subscribed to the Google Groups "jsonschema - An implementation of JSON Schema for Python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema+...@googlegroups.com.
To post to this group, send email to jsons...@googlegroups.com.
 
 

houmie

unread,
Jun 7, 2013, 12:23:28 PM6/7/13
to Chase Sterling, jsons...@googlegroups.com
Hi Chase,

Thank you very much, it worked. :)

Now how about datetime?


schema = {         
          "type" : "object",
          "properties" : {
                          "record_date" : {"type" : "datetime"},
                          "rating" : {"type" : "number"},
                          "notes" : {"type" : "string"},                                                
                          },
          "required": ["record_date", "rating"]
          }

Is this a custom type that I have to define myself?  My date is always defined in ISO, e.g. 2010/12/17

Many Thanks,
Hooman

Julian Berman

unread,
Jun 7, 2013, 12:59:52 PM6/7/13
to jsons...@googlegroups.com, Chase Sterling
datetime (and uri, and the rest) are not types, they're formats for things that are type string. You'd use the `format` validator keyword to specify them.

Julian

Amey Ghadigaonkar

unread,
Dec 29, 2014, 2:53:06 PM12/29/14
to jsons...@googlegroups.com, chase.s...@gmail.com
Is this solution valid for the 4th draft of JSON schema? Or I need to change this somehow?

If I try the following, I still get an error:
{
"anyOf" : [{"type": "string"}, {"type": null}]

Pelle Almquist

unread,
Sep 25, 2015, 4:51:39 AM9/25/15
to jsonschema - An implementation of JSON Schema for Python, chase.s...@gmail.com
Hi Amey,
this is probably a little late but for consistency. I think you have a typo in your last schema. Instead of just `null` you should enclose it in quotes like `"null"`.

BR Pelle

Azwa Osman

unread,
Mar 9, 2017, 7:04:25 AM3/9/17
to jsonschema - An implementation of JSON Schema for Python
 "ID_NUMBER": {
      "type": ["string","null"],
      "title": "IC Number"
    },

i got error Unsupported field schema { "type": [ "string", "null" ], "title": "IC Number" }.

Hassan Faghihi

unread,
Sep 7, 2017, 5:39:31 AM9/7/17
to jsonschema - An implementation of JSON Schema for Python
It was long since i forgot the issue i left in my system, just tried fixing it and with little help from you, and ' http://json-schema.org/draft-04/schema# ', i found it like this:

""AgencyTelephone"": {
   ""type"":[""object"", ""null""],
   ""oneOf"":[
       {
           ""$ref"": ""#/definitions/cityPhone""
       }, {
           ""type"":""null""
       }
   ]
},

Mohammad Faizan

unread,
Mar 6, 2018, 3:37:24 AM3/6/18
to jsonschema - An implementation of JSON Schema for Python
Thanks a lot man! helped me solve many bugs. Cheers

Dan Davis

unread,
Dec 4, 2018, 12:48:22 PM12/4/18
to jsonschema - An implementation of JSON Schema for Python
I have a related but slightly tangential solution, which has taken me a while to work out.   I am not 100% in control of the swagger schema generation for my API, and in any case, I care more that the API's swagger spec complies with 2.0 than that my validation of the API responses is easy.

So, I don't wish to use "oneOf" or anything like that - I simply want to customize the schema validation.   The documentation is not too clear about how to do this, but this seems to work:

validator3 = Draft3Validator(swagger_spec, types={'string': (str, type(None))})
          
And, since keywords are passed on to jsonschema.validate(), we can also do:

          jsonschema.validate(instance, schema, types={'string': (str, type(None))})

I prefer the former, especially as I make it a method on my unittest class, e.g. self.should_match_schema(data, path)

Dan Davis

unread,
Dec 4, 2018, 3:20:09 PM12/4/18
to jsonschema - An implementation of JSON Schema for Python
Looks like it might be worthwhile to support this in the schema using "x-nullable", but that takes me away from JSON schema towards swagger.
Reply all
Reply to author
Forward
0 new messages