Skip to main content

YuPy - Python Schema Validation Library

Dead simple object schema validation for Python

license pypi pypi-pre py-versions Test Ruff Mypy coverage downloads downloads/month Made in Ukraine

Inspired by yup js library

YuPy is a schema builder for runtime value parsing and validation. Define a schema, transform a value to match, assert the shape of an existing value, or both. YuPy schemas are extremely expressive and allow modeling complex, interdependent validations or value transformations.


๐Ÿ“‹ Table of Contents


๐Ÿ”ง Features

  • โœ… Schema-based validation: strings, numbers, arrays, dictionaries
  • ๐Ÿ” Type checking
  • โ“ Nullability control (None)
  • ๐Ÿ”„ Value transformation
  • ๐Ÿงช Custom validators
  • ๐Ÿงพ Detailed error reporting
  • ๐ŸŒ Locale support
  • ๐Ÿ”Œ Built-in adapters: default, required, immutable
  • ๐Ÿ“ Comparison and size constraints
  • ๐Ÿ” Mixed types and Union schema support

๐Ÿ“ฆ Installation

pip install yupy

๐Ÿš€ Usage

Basic validation

from yupy import string, number

string().min(3).max(10).validate("hello")  # โœ…
number().positive().integer().validate(42)  # โœ…

Nullability

string().nullable().validate(None)  # โœ…

Arrays

from yupy import array

array().of(string().min(2)).min(1).validate(["ok", "yes"])

Dictionaries (Mappings)

from yupy import mapping

user_schema = mapping().shape({
    "name": string().min(3),
    "age": number().ge(18)
})

Union

from yupy import union

union().one_of([string(), number()]).validate("hello")
union().one_of([string(), number()]).validate(10)

๐Ÿงฉ Adapters

required

from yupy import required

required(string().min(3)).validate("abc")

default

from yupy import default

default("N/A", string()).validate(None)  # โ†’ "N/A"

immutable

from yupy import immutable

immutable(string()).validate("data")  # -> creates deep copy

๐Ÿ“˜ API Reference

Base Schema

Inheritance: Schema (implements ISchema)

Base class for all schema types providing core validation functionality.

Method Description
nullable() -> Self Makes the schema accept None values
not_nullable(message: ErrorMessage = None) -> Self Explicitly disallows None values with custom message
test(func: ValidatorFunc) -> Self Adds a custom validation function
const(value: Any, message: ErrorMessage = None) -> Self Validates that the value equals a constant
transform(func: TransformFunc) -> Self Adds a transformation function
validate(value: Any, abort_early: bool = True, path: str = "~") -> Any Validates the value against the schema

Sized Schema

Inheritance: Schema โ†’ SizedSchema (implements ISizedSchema)

Provides size-based validation methods for sequences and collections.

Method Description
length(limit: int, message: ErrorMessage = None) -> Self Validates exact length
min(limit: int, message: ErrorMessage = None) -> Self Validates minimum length
max(limit: int, message: ErrorMessage = None) -> Self Validates maximum length

Comparable Schema

Inheritance: Schema โ†’ ComparableSchema (implements IComparableSchema)

Provides comparison-based validation methods.

Method Description
le(limit: Any, message: ErrorMessage = None) -> Self Validates value โ‰ค limit
ge(limit: Any, message: ErrorMessage = None) -> Self Validates value โ‰ฅ limit
lt(limit: Any, message: ErrorMessage = None) -> Self Validates value < limit
gt(limit: Any, message: ErrorMessage = None) -> Self Validates value > limit
eq(value: Any, message: ErrorMessage = None) -> Self Validates value equals specified value
ne(value: Any, message: ErrorMessage = None) -> Self Validates value not equals specified value

String Schema

Inheritance: Schema โ†’ SizedSchema, ComparableSchema, EqualityComparableSchema โ†’ StringSchema

Validates string values with text-specific methods.

Method Description
email(message: ErrorMessage = None) -> Self Validates email format
url(message: ErrorMessage = None) -> Self Validates URL format
uuid(message: ErrorMessage = None) -> Self Validates UUID format
matches(regex: re.Pattern, message: ErrorMessage = None, exclude_empty: bool = False) -> Self Validates against regex pattern
lowercase(message: ErrorMessage = None) -> Self Validates string is lowercase
uppercase(message: ErrorMessage = None) -> Self Validates string is uppercase
ensure() -> Self Transforms empty/null values to empty string

