Skip to Content
DocumentationData Type Validation

Data Type Validation

Overview

Ensuring accurate data types in event tracking is crucial for data reliability and meaningful analysis.
event-tracker provides built-in data type validation using Zod, a TypeScript-first schema validation library. This feature allows you to define schemas for your event parameters, ensuring that the data conforms to the expected structure before processing. This is particularly useful for maintaining consistency and preventing errors in event tracking.

Configuration

To configure data type validation in event-tracker, you need to define your schemas using Zod and pass them to the tracker configuration.

Options

  • schemas: A record of Zod schemas that define the structure of your event parameters.
  • onSchemaError: A callback function that is invoked when schema validation fails.
  • abortOnError: A boolean indicating whether to abort the event processing on validation error. false by default.

Example Flow

  1. Define Schemas: Use Zod to define the expected structure of your event parameters.

    import { z } from "zod"; const page_view = z.object({ title: z.string(), }); const click_button = z.object({ target: z.string(), });
  2. Configure Tracker: Pass the schemas to the createTracker function.

    import { createTracker } from "@offlegacy/event-tracker"; type schemas = { page_view; clic_button; }; const [Track] = createTracker<{}, {}, typeof schemas>({ schema: { schemas, onSchemaError: (error) => { console.error("Schema validation error:", error); }, abortOnError: true, }, // other configurations... });
  3. Validate Events: When events are tracked, they are validated against the defined schemas. It will also be validated in advance using TypeScript.

    <Track.PageView schema='page_view' params={{ title: "Home Page" }} /> <Track.Click schema='click_button' params={{ target: "submit-button" }} />

Important Notes

  • Zod schemas are by default validated with .parse().
  • We recommend defining schemas outside of createTracker function, then assigning typeof schemas in the third generic type argument of createTracker for smooth type inference.

We plan to support other schema validation libraries in the future.

Last updated on