Skip to content

Commit

Permalink
fix: Fix types and builder.build() return design
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvanasCry committed Jul 30, 2023
1 parent 96ae721 commit 904cacd
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 39 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ The library is a wrapper for the `Ajv` instance, which is in turn used in conjun
- Allows retrieval of `Ajv` validation errors for the payload data by pulling them up to the application level.
- Resolves the `TypeError` when passing an `Ajv` instance to the SchemaRegistry constructor options.

#### Bonus Features
- Saves you from having to handle the schema ID.

## Installation

Install the library:
Expand Down Expand Up @@ -53,7 +56,7 @@ const builder = new SchemaRegistryAjvBuilder({
},
});

const [ajvInstance, getErrors] = await builder.build();
const { ajvInstance, getSchemaId, getErrors } = await builder.build();

const schemaRegistry = new SchemaRegistry({
host: 'https://schema-registry.example.com:8081',
Expand All @@ -70,13 +73,13 @@ Somewhere in your code:
// Publishing to Kafka topic
try {
// To encode Kafka message value
const value = await schemaRegistry.encode(builder.getSchemaId(), event);
const value = await schemaRegistry.encode(getSchemaId(), event);

// ...
} catch (err) {
// To get Ajv validation errors
const validationErorrs = getErrors();

// ...
}
```
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"name": "Sylvanas Cry",
"email": "sylvanas@gmx.us"
},
"contributors": [{
"name": "Ilia Smirnov",
"email": "smirnov.i.o@yandex.ru"
}],
"keywords": [
"confluent schema registry",
"schema-registry",
Expand Down
28 changes: 14 additions & 14 deletions src/schema-registry-ajv-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ describe('SchemaRegistryAjvBuilder', () => {
createVersionsScope(200, [1, 2, 3], headers);
createSchemaScope(3, 200, schemaReply, headers);

const [ajv] = await builder.build();
const { ajvInstance } = await builder.build();

expect(ajv).toBeInstanceOf(SchemaRegistryAjv);
expect(ajv.addSchema(schema)).toBeInstanceOf(Ajv2020);
expect(typeof ajv.compile(schema)).toBe('function');
expect(ajvInstance).toBeInstanceOf(SchemaRegistryAjv);
expect(ajvInstance.addSchema(schema)).toBeInstanceOf(Ajv2020);
expect(typeof ajvInstance.compile(schema)).toBe('function');
});

it('it should provide SchemaRegistry comfortable Ajv instance', async () => {
Expand All @@ -90,10 +90,10 @@ describe('SchemaRegistryAjvBuilder', () => {
createVersionsScope(200, [1, 2, 3], headers);
createSchemaScope(3, 200, schemaReply, headers);

const [ajv] = await builder.build();
const { ajvInstance } = await builder.build();
const schemaRegistry = new SchemaRegistry({
host,
}, { [SchemaType.JSON]: { ajvInstance: ajv } });
}, { [SchemaType.JSON]: { ajvInstance } });

expect(schemaRegistry).toBeInstanceOf(SchemaRegistry);
});
Expand All @@ -113,8 +113,8 @@ describe('SchemaRegistryAjvBuilder', () => {
id: 1,
}, headers);

await builder.build();
expect(builder.getSchemaId()).toBe(1);
const { getSchemaId } = await builder.build();
expect(getSchemaId()).toBe(1);
});

it('should return a latest schema id', async () => {
Expand All @@ -123,8 +123,8 @@ describe('SchemaRegistryAjvBuilder', () => {
createVersionsScope(200, [1, 2, 3], headers);
createSchemaScope(3, 200, schemaReply, headers);

await builder.build();
expect(builder.getSchemaId()).toBe(3);
const { getSchemaId } = await builder.build();
expect(getSchemaId()).toBe(3);
});

it('should return a default latest schema id', async () => {
Expand All @@ -142,8 +142,8 @@ describe('SchemaRegistryAjvBuilder', () => {
id: 6,
}, headers);

await builder.build();
expect(builder.getSchemaId()).toBe(6);
const { getSchemaId } = await builder.build();
expect(getSchemaId()).toBe(6);
});

it('should return validation errors', async () => {
Expand All @@ -152,8 +152,8 @@ describe('SchemaRegistryAjvBuilder', () => {
createVersionsScope(200, [1, 2, 3], headers);
createSchemaScope(3, 200, schemaReply, headers);

const [ajv, getErrors] = await builder.build();
const validate = ajv.compile({});
const { ajvInstance, getErrors } = await builder.build();
const validate = ajvInstance.compile({});
const isValid = validate({
attribute: 'BROKEN',
custom: 'value',
Expand Down
23 changes: 7 additions & 16 deletions src/schema-registry-ajv-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import addFormats from 'ajv-formats';
import axios from 'axios';
import { SchemaRegistryAjvSchemaException, SchemaRegistryAjvVersionException } from './exceptions';
import { SchemaRegistryAjv } from './schema-registry-ajv';
import {
SchemaRegistryAjvBuilderBuildReturn,
SchemaRegistryAjvBuilderOptions,
} from './schema-registry-ajv-builder.types';
import { BuildReturnType, SchemaRegistryAjvBuilderOptions } from './schema-registry-ajv-builder.types';

export class SchemaRegistryAjvBuilder {
/**
Expand Down Expand Up @@ -40,7 +37,7 @@ export class SchemaRegistryAjvBuilder {
};
}

public async build(): Promise<SchemaRegistryAjvBuilderBuildReturn> {
public async build(): Promise<BuildReturnType> {
const { ajvClass, ajvFormats, schemaRegistry } = this.options;
const version = typeof schemaRegistry.version === 'number'
? schemaRegistry.version
Expand All @@ -61,17 +58,11 @@ export class SchemaRegistryAjvBuilder {
const validate = ajv.compile(schema);
this.validate = validate;

return [
new SchemaRegistryAjv(ajv, validate),
() => this.validate.errors,
];
}

/**
* Returns schema id.
*/
public getSchemaId(): number {
return this.schemaId;
return {
ajvInstance: new SchemaRegistryAjv(ajv, validate),
getErrors: () => this.validate.errors,
getSchemaId: () => this.schemaId,
};
}

private async getSchema(version: number): Promise<AnySchema> {
Expand Down
27 changes: 21 additions & 6 deletions src/schema-registry-ajv-builder.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,32 @@ export interface AjvCustomFormat {
format: Format;
}

export type GetErrorsFunction = () => ValidateFunction['errors'];
export type GetSchemaId = () => number;
export type GetErrors = () => ValidateFunction['errors'];

export type SchemaRegistryAjvBuilderBuildReturn = [
SchemaRegistryAjvInstance,
GetErrorsFunction,
];
export interface BuildReturnType {
/**
* SchemaRegistry constructor compatible Ajv instance.
*
* @see {import('@kafkajs/confluent-schema-registry/dist/@types').JsonOptions}
*/
ajvInstance: SchemaRegistryAjvInstance;

/**
* Returns schema id.
*/
getSchemaId: GetSchemaId;

/**
* Returns Ajv validation errors.
*/
getErrors: GetErrors;
}

export interface SchemaRegistryAjvBuilderSchemaRegistryOptions {
/**
* Schema Registry URI.
**
*
* @example https://schema-registry.example.com
* @example http://localhost:8081
* @example http://localhost:8081/
Expand Down

0 comments on commit 904cacd

Please sign in to comment.