Skip to content

Latest commit

 

History

History
77 lines (55 loc) · 1.68 KB

README.md

File metadata and controls

77 lines (55 loc) · 1.68 KB

Zod Event

DRY Castore EventType definition using zod.

📥 Installation

# npm
npm install @castore/zod-event

# yarn
yarn add @castore/zod-event

This package has @castore/core and zod (above v3) as peer dependencies, so you will have to install them as well:

# npm
npm install @castore/core zod

# yarn
yarn add @castore/core zod

👩‍💻 Usage

import z from 'zod';

import { ZodEventType } from '@castore/zod-event';

const userCreatedPayloadSchema = z.object({
  name: z.string(),
  age: z.number(),
});

const userCreatedMetadataSchema = z.object({
  invitedBy: z.string().optional(),
});

// 👇 generics are correctly inferred
const userCreatedEventType = new ZodEventType({
  type: 'USER_CREATED',
  payloadSchema: userCreatedPayloadSchema,
  metadataSchema: userCreatedMetadataSchema,
});

👇 Equivalent to:

import { EventType } from '@castore/core';

const userCreatedEventType = new EventType<
  'USER_CREATED',
  { name: string; age: number },
  { invitedBy?: string }
>({ type: 'USER_CREATED' });

⚙️ Properties & Methods

ZodEventType implements the EventType class and adds the following properties to it:

  • payloadSchema (?object): The event type payload zod schema
const payloadSchema = userCreatedEventType.payloadSchema;
// => userCreatedPayloadSchema
  • metadataSchema (?object): The event type metadata zod schema
const metadataSchema = userCreatedEventType.metadataSchema;
// => userCreatedMetadataSchema