Skip to main content

A lightweight package for validating JSON like Python objects

Project description

vtjson

vtjson is an easy to use validation library compatible with Python type annotations.

Introduction

Here is a simple schema:

book_schema = {
    "title": str,
    "authors": [str, ...],
    "editor?": str,
    "year": int,
}

The following conventions were used:

  • As in typescript, a (string) key ending in ? represents an optional key. The corresponding schema (the item the key points to) will only be used for validation when the key is present in the object that should be validated. A key can also be made optional by wrapping it with optional_key.
  • If in a list/tuple the last entry is ... (ellipsis) it means that the next to last entry will be repeated zero or more times. In this way generic types can be created. For example the schema [str, ...] represents a list of strings.

Let's try to validate some book objects:

good_book = {
    "title": "Gone with the Wind",
    "authors": ["Margaret Mitchell"],
    "year": 1936,
}

bad_book = {
    "title": "Gone with the Wind",
    "authors": ["Margaret Mitchell"],
    "year": "1936",
}

validate(book_schema, good_book, name="good_book")
validate(book_schema, bad_book, name="bad_book")

As expected vtjson throws an exception for the second object:

Traceback (most recent call last):
          ...
    raise ValidationError(message)
vtjson.vtjson.ValidationError: bad_book['year'] (value:'1936') is not of type 'int'

We may also rewrite the book_schema as a valid Python type annotation.

class book_schema(TypedDict):
    title: str
    authors: list[str]
    editor: NotRequired[str]
    year: int

Attempting to validate the bad book would raise a similar exception as before.

Schemas can of course be more complicated and in particular they can be nested. Here is an example that shows more of the features of vtjson.

person_schema = {
    "name": regex("[a-zA-Z. ]*"),
    "email?": email,
    "website?": url,
}

book_schema = {
    "title": str,
    "authors": [person_schema, ...],
    "editor?": person_schema,
    "year": intersect(int, ge(1900)),
}

Let's try to validate an object not fitting the schema.

bad_book = {
    "title": "Gone with the Wind",
    "authors": [{"name": "Margaret Mitchell", "email": "margaret@gmailcom"}],
    "year": "1936",
}
Traceback (most recent call last):
          ...
    raise ValidationError(message)
vtjson.vtjson.ValidationError: bad_book['authors'][0]['email'] (value:'margaret@gmailcom') is not of type 'email': The part after the @-sign is not valid. It should have a period.

As before we can rewrite the new book_schema as a valid type annotation.

class person_schema(TypedDict):
    name: Annotated[str, regex("[a-zA-Z. ]*")]
    email: NotRequired[Annotated[str, email]]
    website: NotRequired[Annotated[str, url]]

class book_schema(TypedDict):
    title: str
    authors: list[person_schema]
    editor: NotRequired[person_schema]
    year: Annotated[int, ge(1900)]

For comprehensive documentation about vtjson see https://www.cantate.be/vtjson (canonical reference) or https://vtjson.readthedocs.io.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

vtjson-2.2.10.tar.gz (27.8 kB view details)

Uploaded Source

Built Distribution

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

vtjson-2.2.10-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file vtjson-2.2.10.tar.gz.

File metadata

  • Download URL: vtjson-2.2.10.tar.gz
  • Upload date:
  • Size: 27.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for vtjson-2.2.10.tar.gz
Algorithm Hash digest
SHA256 c1748106437b64b6cee7a4089def371c1c06e2b748e41c36e02b47bfd94c385f
MD5 6d8e962f89693cd9c3a316bee6cd014e
BLAKE2b-256 70890618a5ce6ffdd3ca4534fbe7bdcd25c0c44a42305de65d30c20e4bf1bcd5

See more details on using hashes here.

File details

Details for the file vtjson-2.2.10-py3-none-any.whl.

File metadata

  • Download URL: vtjson-2.2.10-py3-none-any.whl
  • Upload date:
  • Size: 18.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for vtjson-2.2.10-py3-none-any.whl
Algorithm Hash digest
SHA256 2feae13abee54450a5483791409d3fb860951202d3e49cb7015a857de61cbd69
MD5 1dca48d052eb78da6fe859f912243f36
BLAKE2b-256 2f13cd53a7ea63e481c32ca13186a7a642f4ce8efe3adbac94104eb4b6a71a91

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