Nullable and optional fields

To require that some fields are always available, whitelist it to “required” list.

Warning: this is different than nullable concept below. Field can be required, but nullable. In contrast, field also can be optional, but if provided it may not be null. Use example below as pattern how to handle required field when its missing.

items = requests.get(‘http://items.com)
results = []
for item in items:
   result = {
      ‘price’: item.get(‘kaina’), # always present
      ‘name’: item.get(‘pavadinimas’), # always present
   }

   # schema does not allow NULL (None) value
   discount = item.get(‘discount’)
   if discount is not None: 
      result[‘discount’] = discount

Nullable fields

To allow “None” value for field, you must make it nullable. 

Use case example: in the apartments list table, sold apartments usually has no longer pricing information. For apartments which are available to purchase this data is still available.

Currently the approach to make nullable is to use text version of JSON schema editor, and change type of field, for example:

    "price": {
       "type": [
         "number",
         "null"
       ]
     },

Leave a Reply

Your email address will not be published. Required fields are marked *