Skip to content

Commit

Permalink
fix(api): pick the digest schema for the framework validation
Browse files Browse the repository at this point in the history
  • Loading branch information
LetItRock committed Dec 23, 2024
1 parent 64a06f1 commit e50d923
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { workflow } from '@novu/framework/express';
import { ActionStep, ChannelStep, JsonSchema, Step, StepOptions, StepOutput, Workflow } from '@novu/framework/internal';
import { NotificationStepEntity, NotificationTemplateEntity, NotificationTemplateRepository } from '@novu/dal';
import { StepTypeEnum } from '@novu/shared';
import { JSONSchemaDefinition, JSONSchemaDto, StepTypeEnum } from '@novu/shared';
import { Instrument, InstrumentUsecase, PinoLogger } from '@novu/application-generic';
import { AdditionalOperation, RulesLogic } from 'json-logic-js';
import _ from 'lodash';
Expand All @@ -18,6 +18,7 @@ import {
import { DelayOutputRendererUsecase } from '../output-renderers/delay-output-renderer.usecase';
import { DigestOutputRendererUsecase } from '../output-renderers/digest-output-renderer.usecase';
import { evaluateRules } from '../../../shared/services/query-parser/query-parser.service';
import { isMatchingJsonSchema } from '../../../workflows-v2/util/jsonToSchema';

const LOG_CONTEXT = 'ConstructFrameworkWorkflow';

Expand Down Expand Up @@ -183,8 +184,25 @@ export class ConstructFrameworkWorkflow {
staticStep: NotificationStepEntity,
fullPayloadForRender: FullPayloadForRender
): Required<Parameters<ActionStep>[2]> {
const stepOptions = this.constructCommonStepOptions(staticStep, fullPayloadForRender);

let controlSchema = stepOptions.controlSchema as JSONSchemaDefinition;
const stepType = staticStep.template!.type;

/*
* because of the known AJV issue with anyOf, we need to find the first schema that matches the control values
* ref: https://ajv.js.org/guide/modifying-data.html#assigning-defaults
*/
if (stepType === StepTypeEnum.DIGEST && typeof controlSchema === 'object' && controlSchema.anyOf) {
const fistSchemaMatch = controlSchema.anyOf.find((item) => {
return isMatchingJsonSchema(item, staticStep.controlVariables);
});
controlSchema = fistSchemaMatch ?? controlSchema.anyOf[0];
}

return {
...this.constructCommonStepOptions(staticStep, fullPayloadForRender),
...stepOptions,
controlSchema: controlSchema as JsonSchema,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ import {
UiSchema,
UiSchemaGroupEnum,
} from '@novu/shared';
import { skipStepUiSchema, skipZodSchema } from './skip-control.schema';
import { skipStepUiSchema } from './skip-control.schema';

const digestRegularControlZodSchema = z
.object({
skip: skipZodSchema,
skip: z.object({}).catchall(z.unknown()).optional(),
amount: z.union([z.number().min(1), z.string().min(1)]),
unit: z.nativeEnum(TimeUnitEnum),
unit: z.nativeEnum(TimeUnitEnum).default(TimeUnitEnum.SECONDS),
digestKey: z.string().optional(),
lookBackWindow: z
.object({
amount: z.number().min(1),
unit: z.nativeEnum(TimeUnitEnum),
unit: z.nativeEnum(TimeUnitEnum).default(TimeUnitEnum.SECONDS),
})
.strict()
.optional(),
})
.strict();
const digestTimedControlZodSchema = z
.object({
skip: skipZodSchema,
skip: z.object({}).catchall(z.unknown()).optional(),
cron: z.string().min(1),
digestKey: z.string().optional(),
})
Expand Down

0 comments on commit e50d923

Please sign in to comment.