Repository: marshmallow-code/marshmallow

Issue:
"Partial" deserialization support
For implementing `PATCH` handlers on REST endpoints, it would be useful to have a concept of partial deserialization.

This would mean ignoring missing required fields and default values for missing fields.

I know this sounds a bit weird, but it matches a standard CRUD endpoint fairly well - `POST` or `PUT` to that endpoint should use the full validation w/r/t required fields or defaults, but `PATCH` is intended to apply a partial update and only modify what was actually changed.

I can handle this in userspace by catching `ValidationError`s for missing fields, but I don't think I can do the same for ignoring default field values.

Here's the equivalent API in DRF: http://www.django-rest-framework.org/api-guide/serializers/#partial-updates. From my POV I think any `partial` arg (if you think it makes sense) would best be positioned as a named argument on `load`, though.

Discussion:

Patch:
diff --git a/docs/quickstart.rst b/docs/quickstart.rst
index 4e7dcdb5..959e915a 100644
--- a/docs/quickstart.rst
+++ b/docs/quickstart.rst
@@ -303,6 +303,22 @@ Dictionaries or lists are also accepted as the custom error message, in case you
     #  'age': ['Age is required.'],
     #  'city': {'message': 'City required', 'code': 400}}
 
+Partial Loading
++++++++++++++++
+
+When using the same schema in multiple places, you may only want to check required fields some of the time when deserializing. You can ignore missing fields entirely by setting ``partial=True``.
+
+.. code-block:: python
+    :emphasize-lines: 5,6
+
+    class UserSchema(Schema):
+        name = fields.String(required=True)
+        age = fields.Integer(required=True)
+
+    data, errors = UserSchema().load({'age': 42}, partial=True)
+    # OR UserSchema(partial=True).load({'age': 42})
+    data, errors  # => ({'age': 42}, {})
+
 Schema.validate
 +++++++++++++++
 
diff --git a/marshmallow/marshalling.py b/marshmallow/marshalling.py
index 5caca583..6c315c03 100644
--- a/marshmallow/marshalling.py
+++ b/marshmallow/marshalling.py
@@ -208,7 +208,7 @@ class Unmarshaller(ErrorStore):
                 data=output
             )
 
