Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions cfg_loader/schema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

import marshmallow
from marshmallow import Schema, post_load
from marshmallow import Schema, post_load, INCLUDE

from ..exceptions import ValidationError
from ..fields import UnwrapNested
Expand All @@ -36,7 +36,7 @@ def __init__(self, *args, substitution_mapping=None, **kwargs):
substitution_template=self._substitution_template)
super().__init__(*args, **kwargs)

def load(self, data, many=None, partial=None):
def load(self, data, many=None, partial=None, unknown=None):
"""Deserialize a data structure to an object defined by this Schema’s fields

:param data: Data object to load from
Expand All @@ -45,6 +45,8 @@ def load(self, data, many=None, partial=None):
:type many: bool
:param partial: whether to ignore missing fields
:type partial: bool | tuple
:param unknown: how to handle unknown fields
:type unknown: None | EXCLUDE | INCLUDE | RAISE
:returns: Deserialized data
:type return: dict
"""
Expand All @@ -53,28 +55,17 @@ def load(self, data, many=None, partial=None):
data = self.interpolator.interpolate_recursive(data)

try:
return super().load(data, many, partial)
return super().load(data, many, partial, unknown)
except marshmallow.exceptions.ValidationError as e:
raise ValidationError(e.normalized_messages())


class ExtraFieldsSchema(Schema):
"""Schema class that preserves fields provided in input data but that were omitted in schema fields"""

@post_load(pass_original=True)
def add_extra_fields(self, data, original_data):
"""Add field from input data that were not listed as a schema fields

:param data: Data to complete
:type data: dict
:param extra_data: Extra data to insert
:type extra_data: dict
"""
extra_fields = set(original_data) - set(value.data_key or field for field, value in self.fields.items())
for field in extra_fields:
data[field] = original_data[field]

return data
class Meta:
# https://marshmallow.readthedocs.io/en/3.0/quickstart.html#handling-unknown-fields
unknown = INCLUDE


class UnwrapNestedSchema(Schema):
Expand Down