Exceptions

valify defines its own exception hierarchy so you can catch errors precisely.

Hierarchy

Exception
└── ValifyError
    ├── ValidationError
    │   └── RequiredFieldError
    └── SchemaError

Usage

Catch a specific error:

from valify.exceptions import ValidationError

try:
    schema.validate(data)
except ValidationError as e:
    print(e.field)    # which field failed
    print(e.value)    # what value was rejected
    print(e.message)  # human readable message

Catch everything valify raises:

from valify.exceptions import ValifyError

try:
    schema.validate(data)
except ValifyError as e:
    print(f"valify error: {e}")

API reference

class valify.exceptions.ValifyError[source]

Bases: Exception

Base class for all valify exceptions

class valify.exceptions.ValidationError(message: str, *, field: str | None = None, value: Any = None)[source]

Bases: ValifyError

Raised when a value fails a validation rule.

message

Human-readable description of what failed.

Type:

str

field

The field name that failed. None if used outside a schema.

Type:

str

value

The actual value that was rejected.

Type:

object

class valify.exceptions.RequiredFieldError(field: str)[source]

Bases: ValidationError

Raised when a required field is missing from validation data.

class valify.exceptions.SchemaError(message: str)[source]

Bases: ValifyError

Raised when the schema definition itself is inavlid.