No project description provided
Project description
Hydra Validator Library
Hydrah is a validation library for Python that provides a simple and easy way to validate inputs in your applications. With Hydrah, you can validate user inputs, command-line arguments, or configuration files with just a few lines of code.
Features
- Simple and easy to use
- Supports multiple data types (strings, integers, lists, objects, booleans, datetime)
- Can validate inputs and provide detailed error messages
- Provides a
coercemethod to automatically convert inputs to the desired data type
Installation
h can be installed using pip:
pip install hydrah
Usage
To validate an input, you need to first create a validator instance for the desired data type. For example, to validate a string input:
from hydrah import h
validator = h.string()
if validator.is_valid("hello"):
print("Valid input")
else:
print(validator.get_error_message("hello"))
To validate a list of strings:
from hydrah import h
list_validator = h.list()string()
if list_validator.is_valid(["hello", "world"]):
print("Valid input")
else:
print(list_validator.get_error_message(["hello", 1]))
To validate an object:
from hydrah import h
object_validator = h.object({
"name": h.string(),
"age": h.integer()
})
if object_validator.is_valid({"name": "John", "age": 30}):
print("Valid input")
else:
print(object_validator.get_error_message({"name": "John", "age": "30"}))
Custom Validators
Hydrah can also be extended to support custom data types. To create a custom validator, you need to inherit from the Validator class and implement the is_valid and get_error_message methods.
For example, to create a validator for boolean inputs:
from hydrah import Validator
class BooleanValidator(Validator):
def is_valid(self, value):
return isinstance(value, bool)
def get_error_message(self, value):
return f"Expected boolean, but got {type(value).__name__}"
Coercion
Hydrah also provides a coerce method to automatically convert inputs to the desired data type. For example:
from hydrah import h
validator = h.integer()
value = validator.coerce("10")
print(value) # 10
print(type(value)) # <class 'int'>
Creating Objects with Optional Schemas
In some cases, you may want to define a schema that is optional, meaning that it can either be present or absent in the data. To define an optional schema in Hydrah, you can either use hydrah.h.string().optional() or hydrah.h.optional(hydrah.h.string()).
Here's an example of how you could define an object with an optional string field using both methods:
from hydrah import h
# Using string().optional()
optional_string_validator = h.object({
"optional_field": h.string().optional()
})
# Using optional(hydrah.h.string())
optional_string_validator = h.object({
"optional_field": h.optional(hydrah.string())
})
# Both of these validators will accept the following data:
data = {
"optional_field": "Hello, world!"
}
assert optional_string_validator.is_valid(data)
# And also this data:
data = {}
assert optional_string_validator.is_valid(data)
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 hydrah-0.1.1.tar.gz.
File metadata
- Download URL: hydrah-0.1.1.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.10.9 Darwin/22.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a87bc529745d5548c634ec99b3cec949a94accc2d7b8c10f1e2b03298efa2b08
|
|
| MD5 |
62ed2d5af23221738535236de52d655c
|
|
| BLAKE2b-256 |
8a39160907b41fd86b4e889f8af4bb95c0b56fc5c59c3dc9b1005b7c34724030
|
File details
Details for the file hydrah-0.1.1-py3-none-any.whl.
File metadata
- Download URL: hydrah-0.1.1-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.10.9 Darwin/22.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
de7b79b8eddd41abf0b53266733d76883e5e0bf889d520d5c29352badc442030
|
|
| MD5 |
46773729a2d210fa58e7e6f8800ba368
|
|
| BLAKE2b-256 |
bbecbe3d76e23bbd4d6fff36e5bf0d8032f6278203969aee6e21726bedf3d358
|