Skip to content

Schemas

Valibot schemas are used throughout SafeCall for runtime validation of external data. The shared schemas are defined in lib/src/schemas.ts.

MQTT Schemas

Beacon Advertisement Schemas

SchemaMessage TypeFields
MqttButtonAdv1Schemaadv1Battery, temperature, humidity, button state, alarm counter
MqttButtonAdv4Schemaadv4RSSI (signal strength), gateway MAC
MqttButtonAdv8Schemaadv8Extended sensor data

Beacon Message Schema

MqttBeaconMessageSchema wraps beacon advertisements with metadata:

  • mac — Beacon MAC address
  • type — Advertisement type (1, 4, or 8)
  • data — The advertisement payload

Gateway Schemas

SchemaPurpose
MqttGatewayAliveSchemaGateway heartbeat (IP, uptime, firmware)
MqttGatewayAdvDataSchemaGateway-forwarded beacon advertisements
MqttGatewayMessageSchemaTop-level gateway message wrapper

KISS Schemas

SchemaPurpose
KissLoginResponseSchemaResponse from KISS API login

LLD Schemas (Zabbix)

SchemaPurpose
LLDBeaconSchemaZabbix Low-Level Discovery beacon format
LLDGatewaySchemaZabbix Low-Level Discovery gateway format

WebSocket Schemas

SchemaPurpose
HttpWsMessageSchemaGeneral WebSocket message (type + payload)
HttpWsMessageTrackSchemaTrack/debug subscription (type + mac)
HttpWsMessageOverviewSchemaOverview subscription (type + map_id)
HttpWsMessageBeaconTestSchemaTest beacon simulation message

Web Frontend Contracts

The web frontend defines its own schemas in web/src/lib/contracts.ts. These mirror the server's API contracts and are used for client-side validation:

  • Entity schemas: UserSchema, BeaconSchema, GatewaySchema, MapSchema, FloorSchema, LocationSchema, LogSchema, EventSchema, NodeSchema, EdgeSchema
  • Request schemas: LoginRequestSchema, BeaconCreateRequestSchema, FloorCreateRequestSchema, etc.
  • Response schemas: LoginResponseSchema, MapsListResponseSchema, FloorGraphResponseSchema, etc.

Server Query Schemas

The server defines additional Valibot schemas in server/src/schemas.ts for validating database query results:

  • QueryResultUserSchema, QueryResultBeaconSchema, QueryResultLocationSchema
  • QueryResultGatewaySchema, QueryResultMapSchema, QueryResultFloorSchema
  • QueryResultZoneSchema, QueryResultNodeSchema, QueryResultEdgeSchema
  • And more (see Types for the complete list)

Validation Pattern

All schemas use valibot with a consistent pattern:

typescript
import * as v from "valibot";

// Define schema
const MySchema = v.object({
  id: v.number(),
  name: v.string(),
  active: v.boolean(),
});

// Validate data
const result = v.parse(MySchema, data);

// Infer TypeScript type
type MyType = v.InferOutput<typeof MySchema>;