2.3.1 ā€¢ Published 17 days ago

@castore/event-type-json-schema v2.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
17 days ago

JSON Schema Event

DRY Castore EventType definition using JSON Schemas and json-schema-to-ts

šŸ“„ Installation

# npm
npm install @castore/event-type-json-schema

# yarn
yarn add @castore/event-type-json-schema

This package has @castore/core and json-schema-to-ts (above v2) as peer dependencies, so you will have to install them as well:

# npm
npm install @castore/core json-schema-to-ts

# yarn
yarn add @castore/core json-schema-to-ts

šŸ‘©ā€šŸ’» Usage

import { JSONSchemaEventType } from '@castore/event-type-json-schema';

const pokemonAppearedPayloadSchema = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    level: { type: 'integer' },
  },
  required: ['name', 'level'],
  additionalProperties: false,
} as const; // šŸ‘ˆ Don't forget the "as const" statement
// (Cf json-schema-to-ts documentation)

const pokemonAppearedMetadataSchema = {
  type: 'object',
  properties: {
    trigger: { enum: ['random', 'scripted'] },
  },
  additionalProperties: false,
} as const;

// šŸ‘‡ generics are correctly inferred
const pokemonAppearedEventType = new JSONSchemaEventType({
  type: 'POKEMON_APPEARED',
  payloadSchema: pokemonAppearedPayloadSchema,
  metadataSchema: pokemonAppearedMetadataSchema,
});

šŸ‘‡ Equivalent to:

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

const pokemonAppearedEventType = new EventType<
  'POKEMON_APPEARED',
  { name: string; level: number },
  { trigger?: 'random' | 'scripted' }
>({ type: 'POKEMON_APPEARED' });

āš™ļø Properties & Methods

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

  • payloadSchema (?object): The event type payload JSON schema
const payloadSchema = pokemonAppearedEventType.payloadSchema;
// => pokemonAppearedPayloadSchema
  • metadataSchema (?object): The event type metadata JSON schema
const metadataSchema = pokemonAppearedEventType.metadataSchema;
// => pokemonAppearedMetadataSchema
2.3.1

17 days ago

2.3.0

2 months ago

2.2.0

4 months ago

2.1.0

6 months ago

2.0.0

7 months ago