Skip to Content
Binal

Validation

Constraints and structured errors

from typing import Annotated

from binal import BinaryModel, MaxLength, Prefixed, Range, UInt16


class Packet(BinaryModel, byte_order="big"):
    port: Annotated[UInt16, Range(1, 65535)]
    payload: Annotated[bytes, Prefixed(UInt16), MaxLength(4096)]

Invalid values raise ValidationError with the field path, the expected constraint, and the received value. Truncated or trailing data raises DecodeError with the byte offset, expected size, and available size.