---
source: crates/normalize-typegen/tests/codegen.rs
assertion_line: 305
expression: output
---
// Auto-generated by normalize-typegen
import { z } from "zod";

/** A domain event with discriminated payload */
export const EventSchema = z.discriminatedUnion("type", [
  z.object({
    type: z.literal("user.created"),
    payload: UserCreatedPayloadSchema,
    timestamp: TimestampSchema,
  }),
  z.object({
    type: z.literal("user.deleted"),
    payload: UserDeletedPayloadSchema,
    timestamp: TimestampSchema,
  }),
  z.object({
    type: z.literal("order.placed"),
    payload: OrderPlacedPayloadSchema,
    timestamp: TimestampSchema,
  }),
]);

export type Event = z.infer<typeof EventSchema>;

/** An item in an order */
export const OrderItemSchema = z.object({
  price: z.number(),
  productId: z.string(),
  quantity: z.number().int(),
});

export type OrderItem = z.infer<typeof OrderItemSchema>;

/** Payload for order placement events */
export const OrderPlacedPayloadSchema = z.object({
  items: z.array(OrderItemSchema),
  orderId: z.string(),
  total: z.number(),
  userId: z.string(),
});

export type OrderPlacedPayload = z.infer<typeof OrderPlacedPayloadSchema>;

/** A result type that can be success or error */
export const ResultSchema = z.discriminatedUnion("status", [
  z.object({
    status: z.literal("success"),
    data: z.unknown(),
  }),
  z.object({
    status: z.literal("error"),
    error: z.unknown(),
  }),
]);

export type Result = z.infer<typeof ResultSchema>;

/** ISO 8601 timestamp */
export const TimestampSchema = z.string();

export type Timestamp = z.infer<typeof TimestampSchema>;

/** Payload for user creation events */
export const UserCreatedPayloadSchema = z.object({
  email: z.string(),
  name: z.string(),
  userId: z.string(),
});

export type UserCreatedPayload = z.infer<typeof UserCreatedPayloadSchema>;

/** Payload for user deletion events */
export const UserDeletedPayloadSchema = z.object({
  reason: z.string().optional(),
  userId: z.string(),
});

export type UserDeletedPayload = z.infer<typeof UserDeletedPayloadSchema>;
