vtjson


Namevtjson JSON
Version 2.2.4 PyPI version JSON
download
home_pageNone
SummaryAn easy to use validation library compatible with Python type annotations
upload_time2024-12-22 11:10:25
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # vtjson

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

## Introduction

Here is a simple schema:

```python
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:

```python
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:

```text
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.

```python
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`.

```python
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.

```python
bad_book = {
    "title": "Gone with the Wind",
    "authors": [{"name": "Margaret Mitchell", "email": "margaret@gmailcom"}],
    "year": "1936",
}
```

```text
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.

```python
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](https://www.cantate.be/vtjson) (canonical reference) or [https://vtjson.readthedocs.io](https://vtjson.readthedocs.io).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "vtjson",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "Michel Van den Bergh <michel.vandenbergh@uhasselt.be>",
    "download_url": "https://files.pythonhosted.org/packages/d7/82/fce371aa5211c5eaf861b402bcd8cbaba19b7b279119333e34d843240395/vtjson-2.2.4.tar.gz",
    "platform": null,
    "description": "# vtjson\n\n`vtjson` is an easy to use validation library compatible with Python type annotations.\n\n## Introduction\n\nHere is a simple schema:\n\n```python\nbook_schema = {\n    \"title\": str,\n    \"authors\": [str, ...],\n    \"editor?\": str,\n    \"year\": int,\n}\n```\n\nThe following conventions were used:\n\n- 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`.\n- 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.\n\nLet's try to validate some book objects:\n\n```python\ngood_book = {\n    \"title\": \"Gone with the Wind\",\n    \"authors\": [\"Margaret Mitchell\"],\n    \"year\": 1936,\n}\n\nbad_book = {\n    \"title\": \"Gone with the Wind\",\n    \"authors\": [\"Margaret Mitchell\"],\n    \"year\": \"1936\",\n}\n\nvalidate(book_schema, good_book, name=\"good_book\")\nvalidate(book_schema, bad_book, name=\"bad_book\")\n```\n\nAs expected `vtjson` throws an exception for the second object:\n\n```text\nTraceback (most recent call last):\n          ...\n    raise ValidationError(message)\nvtjson.vtjson.ValidationError: bad_book['year'] (value:'1936') is not of type 'int'\n```\n\nWe may also rewrite the `book_schema` as a valid Python type annotation.\n\n```python\nclass book_schema(TypedDict):\n    title: str\n    authors: list[str]\n    editor: NotRequired[str]\n    year: int\n```\n\nAttempting to validate the bad book would raise a similar exception as before.\n\nSchemas can of course be more complicated and in particular they can be nested.\nHere is an example that shows more of the features of `vtjson`.\n\n```python\nperson_schema = {\n    \"name\": regex(\"[a-zA-Z. ]*\"),\n    \"email?\": email,\n    \"website?\": url,\n}\n\nbook_schema = {\n    \"title\": str,\n    \"authors\": [person_schema, ...],\n    \"editor?\": person_schema,\n    \"year\": intersect(int, ge(1900)),\n}\n```\n\nLet's try to validate an object not fitting the schema.\n\n```python\nbad_book = {\n    \"title\": \"Gone with the Wind\",\n    \"authors\": [{\"name\": \"Margaret Mitchell\", \"email\": \"margaret@gmailcom\"}],\n    \"year\": \"1936\",\n}\n```\n\n```text\nTraceback (most recent call last):\n          ...\n    raise ValidationError(message)\nvtjson.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.\n```\n\nAs before we can rewrite the new `book_schema` as a valid type annotation.\n\n```python\nclass person_schema(TypedDict):\n    name: Annotated[str, regex(\"[a-zA-Z. ]*\")]\n    email: NotRequired[Annotated[str, email]]\n    website: NotRequired[Annotated[str, url]]\n\nclass book_schema(TypedDict):\n    title: str\n    authors: list[person_schema]\n    editor: NotRequired[person_schema]\n    year: Annotated[int, ge(1900)]\n```\n\nFor comprehensive documentation about `vtjson` see [https://www.cantate.be/vtjson](https://www.cantate.be/vtjson) (canonical reference) or [https://vtjson.readthedocs.io](https://vtjson.readthedocs.io).\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "An easy to use validation library compatible with Python type annotations",
    "version": "2.2.4",
    "project_urls": {
        "Bug Tracker": "https://github.com/vdbergh/vtjson/issues",
        "Homepage": "https://github.com/vdbergh/vtjson"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "83ad9ecd042730f571111e92a27bba836008454755178c967d9a738d100123ae",
                "md5": "01668f36dde3f75d0b259764d218b9cf",
                "sha256": "cb94a36023d3351ed3d8b948c05c2b2963691463173642c3383a26afa54e9935"
            },
            "downloads": -1,
            "filename": "vtjson-2.2.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "01668f36dde3f75d0b259764d218b9cf",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 18035,
            "upload_time": "2024-12-22T11:10:23",
            "upload_time_iso_8601": "2024-12-22T11:10:23.953456Z",
            "url": "https://files.pythonhosted.org/packages/83/ad/9ecd042730f571111e92a27bba836008454755178c967d9a738d100123ae/vtjson-2.2.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d782fce371aa5211c5eaf861b402bcd8cbaba19b7b279119333e34d843240395",
                "md5": "2524141d6724642d0e1c542828e4dad9",
                "sha256": "efd1bc8bc6ca92a45f4aa16b6132947194ee2f66111b9431fc3696fe65e8680a"
            },
            "downloads": -1,
            "filename": "vtjson-2.2.4.tar.gz",
            "has_sig": false,
            "md5_digest": "2524141d6724642d0e1c542828e4dad9",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 26064,
            "upload_time": "2024-12-22T11:10:25",
            "upload_time_iso_8601": "2024-12-22T11:10:25.388095Z",
            "url": "https://files.pythonhosted.org/packages/d7/82/fce371aa5211c5eaf861b402bcd8cbaba19b7b279119333e34d843240395/vtjson-2.2.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-22 11:10:25",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "vdbergh",
    "github_project": "vtjson",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "vtjson"
}
        
Elapsed time: 0.37553s