Skip to main content

headersMessage header models, with optional runtime validation of what arrives.

Headers

export default {
...,
generators: [
{
preset: 'headers',
outputPath: './src/headers',
serializationType: 'json',
includeValidation: true,
language: 'typescript',
}
]
};

headers preset is for generating models that represent typed models representing headers.

This is supported through the following inputs: asyncapi, openapi

It supports the following languages; typescript

Inputs

asyncapi

The headers preset with asyncapi input generates all the message headers for each channel in the AsyncAPI document.

The return type is a map of channels and the model that represent the headers.

openapi

The headers preset with openapi input generates all the headers for each path in the OpenAPI document.

The return type is a map of paths and the model that represent the headers.

Options

These are the available options for the headers generator;

OptionDefaultTypeDescription
id'headers-typescript'StringUnique identifier for this generator instance. The channels/client generators reference it as their headerGeneratorId.
dependencies[]String[]IDs of other generators that must run before this one.
outputPath'src/__gen__/headers'StringDirectory the generated header models are written to.
serializationType'json''json'Serialization format used by the generated models. Only json is supported.
includeValidationtrueBooleanInclude the built-in JSON Schema validate/createValidator methods. Requires ajv and ajv-formats (see Dependencies).

Typescript

Dependencies:

  • If validation enabled, ajv: ^8.17.1
  • If validation enabled, ajv-formats: ^3.0.1

Validation

Each generated class includes built-in JSON Schema validation capabilities through two static methods:

  • validate: Validates headers against the schema. Use this method when you want to validate data.
// Example
const result = UserSignedUpHeaders.validate({ data: headers });
if (!result.valid) {
console.error('Validation errors:', result.errors);
}
  • createValidator: Creates a reusable validator function. Use this when you need to validate multiple instances of the same type and want to avoid recreating the validator each time.
// Example
const validator = UserSignedUpHeaders.createValidator();
const result = UserSignedUpHeaders.validate({ data: headers, ajvValidatorFunction: validator });
if (!result.valid) {
console.error('Validation errors:', result.errors);
}

Both methods support custom Ajv instances and options for advanced validation scenarios.