Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

Python request validator

Project description


Python Request(json) Validator

how to install

pip install request_validator

How to use

Imaging you have a json response that get from rest api server and you
must validate response.


from request_validator.serializers import *
from request_validator.fields import *

class GlossDefSerializer(BaseSerializer):
    para = CharField()
    gloss_see_also = CharField(many=True, source="GlossSeeAlso")

class GlossEntrySerializer(BaseSerializer):
    id = IntField(source="ID")
    sort_as = CharField(source="SortAs")
    gloss_term = CharField(source="GlossTerm")
    acronym = CharField(source="Acronym")
    abbrev = CharField(source="Abbrev", required=True, allow_blank=False)
    gloss_def = GlossDefSerializer(source="GlossDef")
    gloss_see = CharField(source="GlossSee")

class GlossListSerializer(BaseSerializer):
    gloss_entry = GlossEntrySerializer(source="GlossEntry")

class GlossDivSerializer(BaseSerializer):
    title = CharField()
    gloss_list = GlossListSerializer(source="GlossList")

class GlossarySerializer(BaseSerializer):
    title = CharField()
    gloss_div = GlossDivSerializer(source="GlossDiv")

class SampleSerializer(BaseSerializer):
    glossary = GlossarySerializer()

sample_data = {
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": 12,
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    "GlossSee": "markup"

validator = SampleSerializer(data=sample_data)
print('validation status:')
print("\nvalidation errors:")
print("\nvalidated data:")

The above sample output:

validation status:

validation errors:
{'glossary': {'gloss_div': {'gloss_list': {'gloss_entry': {'abbrev': ['This field cannot be blank']}}}}}

validated data:
{'glossary': {'title': 'example glossary', 'gloss_div': {'title': 'S', 'gloss_list': {'gloss_entry': {'acronym': 'SGML', 'gloss_term': 'Standard Generalized Markup Language', 'gloss_def': {'gloss_see_also': ['GML', 'XML'], 'para': 'A meta-markup language, used to create markup languages such as DocBook.'}, 'gloss_see': 'markup', 'sort_as': 'SGML', 'id': 12}}}}}

Project details

Download files

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

Files for request-validator, version 1.4.2
Filename, size File type Python version Upload date Hashes
Filename, size request-validator-1.4.2.tar.gz (6.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page