Skip to main content

Python request validator

Project description

request_validator

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.

sample:

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(validator.is_valid())
print("\nvalidation errors:")
print(validator.get_errors())
print("\nvalidated data:")
print(validator.validate_data())

The above sample output:

validation status:
False

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 2.0.5
Filename, size File type Python version Upload date Hashes
Filename, size request-validator-2.0.5.tar.gz (6.6 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page