Number Schema

Inheritance: Schema โ†’ ComparableSchema, EqualityComparableSchema โ†’ NumberSchema

Validates numeric values (int, float) with number-specific methods.

Method Description
positive(message: ErrorMessage = None) -> Self Validates number > 0
negative(message: ErrorMessage = None) -> Self Validates number < 0
integer(message: ErrorMessage = None) -> Self Validates number is integer (no decimals)
multiple_of(multiplier: Union[int, float], message: ErrorMessage = None) -> Self Validates number is multiple of specified value

Array Schema

Inheritance: Schema โ†’ SizedSchema, ComparableSchema, EqualityComparableSchema โ†’ ArraySchema

Validates list and tuple values.

Method Description
of(schema: Union[ISchema, ISchemaAdapter], message: ErrorMessage = None) -> Self Validates all array elements against schema

Mapping Schema

Inheritance: Schema โ†’ EqualityComparableSchema โ†’ MappingSchema

Validates dictionary/mapping values with object shape validation.

Method Description
shape(fields: Dict[str, Union[ISchema, ISchemaAdapter]]) -> Self Defines the expected shape/structure
strict(is_strict: bool = True, message: ErrorMessage = None) -> Self Disallows unknown keys when True

Mixed Schema

Inheritance: Schema โ†’ EqualityComparableSchema โ†’ MixedSchema

Validates values of any type with flexible type checking.

Method Description
of(type_or_types: _SchemaExpectedType, message: ErrorMessage = None) -> Self Validates value is of specified type(s)
one_of(items: Iterable, message: ErrorMessage = None) -> Self Validates value is one of the specified items

Union Schema

Inheritance: Schema โ†’ EqualityComparableSchema โ†’ UnionSchema

Validates values that can match one of multiple schemas.

Method Description
one_of(options: list[Union[ISchema, ISchemaAdapter]], message: ErrorMessage = None) -> Self Validates value matches at least one of the provided schemas

๐Ÿ›  Extending

Custom Validator

from yupy import string, ValidationError


def is_palindrome(value):
    if value != value[::-1]:
        raise ValidationError("Not a palindrome")


string().test(is_palindrome).validate("madam")

Custom Adapter

from yupy import SchemaAdapter, string


class CustomAdapter(SchemaAdapter):
    def validate(self, value, abort_early=True, path="~"):
        # Custom logic before validation
        result = super().validate(value, abort_early, path)
        # Custom logic after validation
        return result


# Usage
custom = CustomAdapter(string().min(3))
custom.validate("hello")

โœ… Running Tests

pytest

๐Ÿค Contributing

Contributions are welcome! Please open issues or submit pull requests.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“„ License

MIT License
Copyright (c) YuPy Contributors

Download files

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

Source Distribution

yupy-0.2.0.tar.gz (98.1 kB view details)

Uploaded Source

Built Distribution

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

yupy-0.2.0-py3-none-any.whl (36.7 kB view details)

Uploaded Python 3

File details

Details for the file yupy-0.2.0.tar.gz.

File metadata

  • Download URL: yupy-0.2.0.tar.gz
  • Upload date:
  • Size: 98.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for yupy-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2cb50a3b95c21c8457cd8f388ddd8b7fed85d6fa3033d874e947b14e5fc69eb4
MD5 d40858bf313a5c9f79c6e2b42be5855e
BLAKE2b-256 90103746114a0904cee22ba7f0482b1074e8d6bde34177da15425bde35949b86

See more details on using hashes here.

File details

Details for the file yupy-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: yupy-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 36.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for yupy-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca9981415381ff3dbfeb02c216a7d274b00ac39ddbd4cac10c30d698d040bc17
MD5 8fa1d646878f8ef7d967e0ea0ea4daa6
BLAKE2b-256 fc4f1c44e5d5c1009ffaff122ea3689a0ff6952bf98a430052b360003c343e93

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 Sentry Error logging StatusPage Status page