home / skills / amnadtaowsoam / cerebraskills / event-contract-generator

This skill generates consistent event contracts including schemas, types, validators, and documentation to ensure producers and consumers share a common format.

npx playbooks add skill amnadtaowsoam/cerebraskills --skill event-contract-generator

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.2 KB
---
name: Event Contract Generator
description: Generator for creating event schemas, types, validators, and documentation for event-driven architecture
---

# Event Contract Generator

## Overview

Automatically generates event contracts (schemas, types, validators) so that producers and consumers use the same event format.

## Why This Matters

- **Type safety**: TypeScript types from schema
- **Validation**: Auto-validate events
- **Documentation**: Auto-generated event catalog
- **Versioning**: Track schema changes

---

## Quick Start

```bash
# Generate event contract
npx generate-event-contract user.created

# Output:
events/user.created/
├── schema.json          # JSON Schema
├── types.ts             # TypeScript types
├── validator.ts         # Validation functions
└── examples.json        # Example events
```

---

## Generated Files

### Schema (JSON Schema)
```json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "UserCreated",
  "type": "object",
  "required": ["eventId", "eventType", "timestamp", "data"],
  "properties": {
    "eventId": { "type": "string", "format": "uuid" },
    "eventType": { "const": "user.created" },
    "timestamp": { "type": "string", "format": "date-time" },
    "data": {
      "type": "object",
      "required": ["userId", "email"],
      "properties": {
        "userId": { "type": "string" },
        "email": { "type": "string", "format": "email" },
        "name": { "type": "string" }
      }
    }
  }
}
```

### TypeScript Types
```typescript
// types.ts (auto-generated)
export interface UserCreatedEvent {
  eventId: string;
  eventType: 'user.created';
  timestamp: string;
  data: {
    userId: string;
    email: string;
    name?: string;
  };
}
```

### Validator
```typescript
// validator.ts (auto-generated)
import Ajv from 'ajv';
import schema from './schema.json';

const ajv = new Ajv();
const validate = ajv.compile(schema);

export function validateUserCreatedEvent(event: unknown): event is UserCreatedEvent {
  return validate(event) as boolean;
}
```

---

## Event Catalog

### Auto-generated Documentation
```markdown
# Event Catalog

## user.created
**Version:** 1.0.0
**Producer:** user-service
**Consumers:** email-service, analytics-service

### Schema
- eventId: UUID (required)
- eventType: "user.created" (required)
- timestamp: ISO 8601 (required)
- data.userId: string (required)
- data.email: email format (required)
- data.name: string (optional)

### Example
```json
{
  "eventId": "123e4567-e89b-12d3-a456-426614174000",
  "eventType": "user.created",
  "timestamp": "2024-01-16T12:00:00Z",
  "data": {
    "userId": "user_123",
    "email": "[email protected]",
    "name": "Test User"
  }
}
```
```

---

## Versioning

```bash
# Create new version
npx generate-event-contract user.created --version 2.0.0

# Output:
events/user.created/
├── v1/
│   ├── schema.json
│   └── types.ts
└── v2/
    ├── schema.json      # New version
    └── types.ts
```

---

## Summary

**Event Contract Generator:** Automatically generates event schemas

**Generated:**
- JSON Schema
- TypeScript types
- Validators
- Documentation
- Examples

**Benefits:**
- Type safety
- Auto-validation
- Version control
- Event catalog

Overview

This skill generates event contracts for event-driven systems, producing JSON Schemas, TypeScript types, validators, examples, and documentation. It ensures producers and consumers share a single source of truth for event shapes and supports versioned contracts for safe evolution. The output is ready to use in build pipelines and service repositories.

How this skill works

Provide an event identifier and optional version, and the generator emits a folder with schema.json, types.ts, validator.ts, and examples.json. It creates JSON Schema compliant definitions, derives TypeScript types, compiles runtime validators (e.g., Ajv), and produces human-readable event catalog entries. Versioning organizes historical schemas into subfolders to maintain compatibility and auditability.

When to use it

  • Establishing a single source of truth for event formats across services.
  • Adding runtime validation to event producers and consumers.
  • Generating TypeScript types to enable compile-time type safety.
  • Documenting event catalogs for cross-team discoverability.
  • Creating new schema versions while preserving old contracts.

Best practices

  • Define minimal required fields (eventId, eventType, timestamp, data) consistently across events.
  • Keep event data payloads focused and additive; prefer new optional fields over breaking changes.
  • Store generated contracts alongside service code and include them in CI to catch breaking changes early.
  • Publish an event catalog entry for each event version with examples and producer/consumer metadata.
  • Automate validator usage in both producers and consumers to avoid silent contract drift.

Example use cases

  • Create a user.created contract that produces schema.json, types.ts, validator.ts, and example events for onboarding flows.
  • Introduce v2 of an order.updated event and keep v1 intact for legacy consumers while migrating new services.
  • Integrate generated TypeScript types into a frontend consumer to guarantee correct event handling at build time.
  • Run generated validators in a message gateway to reject malformed events before they reach downstream services.
  • Generate a central event catalog to onboard new teams and document producers, consumers, and field-level expectations.

FAQ

Can I generate multiple versions of the same event?

Yes. The generator places each version in its own subfolder (e.g., v1, v2) so services can reference the appropriate contract.

Which runtime validator is used?

The default output includes an Ajv-based validator, but the schemas are standard JSON Schema and can be used with other validation libraries.