Catching the serializer.errors and format your own error container don't 
work?

Em sexta-feira, 25 de agosto de 2017 13:19:11 UTC-3, Serge Efremov escreveu:
>
> This is what I do. I have a Serializer-level validator that checks the 
> validity of a number of fields. If they a missing or incorrect I want to 
> display a specific error message. Which basically looks like this.
>
> {
>     'errors': {
>         '__all__': ['missing_security_fields'],
>     },
>     'errors:context': {
>         '__all__': {
>             'missing_security_fields': ['pow_1', 'pow_2', 'ticket'],
>         },
>     },
> }
>
> This is how it looks in (simplified) code:
>
> class SecurityFieldsValidator(object):
>     def __call__(self, value):
>         missing_fields = self.get_missing_fields(value)
>         if missing_fields:
>             errors = {
>                 '__all__': ['missing_security_fields'],
>             }
>             context = {
>                 '__all__': {
>                     'missing_security_fields': missing_fields,
>                 },
>             }
>             raise ValidationError({'errors': errors, 'errors:context': 
> context})
>
>
> But at some point in a call stack this structure stumbles upon 
> as_serializer_error() function (get_validation_error_detail() in older 
> versions) which wraps every item inside my dictionary in a list. What I 
> would like to do is to override this function so that it doesn't do it (at 
> least not for this specific format). But since it is not a method of 
> Serializer class (BTW why?) and there seems to be no setting that could 
> override this function, I don't see how I could change its behavior. One 
> workaround I see is when this function is done with its job, walk over my 
> dict and unwrap every item from its list, which looks like a complete waste 
> of CPU. Another option is to override run_validation() method of my 
> serializer which also doesn't seem nice because other than this problem, 
> I'm completely OK with how it works and I don't want to copy its code into 
> my project just to change a call of one function.
>
> Did anyone try to implement their own error format and how one could 
> circumvent this rather annoying and seemingly hardcoded behavior?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to