Skip to main content

Python request validator

Project description

Github

https://github.com/amirasaran/request_validator

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.

Source Distribution

request-validator-2.0.5.tar.gz (6.6 kB view details)

Uploaded Source

File details

Details for the file request-validator-2.0.5.tar.gz.

File metadata

File hashes

Hashes for request-validator-2.0.5.tar.gz
Algorithm Hash digest
SHA256 48531ba894a0baa5e9c499f5143b54474e1f21016d1cf07e1fcc012fde770aa8
MD5 a77787116c9901c220fc78da7ff0ae90
BLAKE2b-256 2fd88bf4ab5b16e58b3ddbca85771e00fa827dc4662f3a0bf358e782d7c4aab8

See more details on using hashes here.

Supported by

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