Types¶
-
class
schematics.types.base.
BaseType
(required=False, default=None, serialized_name=None, choices=None, validators=None, deserialize_from=None, serialize_when_none=None, messages=None)¶ 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 toself
)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 – An iterable 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)¶ Validate the field and return a clean 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 raisingStopValidation
instead ofValidationError
.
-
class
schematics.types.base.
BooleanType
(required=False, default=None, serialized_name=None, choices=None, validators=None, deserialize_from=None, serialize_when_none=None, messages=None)¶ A boolean field type. In addition to
True
andFalse
, coerces these values:- For
True
: “True”, “true”, “1” - For
False
: “False”, “false”, “0”
- For
-
class
schematics.types.base.
DateTimeType
(formats=None, serialized_format=None, **kwargs)¶ Defaults to converting to and from ISO8601 datetime values.
Parameters: - formats – A value or list of values suitable for
datetime.datetime.strptime
parsing. Default: (‘%Y-%m-%dT%H:%M:%S.%f’, ‘%Y-%m-%dT%H:%M:%S’) - serialized_format – The output format suitable for Python
strftime
. Default:'%Y-%m-%dT%H:%M:%S.%f'
- formats – A value or list of values suitable for
-
class
schematics.types.base.
DateType
(**kwargs)¶ Defaults to converting to and from ISO8601 date values.
-
class
schematics.types.base.
DecimalType
(min_value=None, max_value=None, **kwargs)¶ A fixed-point decimal number field.
-
class
schematics.types.base.
EmailType
(regex=None, max_length=None, min_length=None, **kwargs)¶ A field that validates input as an E-Mail-Address.
-
class
schematics.types.base.
FloatType
(*args, **kwargs)¶ A field that validates input as a Float
-
class
schematics.types.base.
GeoPointType
(required=False, default=None, serialized_name=None, choices=None, validators=None, deserialize_from=None, serialize_when_none=None, messages=None)¶ A list storing a latitude and longitude.
-
to_native
(value, context=None)¶ Make sure that a geo-value is of type (x, y)
-
-
class
schematics.types.base.
IPv4Type
(required=False, default=None, serialized_name=None, choices=None, validators=None, deserialize_from=None, serialize_when_none=None, messages=None)¶ A field that stores a valid IPv4 address
-
validate
(value)¶ Make sure the value is a IPv4 address: http://stackoverflow.com/questions/9948833/validate-ip-address-from-list
-
-
class
schematics.types.base.
IntType
(*args, **kwargs)¶ A field that validates input as an Integer
-
class
schematics.types.base.
LongType
(*args, **kwargs)¶ A field that validates input as a Long
-
class
schematics.types.base.
MD5Type
(required=False, default=None, serialized_name=None, choices=None, validators=None, deserialize_from=None, serialize_when_none=None, messages=None)¶ A field that validates input as resembling an MD5 hash.
-
class
schematics.types.base.
MultilingualStringType
(regex=None, max_length=None, min_length=None, default_locale=None, locale_regex='^[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
orcontext['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
andcontext['locale']
to return the best localized string.
-
-
class
schematics.types.base.
NumberType
(number_class, number_type, min_value=None, max_value=None, **kwargs)¶ A number field.
-
class
schematics.types.base.
SHA1Type
(required=False, default=None, serialized_name=None, choices=None, validators=None, deserialize_from=None, serialize_when_none=None, messages=None)¶ A field that validates input as resembling an SHA1 hash.
-
class
schematics.types.base.
StringType
(regex=None, max_length=None, min_length=None, **kwargs)¶ A unicode string field. Default minimum length is one. If you want to accept empty strings, init with
min_length
0.
-
class
schematics.types.base.
TypeMeta
¶ Meta class for BaseType. Merges MESSAGES dict and accumulates validator methods.
-
class
schematics.types.base.
URLType
(verify_exists=False, **kwargs)¶ A field that validates input as an URL.
If verify_exists=True is passed the validate function will make sure the URL makes a valid connection.
-
class
schematics.types.base.
UUIDType
(required=False, default=None, serialized_name=None, choices=None, validators=None, deserialize_from=None, serialize_when_none=None, messages=None)¶ A field that stores a valid UUID value.
Usage¶
To learn more about how Types are used, visit Using Types