Spec & Runtime Contracts

Published Updated
Zod validation schemas, e-commerce context contracts, and operator interfaces in @aircada/spec.

@aircada/spec defines runtime Zod validation schemas and TypeScript engine contracts shared across storefronts, Studio, CLI, and AIR Engine.

Runtime Zod Schemas & Engine Contracts | Aircada Docs#

Defines non-throwing Zod storefront schemas, ID coercion, and typed IOperator engine contracts.

Resilient Storefront Ingress Validation

AirEcommerceContextSchema parses runtime storefront globals (window.__AIRCADA_ECOMMERCE_CONTEXT__) injected by Liquid, Next.js, or headless APIs. Non-throwing union fallbacks (z.union([Schema, z.record(z.string(), z.never())])) ensure partial or incomplete storefront payloads degrade cleanly instead of crashing the 3D session.

Mixed Numeric & GID Coercion

Shopify and headless platforms mix numeric IDs (12345) with GraphQL Global IDs (gid://shopify/Product/12345). EcommerceProductSchema and EcommerceVariantSchema normalize identifiers with z.union([z.string(), z.number()]). OptionsRegistrySetSchema preserves custom storefront metadata via .catchall() so downstream pricing and fulfillment fields are never stripped.

Decoupled Engine Operator Contracts

Generated interfaces (IOperator, TimerOperator, OnStartOperator, OpenLinkOperator, ISceneObject) separate inputs (callable methods) from outputs (typed AirEvent<T> subscriptions returning an unsubscribe teardown function). Always invoke the returned teardown function on unmount to prevent listener leaks across scene reloads.

Core @aircada/spec Contracts
SymbolRoleNotes
AirEcommerceContextSchemaZod schemaSafe-parses window.AIRCADA_ECOMMERCE_CONTEXT without throwing.
EcommerceProductSchema / EcommerceVariantSchemaZod schemaAccepts both numeric IDs and Shopify GraphQL GID strings.
OptionsRegistrySetSchemaZod schemaMaps option sets to pricing deltas; preserves extra keys via catchall().
IOperator / ISceneObjectTypeScript interfaceAuto-generated contracts decoupling scripts from engine internals.
AirEvent<T>.listen(cb)SubscriptionReturns () => void unsubscribe callback for clean teardown.
parseContext.ts (typescript)
import { AirEcommerceContextSchema, type TimerOperator } from "@aircada/spec";

const parsed = AirEcommerceContextSchema.safeParse(window.__AIRCADA_ECOMMERCE_CONTEXT__);
if (parsed.success) {
  console.log("Storefront context:", parsed.data);
}

export function bindTimer(timer: TimerOperator) {
  const unsubscribe = timer.onComplete.listen(() => {
    console.log("Timer completed");
  });
  return unsubscribe;
}
Safely parse storefront e-commerce context and subscribe to an AirEvent output.