Skip to main content

A TypeScript Zod-inspired validation library for Python with excellent type safety and developer experience

Project description

zodic

PyPI version Python versions License: MIT Tests

A TypeScript Zod-inspired validation library for Python with excellent type safety and developer experience.

Features

  • Type-safe validation with excellent IDE support and autocompletion
  • Intuitive chainable API for building complex schemas
  • Lightning-fast performance - 2M+ operations/second
  • Comprehensive error reporting with detailed nested paths
  • Zero dependencies - lightweight and fast imports
  • Extensible architecture for custom validators
  • Framework agnostic - works with FastAPI, Django, Flask, etc.

Installation

pip install zodic

Requires Python 3.9+

Quick Start

import zodic as z

# Basic validation
name_schema = z.string().min(1).max(100)
name = name_schema.parse("Alice")  # Returns "Alice"

# Object validation  
user_schema = z.object({
    "name": z.string().min(1),
    "age": z.number().int().min(0).max(120),
    "email": z.string().optional(),
    "is_active": z.boolean().default(True)
})

# Parse and validate data
user_data = {
    "name": "Alice Johnson", 
    "age": 30,
    "email": "alice@example.com"
}

user = user_schema.parse(user_data)
# Returns: {"name": "Alice Johnson", "age": 30, "email": "alice@example.com", "is_active": True}

Documentation

Basic Types

import zodic as z

# Primitives
z.string()     # str
z.number()     # int | float
z.boolean()    # bool  
z.none()       # None

# String validation
z.string().min(5)              # Minimum length
z.string().max(100)            # Maximum length  
z.string().length(10)          # Exact length

# Number validation
z.number().int()               # Must be integer
z.number().positive()          # > 0
z.number().min(0).max(100)     # Range validation

# Collections
z.array(z.string())            # List[str]
z.object({"name": z.string()}) # Dict with typed fields

Advanced Features

# Optional and nullable
z.string().optional()          # str | None (can be missing)
z.string().nullable()          # str | None (can be null)
z.string().default("hello")    # Default value if missing

# Transformations
z.string().transform(str.upper)           # Transform after validation
z.number().transform(lambda x: x * 2)     # Custom transformations

# Custom validation
z.string().refine(
    lambda x: x.startswith("prefix_"),
    "Must start with 'prefix_'"
)

# Union types  
z.union([z.string(), z.number()])         # str | int | float

Error Handling

# Parse (throws ZodError on failure)
try:
    result = z.string().parse(123)
except z.ZodError as e:
    print(e)  # "Validation error at root: Expected string, received int"

# Safe parse (returns result object)
result = z.string().safe_parse(123)
if result["success"]:
    print(result["data"])     # Parsed value
else:
    print(result["error"])    # ZodError object

Real-World Example

# API request validation
api_schema = z.object({
    "user": z.object({
        "name": z.string().min(1).max(100),
        "email": z.string().refine(lambda x: "@" in x, "Invalid email"),
        "age": z.number().int().min(13).max(120)
    }),
    "preferences": z.object({
        "theme": z.union([z.literal("light"), z.literal("dark")]).default("light"),
        "notifications": z.boolean().default(True)
    }).optional(),
    "tags": z.array(z.string()).max(10).default([])
})

# Validate incoming request
try:
    validated_data = api_schema.parse(request_data)
    # Process validated data...
except z.ZodError as e:
    return {"error": str(e)}, 400

Why Zodic?

vs Pydantic

  • Lighter weight - Zero dependencies, 10x faster imports
  • Better API - More intuitive, chainable methods inspired by Zod
  • Better errors - Detailed path reporting for nested validation

vs marshmallow

  • Type safety - Full integration with Python type hints
  • Performance - Significantly faster validation
  • Modern API - Designed for Python 3.8+ with latest features

vs cerberus

  • Developer experience - Better error messages and IDE support
  • Composability - Easy to build complex schemas from simple parts
  • Type system - First-class type safety and inference

Performance

Zodic is designed for speed:

String validation:  2,000,000+ ops/sec
Number validation:  1,600,000+ ops/sec  
Object validation:    297,000+ ops/sec

Development

# Clone and install
git clone https://github.com/Seyamalam/zodic.git
cd zodic
pip install -e ".[dev]"

# Run tests
pytest

# Run type checking  
mypy zodic

# Format code
black zodic tests
isort zodic tests

Contributing

We welcome contributions! Please see our Contributing Guide for details.

License

MIT License - see LICENSE file for details.

Acknowledgments

Inspired by Zod - the amazing TypeScript validation library that set the standard for developer experience in schema validation.

Roadmap

  • v0.1: Core validation with basic types (CURRENT)
  • v0.2: Enhanced string/number validators (email, URL, regex, etc.)
  • v0.3: Advanced features (recursive schemas, conditional validation)
  • v0.4: Framework integrations (FastAPI, Django, Flask)
  • v1.0: Production ready with full Zod feature parity

Star us on GitHub if Zodic helps you build better Python applications!

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

zodic-0.1.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

zodic-0.1.0-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: zodic-0.1.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.4 Windows/11

File hashes

Hashes for zodic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cdb2a8b379d2cd33bd2ac989cee8d8509d1738933e95f8e6fb8487a63e36949a
MD5 8674a0be4b1e2d1b463f19aacbed68ed
BLAKE2b-256 c3acede636e2986e8b005196a58dc811b79641e0bfba697da96a5dcb2c26e800

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zodic-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.4 Windows/11

File hashes

Hashes for zodic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0c7c00c0061faab48e7424e6ced7862d4080205757bf72b1401db9124aa14543
MD5 0f40e5a3e82a245d39ab6ff8e1447824
BLAKE2b-256 8ad8f09d00b3277e323241406c7c8d2d01cbd8d79584d5595cfad871fdc0ed20

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