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
Chaining 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
Handling multiple possible types
union = v.str().starts_with("foo") | v.int().gte(5)
union.parse("foobar") # Ok -> Returns 'foobar' (str)
union.parse("1foo2") # Err -> Raises a ValidationException
union.parse(5) # Ok -> Returns 5 (int)
union.parse(3) # Err -> Raises a ValidationException
union.parse(None) # Err -> Raises a ValidationException
Custom validations
def _validate_earth_age(x: int) -> None:
if x != 4_543_000_000:
raise ValueError("The Earth is 4.543 billion years old. Try 4543000000.")
earth_age = v.int().custom(_validate_earth_age)
earth_age.parse(4_543_000_000) # Ok -> Returns 4_543_000_000 (int)
earth_age.parse("4543000000") # Ok -> Returns 4_543_000_000 (int)
earth_age.parse(1) # Err -> Raises ValidationException
Custom reusable types
class DivThree(v.IntType):
@override
def parse_raw(self, raw: t.Any):
parsed: int = super().parse_raw(raw)
if parsed % 3 != 0:
raise ValueError(f"Woops! {parsed!r} is not divisible by three")
my_validation = DivThree().gt(5)
my_validation.parse(6) # Ok -> Returns 6
my_validation.parse(3) # Err (not >5) -> Raises a ValidationException
my_validation.parse(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) -> V6eResult[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
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 v6e-0.1.4.tar.gz.
File metadata
- Download URL: v6e-0.1.4.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
62499167828c713a4cbe1ed7719e39fc2a8036022da853c0a33ff0b564f64078
|
|
| MD5 |
0edc68f5c5dfba07b60f635ffe672685
|
|
| BLAKE2b-256 |
e0277a1c8c4cd360cb2d3d3610fadaa44c8e3388de7f8054ffffbe5b797fd005
|
File details
Details for the file v6e-0.1.4-py3-none-any.whl.
File metadata
- Download URL: v6e-0.1.4-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3fe4ec534a4fb6925d79ccb7f5638265d5b46f962750332df8c6bc9ec805e248
|
|
| MD5 |
b5a18291057f175007b1de140d4781d6
|
|
| BLAKE2b-256 |
3f19fa92a1f63fd3882ca56e88e472c4acb87116b32d03d28231cc5e6eac2f60
|