def disallow_extra_fields(self, processed_data, original_data): # If the input data isn't a dict just short-circuit and let the Marshmallow unmarshaller # raise an error. if not isinstance(original_data, dict): return input_fields = original_data.keys() expected_fields = list(self.fields) + [ field.load_from for field in self.fields.values() if field.load_from is not None ] excluded_fields = self.exclude unsupported_fields = set(input_fields) - set(expected_fields) - set( excluded_fields) if len(unsupported_fields) > 0: raise ValidationError( message=messages.unsupported_fields(unsupported_fields))
def test_unexpected_field(self): data, errs = DisallowExtraFieldsSchema().load({"foo": "bar"}) self.assertEqual(errs, {"_schema": [messages.unsupported_fields(["foo"])]})
def test_unexpected_field(self): data, errs = DisallowExtraFieldsSchema().load({'foo': 'bar'}) self.assertEqual(errs, {'_schema': [messages.unsupported_fields(['foo'])]})