Zodiac API Documentation - v0.0.3
    Preparing search index...

    Function defineMessage

    • Creates a type-safe message definition with validation schema.

      This is the core function for defining message types in Zodiac. It creates a message definition that includes both the data schema and the complete message schema with metadata fields.

      Type Parameters

      • T

        The TypeScript type inferred from the Zod schema

      Parameters

      • id: string

        Unique identifier for this message type

      • dataSchema: ZodType<T>

        Zod schema for validating message data

      Returns MessageDefinition<T>

      A message definition object with type information and schemas

      import { defineMessage, z } from '@xtr-dev/zodiac';

      // Define a user message type
      const userMessage = defineMessage('user-message', z.object({
      name: z.string().min(1),
      message: z.string().max(500),
      timestamp: z.number().optional()
      }));

      // The returned definition has full type inference
      channel.on(userMessage, (data) => {
      // data.name is string
      // data.message is string
      // data.timestamp is number | undefined
      });