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 10-byte header length, parses the buffer, validates it at the decoder boundary, and rejects bad sockets immediately.

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 (10B) Check Magic (0xAF1C) and get Payload Length
3. Schema Check (Zod) Runs pre-registered type validator against raw payload
4. Handler Dispatch Emit 'message' with fully-typed Javascript object

Validation Code Example

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

Error Frame (MSG_ERROR) Response

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