[docs]defvalidates(field_name):"""Register a field validator. :param str field_name: Name of the field that the method validates. """returnset_hook(None,VALIDATES,field_name=field_name)

[docs]defvalidates_schema(fn=None,pass_many=False,pass_original=False,skip_on_field_errors=True,):"""Register a schema-level validator. By default, receives a single object at a time, regardless of whether ``many=True`` is passed to the `Schema`. If ``pass_many=True``, the raw data (which may be a collection) and the value for ``many`` is passed. If ``pass_original=True``, the original data (before unmarshalling) will be passed as an additional argument to the method. If ``skip_on_field_errors=True``, this validation method will be skipped whenever validation errors have been detected when validating fields. .. versionchanged:: 3.0.0b1 ``skip_on_field_errors`` defaults to `True`. """returnset_hook(fn,(VALIDATES_SCHEMA,pass_many),pass_original=pass_original,skip_on_field_errors=skip_on_field_errors,)

[docs]defpre_dump(fn=None,pass_many=False):"""Register a method to invoke before serializing an object. The method receives the object to be serialized and returns the processed object. By default, receives a single object at a time, regardless of whether ``many=True`` is passed to the `Schema`. If ``pass_many=True``, the raw data (which may be a collection) and the value for ``many`` is passed. """returnset_hook(fn,(PRE_DUMP,pass_many))

[docs]defpost_dump(fn=None,pass_many=False,pass_original=False):"""Register a method to invoke after serializing an object. The method receives the serialized object and returns the processed object. By default, receives a single object at a time, transparently handling the ``many`` argument passed to the Schema. If ``pass_many=True``, the raw data (which may be a collection) and the value for ``many`` is passed. If ``pass_original=True``, the original data (before serializing) will be passed as an additional argument to the method. """returnset_hook(fn,(POST_DUMP,pass_many),pass_original=pass_original)

[docs]defpre_load(fn=None,pass_many=False):"""Register a method to invoke before deserializing an object. The method receives the data to be deserialized and returns the processed data. By default, receives a single datum at a time, transparently handling the ``many`` argument passed to the Schema. If ``pass_many=True``, the raw data (which may be a collection) and the value for ``many`` is passed. """returnset_hook(fn,(PRE_LOAD,pass_many))

[docs]defpost_load(fn=None,pass_many=False,pass_original=False):"""Register a method to invoke after deserializing an object. The method receives the deserialized data and returns the processed data. By default, receives a single datum at a time, transparently handling the ``many`` argument passed to the Schema. If ``pass_many=True``, the raw data (which may be a collection) and the value for ``many`` is passed. If ``pass_original=True``, the original data (before deserializing) will be passed as an additional argument to the method. """returnset_hook(fn,(POST_LOAD,pass_many),pass_original=pass_original)

[docs]defset_hook(fn,key,**kwargs):"""Mark decorated function as a hook to be picked up later. .. note:: Currently only works with functions and instance methods. Class and static methods are not supported. :return: Decorated function if supplied, else this decorator with its args bound. """# Allow using this as either a decorator or a decorator factory.iffnisNone:returnfunctools.partial(set_hook,key=key,**kwargs)# Set a __marshmallow_hook__ attribute instead of wrapping in some class,# because I still want this to end up as a normal (unbound) method.try:hook_config=fn.__marshmallow_hook__exceptAttributeError:fn.__marshmallow_hook__=hook_config={}# Also save the kwargs for the tagged function on# __marshmallow_hook__, keyed by (<tag>, <pass_many>)hook_config[key]=kwargsreturnfn

Object serialization and deserialization, lightweight and fluffy.

Donate

If you find marshmallow useful, please consider supporting the team with a donation: