ModelTypeValidator
ModelTypeValidator(*, load_default=<marshmallow.missing>, missing=<marshmallow.missing>, dump_default=<marshmallow.missing>, default=<marshmallow.missing>, data_key=None, attribute=None, validate=None, required=False, allow_none=None, load_only=False, dump_only=False, error_messages=None, metadata=None, **additional_metadata)
A field able to validate the correct type of a value.
Attributes
context
The context dictionary for the parent Schema
.
default
default_error_messages
= {'null': 'Field may not be null.', 'required': 'Missing data for required field.', 'validator_failed': 'Invalid value.'}
Default error messages for various kinds of errors. The keys in this dictionary are passed to Field.make_error. The values are error messages passed to marshmallow.exceptions.ValidationError
.
missing
name
= None
parent
= None
root
= None
valid_types
= (,)
Methods
check_type
ModelTypeValidator.check_type(value, attr, data, **_)
Validates a value against the correct type of the field.
It calls _expected_types
to get a list of valid types.
Subclasses can do one of the following:
- Override the
valid_types
property with a tuple with the expected types for this field. - Override the
_expected_types
method to return a tuple of expected types for the field. - Change
check_type
completely to customize validation.
This method or the overrides must return the value
parameter untouched.
deserialize
ModelTypeValidator.deserialize(value, attr=None, data=None, **kwargs)
Deserialize value
.
Parameters
- value (
Any
) – The value to deserialize. - attr (
Optional
[str
]) – The attribute/key in data to deserialize. - data (
Optional
[Mapping
[str
,Any
]]) – The raw input data passed to Schema.load. - kwargs – Field-specific keyword arguments.
Raises
ValidationError – If an invalid value is passed or if a required value is missing.
fail
ModelTypeValidator.fail(key, **kwargs)
Helper method that raises a ValidationError with an error message from self.error_messages
.
Use make_error <marshmallow.fields.Field.make_error> instead.
get_value
ModelTypeValidator.get_value(obj, attr, accessor=None, default=<marshmallow.missing>)
Return the value for a given key from an object.
Parameters
- obj (object) – The object to get the value from.
- attr (str) – The attribute/key in obj to get the value from.
- accessor (callable) – A callable used to retrieve the value of attr from the object obj. Defaults to marshmallow.utils.get_value.
make_error
ModelTypeValidator.make_error(key, **kwargs)
Helper method to make a ValidationError with an error message from self.error_messages
.
Return type
ValidationError
make_error_serialize
ModelTypeValidator.make_error_serialize(key, **kwargs)
Helper method to return a ValidationError from _serialize.
This method wraps the result of make_error()
, adding contextual information in order to provide more informative information to users.
Parameters
- key (str) – error key index.
- **kwargs – additional arguments to
make_error()
.
Returns
an exception with the field name.
Return type
ValidationError
serialize
ModelTypeValidator.serialize(attr, obj, accessor=None, **kwargs)
Pulls the value for the given key from the object, applies the field’s formatting and returns the result.
Parameters
- attr (
str
) – The attribute/key to get from the object. - obj (
Any
) – The object to access the attribute/key from. - accessor (
Optional
[Callable
[[Any
,str
,Any
],Any
]]) – Function used to access values fromobj
. - kwargs – Field-specific keyword arguments.