Skip to content

Automatic Schema Validation

AfterLink integrates schema validation directly into the protocol frame processing layer. By leveraging Zod schemas, you can prevent corrupt, malformed, or hostile payloads from exhausting application layer computing cycles.

Protocol-Level Shield

Typical websocket servers receive buffers, parse JSON string representations, and only then run validation in the handler. If a packet is malformed, your processor has already allocated CPU cycles and memory. AfterLink parses the 16-byte Protocol v2 header, validates priority bounds (0-7) and routing key lengths (0-255B), verifies payload length, validates schema boundaries, and rejects bad sockets immediately.

Protocol v2 Header Validation

Before attempting MessagePack payload deserialization or schema validation, AfterLink enforces strict binary header checks:

Header Field Validation Rule Failure Behavior & Error Code
Magic Identifier Must match 0xAF 0x1C Socket destroyed immediately; ERR_MAGIC_MISMATCH (100)
Protocol Version Version negotiation 0x01 or 0x02 Frame rejected; ERR_VERSION_UNSUPPORTED (101)
Priority Level Integer range 0 ≤ priority ≤ 7 Frame rejected; ERR_BAD_FRAME (400)
Routing Key Length Byte length 0 ≤ len ≤ 255 Frame rejected; ERR_BAD_FRAME (400)

Schema Registration

You register validation models for each of the specific message types on the server. Whenever a frame matching that type is received, the validation chain runs:

1. Frame Buffer Arrives Hex array stream parsed from socket
↓
2. Decode Header (16B) Check Magic (0xAF1C), Version Negotiation (v1/v2), Priority (0-7), Routing Key length, and Payload Length
↓
3. Schema Check (Zod / Pydantic) Runs pre-registered type validator against raw payload
↓
4. Handler Dispatch Emit route handler with fully-typed validated object

Validation Code Example (Node.js Zod)

Below is an implementation of how schemas are registered and handled inside AfterLink server instances:

Python & Dart Payload Validation

AfterLink v2 multi-language SDKs provide native payload validation integration using Pydantic in Python and typed decoders in Dart:

Python Pydantic Validation:

Dart Payload Validation:

Error Frame (MSG_ERROR) Response

When a schema check fails, the socket connection is not closed immediately. Instead, AfterLink encodes a structured MSG_ERROR (0x08) frame back to the client. The payload of this error frame contains a JSON-serialized details block: