Skip to main content

Extension for displaying serializer validation errors in Django Rest Framework

Project description

Extension for Django REST framework error display

Overview

This package extends default error JSON body providing configurable error codes and more consumable response structure.

It turns default JSON body of HTTP 400 response, which look like this:

{
    "name": ["This field is required."],
    "password": ["This field may not be blank."]
}

into

{
    "name" : [
        {
            "code" : 2002,
            "detail" : "This field is required."
        }
    ],
    "password" : [
        {
            "code" : 2031,
            "detail" : "This field may not be blank."
        }
    ]
}

Library handles all Django REST framework built-in serializer validation.

Requirements

  • Python (2.7, 3.5, 3.6, 3.7)

  • Django (<2.0, >=1.8)

  • Django REST framework (<3.9, >=3.5)

Installation

By running installation script

$ python setup.py install

Or using pip

$ pip install drf-friendly-errors-mod

Usage

Simply add a ErrorMessagesMixin to your serializer or model serializer class

from rest_framework_friendly_errors.mixins import ErrorMessagesMixin

class MySerializer(ErrorMessagesMixin, ModelSerializer):

If you want to change default library settings and provide your own set of error codes just add following in your settings.py

FRIENDLY_ERRORS = {
    "FIELD_ERRORS": {
        'CharField': {'required': 10, 'null':11, 'blank': 12, 'max_length': 13, 'min_length': 14}
    },
    "VALIDATOR_ERRORS": {
        'UniqueValidator': 50
    },
    "EXCEPTION_DICT": {
        'PermissionDenied': 100
    }
}

Custom serializer validation

If you need custom field validation or validation for whole serializer register your validation in serializer class

