Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Best/correct practice for object-level validation errors?#1135

Answeredbysliverc
arttuperala asked this question inQ&A
Discussion options

Is there a best/correct practice for implementing object-level validation and the ensuing validation errors?

Django REST framework documentation suggests doing object-level validation inSerializer.validate()[1].

To do any other validation that requires access to multiple fields, add a method called.validate() to yourSerializer subclass. This method takes a single argument, which is a dictionary of field values. It should raise aserializers.ValidationError if necessary, or just return the validated values.

So if I wanted to evaluate two fields and validate that only one of these fields is given data, I might implement my serializer something like this:

fromrest_framework_json_apiimportserializersclassProductSerializer(serializers.ModelSerializer):defvalidate(self,data):ifdata.get("best_before_date")anddata.get("expiry_date"):raiseserializers.ValidationError("Specify either a best before date or an expiry date."            )returndata

However, using this serializer with django-rest-framework-json-api results in an error response that seems incorrect.

{"errors": [        {"detail":"Specify either a best before date or an expiry date.","status":"400","source": {"pointer":"/data/attributes/non_field_errors"            },"code":"invalid"        }    ]}

As far as I've understood, JSON:API error objects should have the source.pointer value as"/data/attributes/<attribute>" when an error is caused by a specific attribute, and"/data" in other cases[2].

I thought about raising this as a bug report initially, but I'm not sure if this is actually a bug or if I'm just approaching this issue incorrectly. I also madea complete minimal example with unit tests of how I've implemented this.

[1]https://www.django-rest-framework.org/api-guide/serializers/#object-level-validation
[2]https://jsonapi.org/format/#error-objects

You must be logged in to vote

Thanks for your very detailed request. The way how you do validation on several fields is certainly correct. It seems you have found an issue with non field errors which DRF handles in a special manner but DJA is not aware of it.

As a workaround you can usecustom exceptions and set the pointer manually.

Please file a bug where you outline your issue. Feel free to work on a PR if you desire.

Replies: 1 comment

Comment options

Thanks for your very detailed request. The way how you do validation on several fields is certainly correct. It seems you have found an issue with non field errors which DRF handles in a special manner but DJA is not aware of it.

As a workaround you can usecustom exceptions and set the pointer manually.

Please file a bug where you outline your issue. Feel free to work on a PR if you desire.

You must be logged in to vote
0 replies
Answer selected byarttuperala
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@arttuperala@sliverc

[8]ページ先頭

©2009-2025 Movatter.jp