Skip to main content

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

from koda_validate import StringValidator

my_string_validator = StringValidator()

my_string_validator("a string!")
#> Valid("a string!")

my_string_validator(5)
#> Invalid(...)

Additional Validation

from koda_validate import MaxLength, MinLength

str_len_validator = StringValidator(MinLength(1), MaxLength(20))

str_len_validator("abc")
#> Valid("abc")

str_len_validator("")
#> Invalid(...)

str_len_validator("abcdefghijklmnopqrstuvwxyz")
#> Invalid(...)

Combining Validators

from koda_validate import ListValidator, StringValidator

list_string_validator = ListValidator(StringValidator())

list_string_validator(["a", "b", "c"])
# > Valid(["a", "b", "c"])

list_string_validator([1, 2, 3])
# > Invalid(...)

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


Download files

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

Source Distribution

koda_validate-5.1.0.tar.gz (41.2 kB view details)

Uploaded Source

Built Distribution

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

koda_validate-5.1.0-py3-none-any.whl (54.7 kB view details)

Uploaded Python 3

File details

Details for the file koda_validate-5.1.0.tar.gz.

File metadata

  • Download URL: koda_validate-5.1.0.tar.gz
  • Upload date:
  • Size: 41.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.13.6 Darwin/24.6.0

File hashes

Hashes for koda_validate-5.1.0.tar.gz
Algorithm Hash digest
SHA256 9e2d5f39b2a1f00ba07078e423d134a2f0cde7a209a79fa332174dd5b30a98f9
MD5 0267cc6a7ab6f3d27f1c1e65361a3777
BLAKE2b-256 bbc7cd0782a87324ad40cf2fc66d32c7ef5ffd5597ae6538b4fa21d5dbbbc4c8

See more details on using hashes here.

File details

Details for the file koda_validate-5.1.0-py3-none-any.whl.

File metadata

  • Download URL: koda_validate-5.1.0-py3-none-any.whl
  • Upload date:
  • Size: 54.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.13.6 Darwin/24.6.0

File hashes

Hashes for koda_validate-5.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a5832df7a5b82325c608b94889f5e8668de210075bb289b1e4892fc0e6c1d9e4
MD5 790076ff4d2b78490e98fc1f4dc22293
BLAKE2b-256 2787fc7308f36e171a11f54a7e99e449f301711ddffdd056f1aa9b9e495f7aa2

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