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.11.tar.gz (28.3 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.11-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: vtjson-2.2.11.tar.gz
  • Upload date:
  • Size: 28.3 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.11.tar.gz
Algorithm Hash digest
SHA256 26238ce40a4dbebc249e004d33366e3a86129c51febe7c93045b7863ef5353a3
MD5 f1ba3c1d822f91f46427cd7310eae0b1
BLAKE2b-256 33d694256b2f5a549f6022399fe85413df698cd34199802ed5a7d1c88e3289a0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: vtjson-2.2.11-py3-none-any.whl
  • Upload date:
  • Size: 19.0 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.11-py3-none-any.whl
Algorithm Hash digest
SHA256 e00e1011d6ff97722dbde9523c98d5a203d41b21eabcb73797d7e52caab19650
MD5 80add787d90331f3197421221571059c
BLAKE2b-256 9ccce98b6413bd45360d64b64c2691b18d5ae07313bd1c436e9231b7c7480bd9

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