Skip to main content

A simple, type-safe, and extensible Python validations framework

Project description

🔍 V6E

A simple, type-safe, and extensible Python validations framework

Why the name?

v6e comes from the numeronym of "validate".

Examples

Check out the examples in ./examples! You can run them locally with:

uv run examples/validations.py

Usage

Basic validations

import v6e as v

my_validation = v.int().gte(18).lte(21)

# Use it only to check if the value conforms
my_validation.check(18)  # True
my_validation.check(21)  # True
my_validation.check(54)  # False

# Use `.parse()` to validate and get the parsed value
my_validation.parse(21)  # Ok -> Returns 21 (int)
my_validation.parse("21")  # Ok -> Returns 21 (int)
my_validation.parse(54)  # Err -> Raises a ValidationException

Chaing your validations and transformations

my_validation = v.str().trim().starts_with("foo").ends_with("foo").regex(r"^[a-z0-9]*$")
my_validation.parse("  foo12")  # Ok -> Returns 'foo12' (str)
my_validation.parse("12foo  ")  # Ok -> Returns '12foo' (str)
my_validation.parse("1foo2")  # Err -> Raises a ValidationException

Custom validations

class DivThree(v.IntType):
    @override
    def _parse(self, raw: t.Any):
        parsed: int = super()._parse(raw)
        if parsed % 3 != 0:
            raise ValueError(f"Woops! {parsed!r} is not divisible by three")


my_validation = DivThree().gt(5)
my_validation(6)  # Ok -> Returns 6
my_validation(3)  # Err (not >5) -> Raises a ValidationException
my_validation(7)  # Err (not div by 3) -> Raises a ValidationException

🐍 Type-checking

This library is fully type-checked. This means that all types will be correctly inferred from the arguments you pass in.

In this example your editor will correctly infer the type:

my_validation = v.int().gte(8).lte(4)
t.reveal_type(my_validation)  # Type of "my_validation" is "V6eInt"
t.reveal_type(my_validation.check)  # Type of "my_validation.check" is "(raw: Any) -> bool"
t.reveal_type(my_validation.safe_parse)  # Type of "my_validation" is "(raw: Any) -> ParseResult[int]"
t.reveal_type(my_validation.parse)  # Type of "my_validation" is "(raw: Any) -> int"

Why do I care?

Type checking will help you catch issues way earlier in the development cycle. It will also provide nice autocomplete features in your editor that will make you faster ⚡.

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

v6e-0.1.3.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

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

v6e-0.1.3-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file v6e-0.1.3.tar.gz.

File metadata

  • Download URL: v6e-0.1.3.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.2

File hashes

Hashes for v6e-0.1.3.tar.gz
Algorithm Hash digest
SHA256 1c211b968ff6f03a08fff2613da3b456caba1f642ba975910741af4e5314c938
MD5 43a3c5e0d823f2c67355b0c1e84ce789
BLAKE2b-256 b84597253d2697bd542764ecfeb36ff4b39ae44cad2e0b54fab6ea7cc2be362f

See more details on using hashes here.

File details

Details for the file v6e-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: v6e-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.2

File hashes

Hashes for v6e-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 661fba74cea26f346ae796fdae5f408e9f8c07ab31138ac09d1701963b4204d2
MD5 599c1c0d0c5a46fc04f3cca7311a1f63
BLAKE2b-256 862b8d5d41ea106f25fdf54561195aad03adb802d5f594cba1b8b4116ccda66d

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