Skip to main content

Quick and dirty jsonapi response validator

Project description

Quick and dirty jsonapi response validator

A library for validating jsonapi responses and simplifying their handling.

What it does:

  • Validates that types have the fields you want, of the type you want.
  • Links resources together.
  • Validates that resources you wanted included, are included.
  • Pulls up relationships and attributes one level up, to resource object level.

What it doesn't do:

  • Validate that response is valid jsonapi. Use jsonapi's jsonschema.
  • Build requests or handle http codes.
  • ORM.

Usage

Here's example usage:

# Start by defining your types

from qdjarv import Validator, Type, Rel, ValidationError

# Field value can be any callable that either returns a validated value or
# throws.

def Type(t):
    def v(o):
        if isinstance(o, t):
            raise ValidationError
        return o
    return v

types = {
    "articles": {
        "title": Type(str),
        "author": Rel("people"),	# One-to-one relation.
        "comments": Rel(["comments"]),	# One-to-many relation.
    },
    "people": {
        "firstName": Type(str),
        "lastName": Type(str),
        "weight": Type([int])
    },
    "comments": {
        "body": Type(str),
        "author": Rel("people")
    }
}

# If you're using sparse fields, define those, too.

fields = {
    "articles": ["title", "author"],
    "people": ["firstName", "lastName"]
}

# If you're using includes, also define them.
# This is equivalent to 'author,author.comments'.

include = {
    "author": {
        "comments": {}
    }
}

# Also declare your toplevel type.
top = ["articles"]  # This is a list, single element would be just "articles".

# Finally, create a validator.
p = Validator(top, types, include=include, fields=fields)

# Parsing modifies the received message, so make a copy if you want the
# original!
# Also remember to pass the message through jsonapi jsonschema first.
# If something goes wrong, it will throw a qdjarv.ValidationError.
parsed = p.validate(message)

# If you don't feel like repeating yourself, you can get your get parameters
# like so:
fields_args = p.fields_args()
include_args = p.include_args()

Here's an example parsed message:

{
    # Other toplevel stuff skipped for brevity.
    "data": [
        {
            "type": "articles",
            "id": "1",

            # Relationships and attributes were pulled out to this level.
            # You might want to access links and meta (and maybe
            # relationships / attributes), so they're kept with a dot
            # prepended.
            ".links": {
            },
            ".meta": { # ...
            }
            ".relationships": { # ...
            }
            ".attributes": { # ...
            },

            "title": "JSON:API paints my bikeshed!"
            "author": {
                "links": {
                    # ...
                },
                "data": {
                    # If this resource was included, we replaced the binding
                    # with the object itself.
                    # Watch out for loops!
                    "type": "people",
                    "id": "9",
                    ".attributes": { # ...
                    },
                    ".links": { # ...
                    },
                    "firstName": "Dan",
                    "lastName": "Gebhardt",
                    "twitter": "dgeb"
                }
            },
            "comments": { # ...
            },
        }
    ],
    "included": [
        # Included objects are still here, in case you wanted them. Also linked
        # and flattened.
        {
            "type": "people",
            "id": "9",
            # ...
        },
        # ...
    ]
}

TODO

  • Tests.
  • Turning types into requests, maybe.

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

qdjarv-0.1.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qdjarv-0.1.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file qdjarv-0.1.1.tar.gz.

File metadata

  • Download URL: qdjarv-0.1.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.6

File hashes

Hashes for qdjarv-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9f26c24da7ee11ad527020cd8da8064f926a55a862f374d68df3f4a418c62f2a
MD5 dcc193c6f3be2b2ddbd1b1328fb5ae0b
BLAKE2b-256 6555ffa06390759a231e836d2ec90a3ed3145d9ed4b85e4df981dbc5cb2b69e2

See more details on using hashes here.

File details

Details for the file qdjarv-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: qdjarv-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.6

File hashes

Hashes for qdjarv-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 245667940c10b32c1c50617ac5f2fb405620c2006eca4e6d581793326d1f07fc
MD5 4bb1f9200fa02dcc93066f6f6e0438d8
BLAKE2b-256 db3eeac3b480addd4bd6c95d5f03a60415c6d959f2022ef14378fd1e4b8b9f0a

See more details on using hashes here.

Supported by

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