Skip to main content

A composable, expressive data validation library for python

Project description

PyPI version Python CI Documentation

valify

A composable, expressive data validation library for Python.

Installation

pip install valify

Quick Start

from valify import Schema, StringValidator, IntValidator, EmailValidator

schema = Schema({
    "name":  StringValidator(min_length=2, max_length=50),
    "age":   IntValidator(min_value=0, max_value=120),
    "email": EmailValidator(),
})

# Valid data — returns cleaned, validated dictionary
result = schema.validate({
    "name":  "Alice",
    "age":   30,
    "email": "alice@example.com",
})
print(result)
# {'name': 'Alice', 'age': 30, 'email': 'alice@example.com'}

# Invalid data — raises ValidationError with ALL errors at once
schema.validate({
    "name":  "A",
    "age":   -5,
    "email": "not-an-email",
})
# ValidationError: Validation failed:
#   name: Must be at least 2 characters long.
#   age: Must be at least 0.
#   email: 'not-an-email' is not a valid email address.

Validators

Validator What it checks
StringValidator Strings, with optional min/max length
IntValidator Integers, with optional min/max value
FloatValidator Floats, with optional min/max value
BoolValidator Booleans, with optional string coercion
EmailValidator Email address format
OptionalValidator Wraps any validator and makes it optional
ListValidator Validates every item in a list
EnumValidator Value must be one of a fixed set of choices

Validators in Detail

StringValidator

from valify import StringValidator

v = StringValidator(
    min_length=2,   # minimum character length
    max_length=50,  # maximum character length
    strip=True,     # strip whitespace before validating (default: True)
)

IntValidator

from valify import IntValidator

v = IntValidator(
    min_value=0,    # minimum allowed value
    max_value=120,  # maximum allowed value
    coerce=False,   # if True, converts "42" -> 42 (default: False)
)

EmailValidator

from valify import EmailValidator

v = EmailValidator()
v.validate("alice@example.com")  # returns "alice@example.com"

Using Validators Standalone

Validators work without a Schema too:

from valify import IntValidator
from valify.exceptions import ValidationError

v = IntValidator(min_value=0)

try:
    v.validate(-1)
except ValidationError as e:
    print(e.message)  # Must be at least 0.
    print(e.value)    # -1

Nested Schemas

Schemas can be nested inside other schemas for validating complex data:

from valify import Schema, StringValidator, IntValidator

address_schema = Schema({
    "street": StringValidator(min_length=2),
    "city":   StringValidator(min_length=2),
    "pin":    StringValidator(min_length=6, max_length=6),
})

user_schema = Schema({
    "name":    StringValidator(min_length=2),
    "age":     IntValidator(min_value=0),
    "address": address_schema,
})

user_schema.validate({
    "name": "Darshan",
    "age":  20,
    "address": {
        "street": "MG Road",
        "city":   "Pune",
        "pin":    "411001",
    }
})

Error Handling

from valify.exceptions import (
    ValifyError,        # base — catches everything
    ValidationError,    # a value failed validation
    RequiredFieldError, # a required field was missing
    SchemaError,        # the schema definition is invalid
)

Version History

  • 0.4.0 — Added nested schema support
  • 0.3.0 — Added OptionalValidator, ListValidator, EnumValidator
  • 0.2.0 — Full type hints and mypy compatibility
  • 0.1.0 — Initial release

License

MIT

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

valify-0.6.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

valify-0.6.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file valify-0.6.0.tar.gz.

File metadata

  • Download URL: valify-0.6.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for valify-0.6.0.tar.gz
Algorithm Hash digest
SHA256 ee4bf698027b19b1fbd972577aa69b7b831faaf20fcf4caf71a14baea2062311
MD5 644f9470bc7850354d89cb18a169ebc4
BLAKE2b-256 837f62ee713785183c9cb0546b6c8cb50b6b134f7a530a609ee150ab4122003c

See more details on using hashes here.

File details

Details for the file valify-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: valify-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for valify-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 60a033c7a2b946e5e4231a84387530851a0b72d638739bab9b4ca4ed2dda946f
MD5 d586d84836174678d3ab9511b025171f
BLAKE2b-256 338b7bd149538e8371af023a9e30be7c192838e63d2c33bc576332f5ae72f2fd

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