Skip to main content

django_validated_jsonfield

django_validated_jsonfield is an inplace replacement for django JSONField which is validated against a json_schema

  • A json_schema can be added to the field description
  • The json_schema is used to validate the jsonfield data during full_clean or during restframework validation
  • The field is also documented on Swagger (via drf_yasg specific inspectors)
  • Missing data in the json structure is initialized/filled to the schema's default values

Usage

import

Replace your JSONField import

from django.db.models import JSONField` or `from django.contrib.postgres.fields import JSONField

by

from django_validated_jsonfield import ValidatedJSONField as JSONField

models

class MyModel(models.Model):
    _data_schema = {
        "type": "object",
        "properties": {
            "en": {"type": ["string", "null"], "default":"",   "example":"Name", "title":"Name"}
            "fr": {"type": ["string", "null"], "default":"",   "example":"Nom",  "title":"Nom"}
        },
        "default": {}, #note the top level default
        "additionalProperties": False,
    }

    data = JSONField(schema=_data_schema, editable=True)

Rest Framework serializers

As defined field:

replace

from rest_framework.serializers import JSONField

by

from django_validated_jsonfield import ValidatedJSONFieldSerializer as JSONField

The updated JSONField expect to receive a "schema" and "json_validator_cls" argument at initilization.

In serializers.ModelSerializer:

Add the ValidatedJsonModelSerializerMixin to your ModelSerializer classes.

from django_validated_jsonfield import ValidatedJsonModelSerializerMixin

class MyModelSerializer(ValidatedJsonModelSerializerMixin, serializers.ModelSerializer):
    class Meta:
        model = MyModel
        exclude = []

Swagger documentation (drf_yasg)

add the following block to your django settings.py file

from django_validated_jsonfield.yasg import DEFAULT_FIELD_INSPECTORS
SWAGGER_SETTINGS = {
    'DEFAULT_FIELD_INSPECTORS': DEFAULT_FIELD_INSPECTORS
}

you should see the json field of your model being documented in Swagger

Additional remarks

json schema default

if the default field is provided in the json schema, the data will be initialized to the default value (if missing). The feature works well only parents of nested fields in the json schema have a default themselves (to list or dict) --> Note the "top level default" in the example above.

Release files for django-validated-jsonfield 1.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-validated-jsonfield 1.2.3
File Size Uploaded
django-validated-jsonfield-1.2.3.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-validated-jsonfield 1.2.3
File Interpreter ABI Platform
django_validated_jsonfield-1.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 12.9 kB

Release files / django-validated-jsonfield-1.2.3.tar.gz

Download URL django-validated-jsonfield-1.2.3.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8881d38178c50d8d995b6e8deb793b716104a82a9ad09ca16e5ab4a946c34e71
BLAKE2b-256 checksum
How to use checksums
c86d0c4f959c6c7c2651398c9e167ff93e8f7913e792c66937c4bc371e501cf6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.8.5 Linux/5.4.0-73-generic

Release files / django_validated_jsonfield-1.2.3-py3-none-any.whl

Download URL django_validated_jsonfield-1.2.3-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4ffa76be4bb0f6c171efeb5f3648c991a2e8167e028219a2feaebaed67d7d4c1
BLAKE2b-256 checksum
How to use checksums
496c867dc85364330aa936326b563e2ca6455c066e374a9f5f63af132badb207
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.8.5 Linux/5.4.0-73-generic

Release history Release notifications | RSS feed

This release

1.2.3 This release

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.9

2 release files

1.1.8

2 release files

1.1.7

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page