class PostSerializer(ErrorMessagesMixin,
                     serializers.ModelSerializer):
    class Meta:
        model = Post

    def validate_title(self, value):
        if value[0] != value[0].upper():
            raise ValidationError('First letter must be an uppercase')
        return value

    def validate(self, attrs):
        category = attrs.get('category)
        title = attrs.get('title')
        if category and category not in title:
            raise ValidationError('Title has to include category')
        return attrs

    FIELD_VALIDATION_ERRORS = {'validate_title': 5000} # register your own validation method and assign it to error code
    NON_FIELD_ERRORS = {'Title has to include category': 8000} # register non field error messages and assign it to error code

If you want to raise field error in validate method use register_error method provided by a mixin

class PostSerializer(ErrorMessagesMixin,
                     serializers.ModelSerializer):
    class Meta:
        model = Post

    def validate(self, attrs):
        category = attrs.get('category')
        title = attrs.get('title')
        if category and category not in title:
            self.register_error(error_message='Title has to include category',
                                error_code=8000,
                                field_name='title')
        return attrs

Default error codes

Following conventions were used:

1xxx - Are reserved for non field errors

2xxx - Are reserved for field errors

3xxx - Are reserved for validator errors

4xxx - Are reserved for other errors not related to serializer validation

Default field error codes

Field is required

  • 2001: BooleanField, NullBooleanField

  • 2002: CharField, EmailField, PasswordField, RegexField, SlugField, URLField, UUIDField, FilePathField, IPAddressField

  • 2003: IntegerField, FloatField, DecimalField

  • 2004: ChoiceField, MultipleChoiceField

  • 2005: FileField, ImageField

  • 2006: ListField, DictField, JSONField

  • 2007: StringRequiredField, PrimaryKeyRelatedField, HyperlinkedRelatedField, SlugRelatedField, HyperlinkedIdentityField, ManyRelatedField

  • 2008: ReadOnlyField, HiddenField, ModelField, SerializerMethodField

Field data is invalid (invalid regex, string instead of number, date, etc.)

  • 2011: BooleanField, NullBooleanField

  • 2012: CharField, EmailField, PasswordField, RegexField, SlugField, URLField, UUIDField, IPAddressField

  • 2013: IntegerField, FloatField, DecimalField

  • 2014: FileField, ImageField

  • 2015: DateTimeField, DateField, TimeField, DurationField

Field data cannot be null

  • 2021: BooleanField, NullBooleanField

  • 2022: CharField, EmailField, PasswordField, RegexField, SlugField, URLField, UUIDField, FilePathField, IPAddressField

  • 2023: IntegerField, FloatField, DecimalField

  • 2024: ChoiceField, MultipleChoiceField

  • 2025: FileField, ImageField

  • 2026: ListField, DictField, JSONField

  • 2027: StringRequiredField, PrimaryKeyRelatedField, HyperlinkedRelatedField, SlugRelatedField, HyperlinkedIdentityField, ManyRelatedField

  • 2028: ReadOnlyField, HiddenField, ModelField, SerializerMethodField

Field data cannot be blank

  • 2031: CharField, EmailField, PasswordField, RegexField, SlugField, URLField, UUIDField, IPAddressField

Field data is too long string

  • 2041: CharField, EmailField, PasswordField, RegexField, SlugField, URLField, UUIDField, IPAddressField

  • 2042: IntegerField, FloatField, DecimalField

  • 2043: FileField, ImageField

Field data is too short string

  • 2051: CharField, EmailField, PasswordField, RegexField, SlugField, URLField, UUIDField, IPAddressField

Field data is too big number

  • 2061: IntegerField, FloatField, DecimalField

Field data is too small number

  • 2071: IntegerField, FloatField, DecimalField

Field data do not match any value from available choices

  • 2081: ChoiceField, MultipleChoiceField

  • 2082: FilePathField

  • 2083: ManyRelatedField

Field is empty

  • 2091: FileField, ImageField

  • 2092: MultipleChoiceField

  • 2093: ManyRelatedField

File has no name

  • 2101: FileField, ImageField

File is an invalid image

  • 2111: ImageField

Field is not a list

  • 2121: MultipleChoiceField

  • 2122: ListField

  • 2123: ManyRelatedField

Field is not a dict

  • 2131: DictField

Field is not a json

  • 2141: JSONField

Field does not exist (invalid hyperlink, primary key, etc.)

  • 2151: PrimaryKeyRelatedField, HyperlinkedRelatedField, SlugRelatedField, HyperlinkedIdentityField

Incorrect type for relation key

  • 2161: PrimaryKeyRelatedField, HyperlinkedRelatedField, SlugRelatedField, HyperlinkedIdentityField

Couldn’t match url or name to a view

  • 2171: HyperlinkedRelatedField, HyperlinkedIdentityField

Expected a DateTime, got Date

  • 2181: DateTimeField

Excpected a Date, got DateTime

  • 2191: DateField

Too many digits for defined Decimal

  • 2201: DecimalField

Too many whole digits for defined Decimal

  • 2211: DecimalField

Too many decimal digits for defined Decimal

  • 2221: DecimalField

Default built-in validators error codes

  • UniqueValidator: 3001

  • UniqueTogetherValidator: 3003

  • UniqueForDateValidator: 3004

  • UniqueForMonthValidator: 3004

  • UniqueForYearValidator: 3005

  • RegexValidator: 3006

  • EmailValidator: 3007

  • URLValidator: 3008

  • MaxValueValidator: 3009

  • MinValueValidator: 3010

  • MaxLengthValidator: 3011

  • MinLengthValidator: 3012

  • DecimalValidator: 3013

  • validate_email: 3014

  • validate_slug: 3015

  • validate_unicode_slug: 3016

  • validate_ipv4_address: 3017

  • validate_ipv46_address: 3018

  • validate_comma_separated_integer_list: 3019

  • int_list_validator: 3020

Tests

Pull requests won’t be accepted without passing tests. You can run the test suite with:

python runtests.py

Contributors

  • Geoffrey Lehée <socketubs>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf-friendly-errors-mod-0.13.10.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

drf_friendly_errors_mod-0.13.10-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file drf-friendly-errors-mod-0.13.10.tar.gz.

File metadata

  • Download URL: drf-friendly-errors-mod-0.13.10.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for drf-friendly-errors-mod-0.13.10.tar.gz
Algorithm Hash digest
SHA256 f329dc903eb2fd2979d5e8e5494199306de41db040997fe5ed30887aad0e9470
MD5 ee75c70c5d5f421a21f6133ddcc1f94b
BLAKE2b-256 ec54a49ddb20f7edd00fb412061c96774870b0efa90ba3f6ca382014baf428cc

See more details on using hashes here.

File details

Details for the file drf_friendly_errors_mod-0.13.10-py3-none-any.whl.

File metadata

  • Download URL: drf_friendly_errors_mod-0.13.10-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for drf_friendly_errors_mod-0.13.10-py3-none-any.whl
Algorithm Hash digest
SHA256 2fcc6f21b56dcd6a63dc03557c5cac315576a62aa0868992e679ef1aec1b0816
MD5 e9140b878e76fc00c737d7662a8f384f
BLAKE2b-256 b6f0085893a15d94c6d398c05365a4598ac689031bd33af872e7a8d9bf972f4d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page