Typesafe, composable validation
Project description
Koda Validate
Koda Validate is a library and toolkit for building composable and typesafe validators. In many cases, validators can be derived from typehints (e.g. TypedDicts, dataclasses, and NamedTuples). For everything else, you can combine existing validation logic, or write your own. At its heart, Koda Validate is just a few kinds of callables that fit together, so the possibilities are endless. It is async-friendly and comparable in performance to Pydantic 2.
Koda Validate can be used in normal control flow or as a runtime type checker.
Docs: https://koda-validate.readthedocs.io/en/stable/
At a Glance
Explicit Validators
from koda_validate import ListValidator, StringValidator, MaxLength, MinLength
my_string_validator = StringValidator(MinLength(1), MaxLength(20))
my_string_validator("a string!")
#> Valid("a string!")
my_string_validator(5)
#> Invalid(...)
# Composing validators
list_string_validator = ListValidator(my_string_validator)
list_string_validator(["a", "b", "c"])
#> Valid(["a", "b", "c"])
Derived Validators
from typing import TypedDict
from koda_validate import (TypedDictValidator, Valid, Invalid)
from koda_validate.serialization import to_serializable_errs
class Person(TypedDict):
name: str
hobbies: list[str]
person_validator = TypedDictValidator(Person)
match person_validator({"name": "Guido"}):
case Valid(string_list):
print(f"woohoo, valid!")
case Invalid() as invalid:
# readable errors
print(to_serializable_errs(invalid))
#> {'hobbies': ['key missing']}
Runtime Type Checking
from koda_validate.signature import validate_signature
@validate_signature
def add(a: int, b: int) -> int:
return a + b
add(1, 2) # returns 3
add(1, "2") # raises `InvalidArgsError`
# koda_validate.signature.InvalidArgsError:
# Invalid Argument Values
# -----------------------
# b='2'
# expected <class 'int'>
There's much, much more in the Docs.
Something's Missing or Wrong
Open an issue on GitHub please!
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file koda_validate-4.1.1.tar.gz.
File metadata
- Download URL: koda_validate-4.1.1.tar.gz
- Upload date:
- Size: 41.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a818a4c80bdb539aab0a70c48ff37f28f0f2cb07d1c6cf4fb10d664af42c11a
|
|
| MD5 |
b19e0c62eb092202f8a7f3f5b5483ebf
|
|
| BLAKE2b-256 |
77f351a061a669e77bb62f83d5ad638e806c588786efadcde8990696661f6bf3
|
File details
Details for the file koda_validate-4.1.1-py3-none-any.whl.
File metadata
- Download URL: koda_validate-4.1.1-py3-none-any.whl
- Upload date:
- Size: 53.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33ea4a73c2c1a67f61606f53227391cbcd21378039db4c55f471f16258082e97
|
|
| MD5 |
8ce97cf849bcfd4429a0965c5d11ab88
|
|
| BLAKE2b-256 |
fd833f53dbc9d142518a80c41ee51564081d96b25845c951301f329ab683637a
|