@castore/json-schema-event
v1.25.3
Published
DRY Castore EventType definition using JSON Schemas and json-schema-to-ts
Downloads
1,292
Readme
JSON Schema Event
DRY Castore EventType
definition using JSON Schemas and json-schema-to-ts
📥 Installation
# npm
npm install @castore/json-schema-event
# yarn
yarn add @castore/json-schema-event
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/json-schema-event';
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