Add `Property` class and `ObjectType` class, prefering name-less types
This MR updates how json schema helper functions are implemented. Previously the "type" classes were functioning as "properties" - as each took a "name" argument and an optional "required" argument. However, this approach breaks down for arrays which wrap another type and have no name themselves.
Rather than have some types acting as properties and others as types, this update introduces a Property
class, which is a more appropriate and natural way to model properties which contain the (required) name
and (optional) required
attributes. The Property
class's constructor takes name
, type
, and an optional required
flag - with the type
input accepting either a type class of a type object.
Here is the new syntax example as listed at the top of helpers/typing.py
:
jsonschema = PropertiesList(
Property("id", IntegerType, required=True),
Property("name", StringType),
Property("tags", ArrayType(StringType)),
Property("ratio", NumberType),
Property("days_active", IntegerType),
Property("updated_on", DateTimeType),
Property("is_deleted", BooleanType),
Property(
"author",
ObjectType(
Property("id", StringType),
Property("name", StringType),
)
),
Property(
"groups",
ArrayType(
ObjectType(
Property("id", StringType),
Property("name", StringType),
)
)
),
).to_json()
Implementation note: in order to make this work properly, the type_dict
properties are now class properties, which means they work identically on the type classes as well as instances of those classes.
Deprecation notices:
- the
name
property of the type helpers should be considered deprecated and preference going forward should be for the new wrapped syntaxProperty(name, type)
. - Going forward: "properties" have names and required specifications, while "types" should be used to define schema. The exception to this is "objects", which themselves have named "properties". While a little more complex to describe, this more closely aligns with the JSON Schema spec.
-
ComplexType
references should be replaced withObjectType
. The latter does not take a name argument, and the reference toobject
more closely aligns with the native JSONSchema type ofobject
. -
ComplexType
and the optionalname
argument for type constructors will continue to work for a while longer, but that support will likely be removed at/before the first public beta.