How to collect all errors when using custom class validor

30 views
Skip to first unread message

Profs Stars

unread,
Dec 12, 2022, 7:12:40 AM12/12/22
to Django REST framework

Hello, i'm using custom class validator to validate serializer fields, and i would like to raise ValidationError for all fields, instead now i'm getting the validation error only for single fields. Before i've used method validations inside my serialzier and it worked as i need, but this is nto the case with class validators.

Here is how validators looks like

class TitleValidator:
   
    MIN_TITLE_LENGTH = 20
   
    def __call__(self, attrs: dict):
        title = attrs.get("title", "")
        if len(title) < self.MIN_TITLE_LENGTH:
            raise ValidationError(f"Min title length is {self.MIN_TITLE_LENGTH}")
        return title


class SlugsValidator:

    def __call__(self, attrs):
        slug = attrs.get("slug", "")
        if len(slug) < 10:
            raise ValidationError("Slug must be at least 10 characters long")
        return slug

Inside my serializer i'm using validators inside Meta class.
Reply all
Reply to author
Forward
0 new messages