Skip to content

Commit

Permalink
chore(api): Apply minor refactoring to empty JSON Schema generation
Browse files Browse the repository at this point in the history
  • Loading branch information
SokratisVidros committed Dec 23, 2024
1 parent 49ad280 commit 21e2191
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Instrument, InstrumentUsecase } from '@novu/application-generic';
import { flattenObjectValues } from '../../util/utils';
import { pathsToObject } from '../../util/path-to-object';
import { extractLiquidTemplateVariables } from '../../util/template-parser/liquid-parser';
import { convertJsonToSchemaWithDefaults } from '../../util/jsonToSchema';
import { convertJsonToSchemaWithDefaults, emptyJsonSchema } from '../../util/jsonToSchema';
import { BuildPayloadSchemaCommand } from './build-payload-schema.command';
import { transformMailyContentToLiquid } from '../generate-preview/transform-maily-content-to-liquid';
import { isStringTipTapNode } from '../../util/tip-tap.util';
Expand All @@ -19,20 +19,12 @@ export class BuildPayloadSchema {
const controlValues = await this.buildControlValues(command);

if (!controlValues.length) {
return {
type: 'object',
properties: {},
additionalProperties: true,
};
return emptyJsonSchema();
}

const templateVars = await this.processControlValues(controlValues);
if (templateVars.length === 0) {
return {
type: 'object',
properties: {},
additionalProperties: true,
};
return emptyJsonSchema();
}

const variablesExample = pathsToObject(templateVars, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BuildAvailableVariableSchemaCommand } from './build-available-variable-
import { parsePayloadSchema } from '../../shared/parse-payload-schema';
import { BuildPayloadSchemaCommand } from '../build-payload-schema/build-payload-schema.command';
import { BuildPayloadSchema } from '../build-payload-schema/build-payload-schema.usecase';
import { emptyJsonSchema } from '../../util/jsonToSchema';

@Injectable()
export class BuildAvailableVariableSchemaUsecase {
Expand Down Expand Up @@ -62,13 +63,7 @@ export class BuildAvailableVariableSchemaUsecase {
command: BuildAvailableVariableSchemaCommand
): Promise<JSONSchemaDto> {
if (workflow.payloadSchema) {
return (
parsePayloadSchema(workflow.payloadSchema, { safe: true }) || {
type: 'object',
properties: {},
additionalProperties: true,
}
);
return parsePayloadSchema(workflow.payloadSchema, { safe: true }) || emptyJsonSchema();
}

return this.buildPayloadSchema.execute(
Expand Down
12 changes: 8 additions & 4 deletions apps/api/src/app/workflows-v2/util/jsonToSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { JSONSchemaDefinition, JSONSchemaDto } from '@novu/shared';

export function emptyJsonSchema(): JSONSchemaDto {
return {
type: 'object',
properties: {},
additionalProperties: true,
};
}

export function convertJsonToSchemaWithDefaults(unknownObject?: Record<string, unknown>) {
if (!unknownObject) {
return {};
Expand All @@ -8,10 +16,6 @@ export function convertJsonToSchemaWithDefaults(unknownObject?: Record<string, u
return generateJsonSchema(unknownObject) as unknown as JSONSchemaDto;
}

function isAJsonSchemaDto(schema: JSONSchemaDto | boolean): schema is JSONSchemaDto {
return typeof schema !== 'boolean';
}

function generateJsonSchema(jsonObject: Record<string, unknown>): JSONSchemaDto {
const schema: JSONSchemaDto = {
type: 'object',
Expand Down

0 comments on commit 21e2191

Please sign in to comment.