No project description provided
Project description
Validates Python’s TypedDict
日本語の説明はこちら / Description in Japanese
About
This is a utility to validate a dict object using TypedDict. It recursively checks whether the dict has necessary keys and the values has appropriate type. It will be useful when you deserialize the json or yaml data, such as an API request or any file.
Currently supported types are generic types and Union (including Optional).
Requirements
Python 3.10 or higher
Installation
https://pypi.org/project/typeddict-validator/
pip install typeddict-validator
Usage
>>> from typing import TypedDict
>>> from typeddict_validator import validate_typeddict
>>>
>>> PersonDict = TypedDict("PersonDict", {"name": str, "age": int, "interests": list[str]})
>>>
>>> person = {"name": "Taro Yamada", "age": 30, "interests": ["programming", "painting"]}
>>>
>>> if validate_typeddict(person, PersonDict):
... print("It's a PersonDict!")
It's a PersonDict!
By default, it will raise an error when the dict object does not match.
>>> robot = {"name": "Doraemon"}
>>>
>>> if validate_typeddict(robot, PersonDict):
... print("It's a PersonDict!")
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/app/typeddict_validator/validate.py", line 37, in validate_typeddict raise DictMissingKeyException(key=k) typeddict_validator.validate.DictMissingKeyException
You can use silent=True option not to raise an error.
>>> if not validate_typeddict(robot, PersonDict, silent=True):
... print("It's not a PersonDict!!")
It's not a PersonDict!!
See typeddict_validator/validate_test.py for more examples.
Tests
docker run --mount source=$(pwd),target=/app,type=bind python:latest python3 -m unittest -v /app/typeddict_validator/validate_test.py
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
Built Distribution
Hashes for typeddict-validator-0.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d5daab8b54cdd770f5c3b168c4efcef541b8e85e8b426cf72ab1566c6803a22 |
|
MD5 | 400fedad4c4fc52b4c95a22d8dfbb80e |
|
BLAKE2b-256 | 2b12fd108f455dde0520a6e0ffac4019937e6402a62248c71c85b9519791537a |
Hashes for typeddict_validator-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1613247ce8edfaf0a105ab80111dbb5d8ca12913e0e63ba73d82d6fa573bf29d |
|
MD5 | 6d3420249ec9ca0e346f772f49addde3 |
|
BLAKE2b-256 | c2f3201601434c44608aeef27318f7497e65e6c4b926db911da8ebedd268e6fc |