@hamstore/event-type-zod
v3.1.0
Published
DRY Hamstore EventType definition using zod
Readme
Zod Event
[!WARNING] Deprecated: This package provides type inference only and does not perform runtime validation of event payloads or metadata. It is deprecated in favor of
@hamstore/event-type-standard-schema, which supports runtime validation via the Standard Schema interface (compatible with Zod, Valibot, ArkType, and more). This package will be removed in a future release.
DRY Hamstore EventType definition using zod.
📥 Installation
# npm
npm install @hamstore/event-type-zod
# pnpm
pnpm add @hamstore/event-type-zodThis package has @hamstore/core and zod (above v3) as peer dependencies, so you will have to install them as well:
# npm
npm install @hamstore/core zod
# pnpm
pnpm add @hamstore/core zod👩💻 Usage
import z from 'zod';
import { ZodEventType } from '@hamstore/event-type-zod';
const pokemonAppearedPayloadSchema = z.object({
name: z.string(),
level: z.number(),
});
const pokemonAppearedMetadataSchema = z.object({
trigger: z.enum(['random', 'scripted']).optional(),
});
// 👇 generics are correctly inferred
const pokemonAppearedEventType = new ZodEventType({
type: 'POKEMON_APPEARED',
payloadSchema: pokemonAppearedPayloadSchema,
metadataSchema: pokemonAppearedMetadataSchema,
});👇 Equivalent to:
import { EventType } from '@hamstore/core';
const pokemonAppearedEventType = new EventType<
'POKEMON_APPEARED',
{ name: string; level: number },
{ trigger?: 'random' | 'scripted' }
>({ type: 'POKEMON_APPEARED' });⚙️ Properties & Methods
ZodEventType implements the EventType class and adds the following properties to it:
- payloadSchema (?object): The event type payload zod schema
const payloadSchema = pokemonAppearedEventType.payloadSchema;
// => pokemonAppearedPayloadSchema- metadataSchema (?object): The event type metadata zod schema
const metadataSchema = pokemonAppearedEventType.metadataSchema;
// => pokemonAppearedMetadataSchema