required=False for Nested Types


#1

Hello

How can I use Nested types that are optional.
I have the following types

class Location(types.Type):
    longitude = validators.Number()
    latitude = validators.Number()


class Product(types.Type):
    message = validators.String(max_length=100)
    location = Location

and I would like to have the location to be required=False.

I have tried something like

class Product(types.Type):
    message = validators.String(max_length=100)
    location = validators.Object(type=Location, required=False)

(but this does not work).

And I could not find documentation on this point.


#2

#apistar == 0.5
validator.Object do not accept apistar.type.Type, only properties dictionary.
A work around could be to get the properties dict from an apistar.type.Type, but i did not find how.


#3

An embedded Type is always expected to at least have the {} value, your best chance is:

class Product(types.Type):
    message = validators.String(max_length=100)
    location = Object(
          {
                  "longitude": validators.Number(),
                  "latitude": validators.Number()
          }, 
          allow_null=True
    )

#4

+1 on this, it would be great to be able to set allow_null on a nested subclass as this workaround makes it a bit hacky on more complicated schemas