Skip to main content

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 coerce method 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


Download files

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

Source Distribution

hydrah-0.1.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

hydrah-0.1.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file hydrah-0.1.0.tar.gz.

File metadata

  • Download URL: hydrah-0.1.0.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

Hashes for hydrah-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9f2dff90dbfac7abeae5f8bc4b169da8bb94b895071d5de085af3dbbae97fa49
MD5 3457646bc8da6c31490e275098fdf5c8
BLAKE2b-256 d8d8f2bf5fd41c2d5508b241334ebb52c4ea2b9792032df716e4dce475a6816a

See more details on using hashes here.

File details

Details for the file hydrah-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: hydrah-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 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

Hashes for hydrah-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e607d4f3401fbb9bf2a40b9c9310a0ea07dfc0064a5a49e72367d7aa3c28d65
MD5 43291e59d275766c695470bbcb783494
BLAKE2b-256 da6f76760fcbd8df612d59edbd579a290d3a417593f7d20c42be51f087cadbfc

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