Skip to content

Commit

Permalink
fix: better validation to ingestion-config
Browse files Browse the repository at this point in the history
  • Loading branch information
almog8k committed Jul 28, 2024
1 parent 75fa2d1 commit 28db713
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/utils/configUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import { IngestionJobsConfig } from '../common/interfaces';
import { MissingConfigError } from '../common/errors';

function isStringEmpty(str: string): boolean {
return typeof str === 'string' && str.trim().length === 0;
}

export const getAvailableJobTypes = (ingestionConfig: IngestionJobsConfig): string[] => {
const jobTypes: string[] = [];
for (const jobKey in ingestionConfig) {
Expand All @@ -18,19 +22,21 @@ export const getAvailableJobTypes = (ingestionConfig: IngestionJobsConfig): stri

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const validateAndGetHandlersTokens = (ingestionConfig: IngestionJobsConfig) => {
if (ingestionConfig.new?.type === undefined) {
const { new: newJob, update: updateJob, swapUpdate: swapUpdateJob } = ingestionConfig;

if (newJob?.type === undefined || isStringEmpty(newJob.type)) {
throw new MissingConfigError('Missing "new-job" type configuration');
}
if (ingestionConfig.update?.type === undefined) {
if (updateJob?.type === undefined || isStringEmpty(updateJob.type)) {
throw new MissingConfigError('Missing "update-job" type configuration');
}
if (ingestionConfig.swapUpdate?.type === undefined) {
if (swapUpdateJob?.type === undefined || isStringEmpty(swapUpdateJob.type)) {
throw new MissingConfigError('Missing "swap-update-job" type configuration');
}

return {
Ingestion_New: ingestionConfig.new.type,
Ingestion_Update: ingestionConfig.update.type,
Ingestion_Swap_Update: ingestionConfig.swapUpdate.type,
Ingestion_New: newJob.type,
Ingestion_Update: updateJob.type,
Ingestion_Swap_Update: swapUpdateJob.type,
} as const satisfies Record<string, string>;
};
12 changes: 12 additions & 0 deletions tests/unit/utils/configUtil.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,17 @@ describe('configUtil', () => {

expect(action).toThrow(MissingConfigError);
});

it('should throw an error if one of the job types is empty', () => {
const ingestionConfig = {
new: { type: '', tasks: {} },
update: { type: 'Ingestion_Update', tasks: {} },
swapUpdate: { type: 'Ingestion_Swap_Update', tasks: {} },
};

const action = () => validateAndGetHandlersTokens(ingestionConfig);

expect(action).toThrow(MissingConfigError);
});
});
});

0 comments on commit 28db713

Please sign in to comment.