You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current problem that I'm trying to solve is to ensure that I can drop errors for invalid data sent to any of the nested Serializers. One example is one in which the nested Serializer expects a dictionary but a list is sent to it instead.
Example:
fromdrf_braces.serializers.enforce_validation_serializerimportcreate_enforce_validation_serializer, EnforceValidationFieldMixinfromrest_frameworkimportfields, serializersfromrest_framework.fieldsimportemptyfromrest_framework.serializersimportSerializerclassDropInvalidFieldsMixin(EnforceValidationFieldMixin):
defrun_validation(self, data=empty):
self.parent.must_validate_fields= []
try:
returnsuper(EnforceValidationFieldMixin, self).run_validation(data)
exceptserializers.ValidationError:
field_name=getattr(self, "field_name")
raisefields.SkipField(
'This field "{}" is being skipped as per enforce validation logic.'''.format(field_name)
)
classPlayerSerializer(Serializer):
name=fields.CharField()
classPlayersSerializer(Serializer):
player=PlayerSerializer(many=True)
classGameSerializer(Serializer):
name=fields.CharField()
country_of_origin=fields.CharField(required=False)
players=PlayersSerializer(required=False)
serializer_class=create_enforce_validation_serializer(GameSerializer, strict_mode_by_default=False)
serializer=serializer_class(data={"name": "Chop the Money", "country_of_origin": "Nigeria", "players": [{"name": "Olusegun Obasanjo"}, {"name": "Sani Abacha"}] })
>>>serializer.is_valid()
>>> {'players': {u'non_field_errors': [ErrorDetail(string=u'Invalid data. Expected a dictionary, but got list.', code=u'invalid')]}}
I expect that the GameSerializer be valid and the incorrect data in the PlayersSerializer be dropped especially since self.parent.must_validate_fields is set to an empty list in DropInvalidFieldsMixin.run_validation.
The text was updated successfully, but these errors were encountered:
@shosca: I can't modify the definition of GameSerializer, since another service expects it to be in that format. I'm piggybacking on that service, which does 99% of what I want. 😄
Even though
EnforceValidationFieldMixin
(https://github.com/dealertrack/django-rest-framework-braces/blob/master/drf_braces/serializers/enforce_validation_serializer.py#L10) is added to the MRO of the Serializer and all nested Serializers, theSerializer's.run_validation
method still does not run theEnforceValidationFieldMixin.run_validation
method or those of any subclasses ofEnforceValidationFieldMixin
that might overriderun_validation
.The current problem that I'm trying to solve is to ensure that I can drop errors for invalid data sent to any of the nested
Serializers
. One example is one in which the nestedSerializer
expects a dictionary but a list is sent to it instead.Example:
I expect that the
GameSerializer
be valid and the incorrect data in thePlayersSerializer
be dropped especially sinceself.parent.must_validate_fields
is set to an empty list inDropInvalidFieldsMixin.run_validation
.The text was updated successfully, but these errors were encountered: