Types

class IntType(*args, **kwargs)

A field that validates input as an Integer

class BaseType(required=False, default=Undefined, serialized_name=None, choices=None, validators=None, deserialize_from=None, export_level=None, serialize_when_none=None, messages=None, **kwargs)

A base class for Types in a Schematics model. Instances of this class may be added to subclasses of Model to define a model schema.

Validators that need to access variables on the instance can be defined be implementing methods whose names start with validate_ and accept one parameter (in addition to self)

Parameters:
  • required – Invalidate field when value is None or is not supplied. Default: False.
  • default – When no data is provided default to this value. May be a callable. Default: None.
  • serialized_name – The name of this field defaults to the class attribute used in the model. However if the field has another name in foreign data set this argument. Serialized data will use this value for the key name too.
  • deserialize_from – A name or list of named fields for which foreign data sets are searched to provide a value for the given field. This only effects inbound data.
  • choices – A list of valid choices. This is the last step of the validator chain.
  • validators – A list of callables. Each callable receives the value after it has been converted into a rich python type. Default: []
  • serialize_when_none – Dictates if the field should appear in the serialized data even if the value is None. Default: True
  • messages – Override the error messages with a dict. You can also do this by subclassing the Type and defining a MESSAGES dict attribute on the class. A metaclass will merge all the MESSAGES and override the resulting dict with instance level messages and assign to self.messages.
to_native(value, context=None)

Convert untrusted data to a richer Python construct.

to_primitive(value, context=None)

Convert internal data to a value safe to serialize.

validate(value, context=None)

Validate the field and return a converted value or raise a ValidationError with a list of errors raised by the validation chain. Stop the validation process from continuing through the validators by raising StopValidationError instead of ValidationError.

class BooleanType(required=False, default=Undefined, serialized_name=None, choices=None, validators=None, deserialize_from=None, export_level=None, serialize_when_none=None, messages=None, **kwargs)

A boolean field type. In addition to True and False, coerces these values:

  • For True: “True”, “true”, “1”
  • For False: “False”, “false”, “0”
class DateType(**kwargs)

Defaults to converting to and from ISO8601 date values.

class DecimalType(min_value=None, max_value=None, **kwargs)

A fixed-point decimal number field.

class StringType(regex=None, max_length=None, min_length=None, **kwargs)

A Unicode string field.

class UUIDType(required=False, default=Undefined, serialized_name=None, choices=None, validators=None, deserialize_from=None, export_level=None, serialize_when_none=None, messages=None, **kwargs)

A field that stores a valid UUID value.

class DateTimeType(formats=None, serialized_format=None, parser=None, tzd=u'allow', convert_tz=False, drop_tzinfo=False, **kwargs)

A field that holds a combined date and time value.

The built-in parser accepts input values conforming to the ISO 8601 format <YYYY>-<MM>-<DD>T<hh>:<mm>[:<ss.ssssss>][<z>]. A space may be substituted for the delimiter T. The time zone designator <z> may be either Z or ±<hh>[:][<mm>].

Values are stored as standard datetime.datetime instances with the time zone offset in the tzinfo component if available. Raw values that do not specify a time zone will be converted to naive datetime objects unless tzd='utc' is in effect.

Unix timestamps are also valid input values and will be converted to UTC datetimes.

Parameters:
  • formats – (Optional) A value or iterable of values suitable as datetime.datetime.strptime format strings, for example ('%Y-%m-%dT%H:%M:%S', '%Y-%m-%dT%H:%M:%S.%f'). If the parameter is present, strptime() will be used for parsing instead of the built-in parser.
  • serialized_format – The output format suitable for Python strftime. Default: '%Y-%m-%dT%H:%M:%S.%f%z'
  • parser – (Optional) An external function to use for parsing instead of the built-in parser. It should return a datetime.datetime instance.
  • tzd

    Sets the time zone policy. Default: 'allow'

    'require' Values must specify a time zone.
    'allow' Values both with and without a time zone designator are allowed.
    'utc' Like allow, but values with no time zone information are assumed to be in UTC.
    'reject' Values must not specify a time zone. This also prohibits timestamps.
  • convert_tz

    Indicates whether values with a time zone designator should be automatically converted to UTC. Default: False

    • True: Convert the datetime to UTC based on its time zone offset.
    • False: Don’t convert. Keep the original time and offset intact.
  • drop_tzinfo

    Can be set to automatically remove the tzinfo objects. This option should generally be used in conjunction with the convert_tz option unless you only care about local wall clock times. Default: False

    • True: Discard the tzinfo components and make naive datetime objects instead.
    • False: Preserve the tzinfo components if present.
class NumberType(number_class, number_type, min_value=None, max_value=None, strict=False, **kwargs)

A number field.

class FloatType(*args, **kwargs)

A field that validates input as a Float

class MultilingualStringType(regex=None, max_length=None, min_length=None, default_locale=None, locale_regex=u'^[a-z]{2}(:?_[A-Z]{2})?$', **kwargs)

A multilanguage string field, stored as a dict with {‘locale’: ‘localized_value’}.

Minimum and maximum lengths apply to each of the localized values.

At least one of default_locale or context.app_data['locale'] must be defined when calling .to_primitive.

to_native(value, context=None)

Make sure a MultilingualStringType value is a dict or None.

to_primitive(value, context=None)

Use a combination of default_locale and context.app_data['locale'] to return the best localized string.

class SHA1Type(regex=None, max_length=None, min_length=None, **kwargs)

A field that validates input as resembling an SHA1 hash.

class LongType(*args, **kwargs)

A field that validates input as a Long

class UTCDateTimeType(formats=None, parser=None, tzd=u'utc', convert_tz=True, drop_tzinfo=True, **kwargs)

A variant of DateTimeType that normalizes everything to UTC and stores values as naive datetime instances. By default sets tzd='utc', convert_tz=True, and drop_tzinfo=True. The standard export format always includes the UTC time zone designator "Z".

class GeoPointType(required=False, default=Undefined, serialized_name=None, choices=None, validators=None, deserialize_from=None, export_level=None, serialize_when_none=None, messages=None, **kwargs)

A list storing a latitude and longitude.

to_native(value, context=None)

Make sure that a geo-value is of type (x, y)

class MD5Type(regex=None, max_length=None, min_length=None, **kwargs)

A field that validates input as resembling an MD5 hash.

class TimestampType(formats=None, parser=None, drop_tzinfo=False, **kwargs)

A variant of DateTimeType that exports itself as a Unix timestamp instead of an ISO 8601 string. Always sets tzd='require' and convert_tz=True.

class TypeMeta

Meta class for BaseType. Merges MESSAGES dict and accumulates validator methods.

class ListType(field, min_size=None, max_size=None, **kwargs)

A field for storing a list of items, all of which must conform to the type specified by the field parameter.

Use it like this:

...
categories = ListType(StringType)
class ModelType(model_spec, **kwargs)

A field that can hold an instance of the specified model.

class DictType(field, coerce_key=None, **kwargs)

A field for storing a mapping of items, the values of which must conform to the type specified by the field parameter.

Use it like this:

...
categories = DictType(StringType)
class PolyModelType(model_spec, **kwargs)

A field that accepts an instance of any of the specified models.

find_model(data)

Finds the intended type by consulting potential classes or claim_function.

Usage

To learn more about how Types are used, visit Using Types