-    def deserialize(self, data, fields_dict, many=False,
+    def deserialize(self, data, fields_dict, many=False, partial=False,
             dict_class=dict, index_errors=True, index=None):
         """Deserialize ``data`` based on the schema defined by ``fields_dict``.
 
@@ -216,6 +216,7 @@ class Unmarshaller(ErrorStore):
         :param dict fields_dict: Mapping of field names to :class:`Field` objects.
         :param bool many: Set to `True` if ``data`` should be deserialized as
             a collection.
+        :param bool partial: If `True`, ignore missing fields.
         :param type dict_class: Dictionary class used to construct the output.
         :param bool index_errors: Whether to store the index of invalid items in
             ``self.errors`` when ``many=True``.
@@ -229,7 +230,7 @@ class Unmarshaller(ErrorStore):
         if many and data is not None:
             self._pending = True
             ret = [self.deserialize(d, fields_dict, many=False,
-                        dict_class=dict_class,
+                        partial=partial, dict_class=dict_class,
                         index=idx, index_errors=index_errors)
                     for idx, d in enumerate(data)]
 
@@ -265,6 +266,8 @@ class Unmarshaller(ErrorStore):
                     field_name = field_obj.load_from
                     raw_value = data.get(field_obj.load_from, missing)
                 if raw_value is missing:
+                    if partial:
+                        continue
                     _miss = field_obj.missing
                     raw_value = _miss() if callable(_miss) else _miss
                 if raw_value is missing and not field_obj.required:
diff --git a/marshmallow/schema.py b/marshmallow/schema.py
index 6275bd0a..6b38c984 100644
--- a/marshmallow/schema.py
+++ b/marshmallow/schema.py
@@ -12,7 +12,7 @@ import types
 import uuid
 import warnings
 from collections import namedtuple
-from functools import partial
+import functools
 
 from marshmallow import base, fields, utils, class_registry, marshalling
 from marshmallow.compat import (with_metaclass, iteritems, text_type,
@@ -251,6 +251,7 @@ class BaseSchema(base.SchemaABC):
     :param tuple load_only: A list or tuple of fields to skip during serialization
     :param tuple dump_only: A list or tuple of fields to skip during
         deserialization, read-only fields
+    :param bool partial: If `True`, ignore missing fields when deserializing.
 
     .. versionchanged:: 2.0.0
         `__validators__`, `__preprocessors__`, and `__data_handlers__` are removed in favor of
@@ -320,7 +321,8 @@ class BaseSchema(base.SchemaABC):
         pass
 
     def __init__(self, extra=None, only=(), exclude=(), prefix='', strict=False,
-                 many=False, context=None, load_only=(), dump_only=()):
+                 many=False, context=None, load_only=(), dump_only=(),
+                 partial=False):
         # copy declared fields from metaclass
         self.declared_fields = copy.deepcopy(self._declared_fields)
         self.many = many
@@ -331,6 +333,7 @@ class BaseSchema(base.SchemaABC):
         self.ordered = self.opts.ordered
         self.load_only = set(load_only) or set(self.opts.load_only)
         self.dump_only = set(dump_only) or set(self.opts.dump_only)
+        self.partial = partial
         #: Dictionary mapping field_names -> :class:`Field` objects
         self.fields = self.dict_class()
         #: Callable marshalling object
@@ -519,19 +522,21 @@ class BaseSchema(base.SchemaABC):
         ret = self.opts.json_module.dumps(deserialized, *args, **kwargs)
         return MarshalResult(ret, errors)
 
-    def load(self, data, many=None):
+    def load(self, data, many=None, partial=None):
         """Deserialize a data structure to an object defined by this Schema's
         fields and :meth:`make_object`.
 
         :param dict data: The data to deserialize.
         :param bool many: Whether to deserialize `data` as a collection. If `None`, the
             value for `self.many` is used.
+        :param bool partial: Whether to ignore missing fields. If `None`, the
+            value for `self.partial` is used.
         :return: A tuple of the form (``data``, ``errors``)
         :rtype: `UnmarshalResult`, a `collections.namedtuple`
 
         .. versionadded:: 1.0.0
         """
-        result, errors = self._do_load(data, many, postprocess=True)
+        result, errors = self._do_load(data, many, partial, postprocess=True)
         return UnmarshalResult(data=result, errors=errors)
 
     def loads(self, json_data, many=None, *args, **kwargs):
@@ -540,13 +545,20 @@ class BaseSchema(base.SchemaABC):
         :param str json_data: A JSON string of the data to deserialize.
         :param bool many: Whether to deserialize `obj` as a collection. If `None`, the
             value for `self.many` is used.
+        :param bool partial: Whether to ignore missing fields. If `None`, the
+            value for `self.partial` is used.
         :return: A tuple of the form (``data``, ``errors``)
         :rtype: `UnmarshalResult`, a `collections.namedtuple`
 
         .. versionadded:: 1.0.0
         """
+        # TODO: This avoids breaking backward compatibility if people were
+        # passing in positional args after `many` for use by `json.loads`, but
+        # ideally we shouldn't have to do this.
+        partial = kwargs.pop('partial', None)
+
         data = self.opts.json_module.loads(json_data, *args, **kwargs)
-        return self.load(data, many=many)
+        return self.load(data, many=many, partial=partial)
 
     def validate(self, data, many=None):
         """Validate `data` against the schema, returning a dictionary of
@@ -565,17 +577,20 @@ class BaseSchema(base.SchemaABC):
 
     ##### Private Helpers #####
 
-    def _do_load(self, data, many=None, postprocess=True):
+    def _do_load(self, data, many=None, partial=None, postprocess=True):
         """Deserialize `data`, returning the deserialized result and a dictonary of
         validation errors.
 
         :param data: The data to deserialize.
         :param bool many: Whether to deserialize `data` as a collection. If `None`, the
             value for `self.many` is used.
+        :param bool partial: Whether to ignore missing fields. If `None`, the
+            value for `self.partial` is used.
         :param bool postprocess: Whether to run post_load methods..
         :return: A tuple of the form (`data`, `errors`)
         """
         many = self.many if many is None else bool(many)
+        partial = self.partial if partial is None else bool(partial)
 
         processed_data = self._invoke_load_processors(PRE_LOAD, data, many, original_data=data)
 
@@ -584,6 +599,7 @@ class BaseSchema(base.SchemaABC):
                 processed_data,
                 self.fields,
                 many=many,
+                partial=partial,
                 dict_class=self.dict_class,
                 index_errors=self.opts.index_errors,
             )
@@ -780,7 +796,7 @@ class BaseSchema(base.SchemaABC):
             validator_kwargs = validator.__marshmallow_kwargs__[(VALIDATES_SCHEMA, pass_many)]
             pass_original = validator_kwargs.get('pass_original', False)
             if pass_many:
-                validator = partial(validator, many=many)
+                validator = functools.partial(validator, many=many)
             if many:
                 for idx, item in enumerate(data):
                     try:


Test patch:
diff --git a/tests/test_deserialization.py b/tests/test_deserialization.py
index 3b26dc5c..f886f5e6 100644
--- a/tests/test_deserialization.py
+++ b/tests/test_deserialization.py
@@ -1089,6 +1089,28 @@ class TestSchemaDeserialization:
         assert len(errors['foo']) == 1
         assert 'Missing data for required field.' in errors['foo']
 
+    @pytest.mark.parametrize('partial_schema',
+    [
+        True,
+        False
+    ])
+    def test_partial_deserialization(self, partial_schema):
+        class MySchema(Schema):
+            foo = fields.Field(required=True)
+            bar = fields.Field(required=True)
+
+        schema_args = {}
+        load_args = {}
+        if partial_schema:
+            schema_args['partial'] = True
+        else:
+            load_args['partial'] = True
+        data, errors = MySchema(**schema_args).load({'foo': 3}, **load_args)
+
+        assert data['foo'] == 3
+        assert 'bar' not in data
+        assert not errors
+
 validators_gen = (func for func in [lambda x: x <= 24, lambda x: 18 <= x])
 
 validators_gen_float = (func for func in