Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add support for nested validations #6788

Draft
wants to merge 5 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions apps/api/src/app/shared/commands/base.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@ import { validateSync } from 'class-validator';
import { addBreadcrumb } from '@sentry/node';
import { BadRequestException } from '@nestjs/common';

interface IConstraint {
path: string[];
constraint: string[];
}

function extractConstraints(obj: any, path: string[] = []): IConstraint[] {
const constraints: IConstraint[] = [];

for (const key in obj) {
if (obj[key] && typeof obj[key] === 'object') {
const currentLocation = obj[key]?.property;
const newPath = [...path, currentLocation];
if (obj[key].constraints) {
constraints.push({ path: [...newPath, key], constraint: Object.values(obj[key].constraints) });
}
constraints.push(...extractConstraints(obj[key].children, newPath));
}
}

return constraints;
}

export abstract class BaseCommand {
static create<T extends BaseCommand>(this: new (...args: unknown[]) => T, data: T): T {
const convertedObject = plainToInstance<T, unknown>(this, {
Expand All @@ -11,13 +33,7 @@ export abstract class BaseCommand {

const errors = validateSync(convertedObject as unknown as object);
if (errors?.length) {
const mappedErrors = errors.flatMap((item) => {
if (!item.constraints) {
return [];
}

return Object.values(item.constraints);
});
const mappedErrors = extractConstraints(errors);

if (mappedErrors.length > 0) {
addBreadcrumb({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { EnvironmentWithUserObjectCommand } from '@novu/application-generic';
import { CreateWorkflowDto, IdentifierOrInternalId, UpdateWorkflowDto } from '@novu/shared';
import { IsDefined, IsOptional, IsString } from 'class-validator';

export class UpsertWorkflowCommand extends EnvironmentWithUserObjectCommand {
@IsOptional()
@IsString()
identifierOrInternalId?: IdentifierOrInternalId;

@IsDefined()
workflowDto: CreateWorkflowDto | UpdateWorkflowDto;
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export class UpsertWorkflowUseCase {
description: workflowDto.description || '',
tags: workflowDto.tags || [],
critical: false,
triggerIdentifier: slugifyName(workflowDto.name),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug fix

triggerIdentifier: slugifyName(workflowDto.workflowId),
};
}

Expand All @@ -229,7 +229,7 @@ export class UpsertWorkflowUseCase {
description: workflowDto.description,
tags: workflowDto.tags,
active: workflowDto.active ?? true,
workflowId: workflowDto.workflowId,
workflowId: slugifyName(workflowDto.workflowId),
};
}

Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'reflect-metadata';

import { bootstrap } from './bootstrap';

bootstrap();
13 changes: 11 additions & 2 deletions packages/shared/src/dto/workflows/create-workflow-dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { Type } from 'class-transformer';
import { IsEnum, IsNotEmpty, ValidateNested, IsDefined, IsOptional } from 'class-validator';

import { PreferencesRequestDto, StepCreateDto, WorkflowCommonsFields } from './workflow-commons-fields';
import { WorkflowCreationSourceEnum } from '../../types';

export type CreateWorkflowDto = WorkflowCommonsFields & {
export class CreateWorkflowDto extends WorkflowCommonsFields {
@ValidateNested({ each: true })
@Type(() => StepCreateDto)
@IsDefined()
steps: StepCreateDto[];

@IsEnum(WorkflowCreationSourceEnum)
@IsNotEmpty()
__source: WorkflowCreationSourceEnum;

@IsOptional()
preferences?: PreferencesRequestDto;
};
}
18 changes: 15 additions & 3 deletions packages/shared/src/dto/workflows/update-workflow-dto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { PreferencesRequestDto, StepCreateDto, StepUpdateDto, WorkflowCommonsFields } from './workflow-commons-fields';
import { IsDefined, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';

export type UpdateWorkflowDto = WorkflowCommonsFields & {
import {
PreferencesRequestDto,
StepCreateDto,
StepDto,
StepUpdateDto,
WorkflowCommonsFields,
} from './workflow-commons-fields';

export class UpdateWorkflowDto extends WorkflowCommonsFields {
updatedAt: string;

@Type(() => StepDto)
@ValidateNested({ each: true })
steps: (StepCreateDto | StepUpdateDto)[];

@IsDefined()
preferences: PreferencesRequestDto;
};
}
41 changes: 22 additions & 19 deletions packages/shared/src/dto/workflows/workflow-commons-fields.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsArray, IsBoolean, IsDefined, IsObject, IsOptional, IsString } from 'class-validator';
import { IsArray, IsBoolean, IsEnum, IsNotEmpty, IsObject, IsOptional, IsString } from 'class-validator';

import { JSONSchema } from 'json-schema-to-ts';
import { WorkflowResponseDto } from './workflow-response-dto';
Expand All @@ -10,18 +10,34 @@ export class ControlsSchema {
schema: JSONSchema;
}

export class StepDto {
@IsString()
@IsNotEmpty()
name: string;

@IsEnum(StepTypeEnum)
@IsNotEmpty()
type: StepTypeEnum;

@IsObject()
@IsOptional()
controlValues?: Record<string, unknown>;
}

export type StepResponseDto = StepDto & {
_id: string;
slug: Slug;
stepId: string;
controls: ControlsSchema;
};

export type StepUpdateDto = StepDto & {
export class StepUpdateDto extends StepDto {
@IsString()
@IsNotEmpty()
_id: string;
};
}

export type StepCreateDto = StepDto;
export class StepCreateDto extends StepDto {}

export type ListWorkflowResponse = {
workflows: WorkflowListResponseDto[];
Expand All @@ -35,19 +51,6 @@ export type WorkflowListResponseDto = Pick<
stepTypeOverviews: StepTypeEnum[];
};

export class StepDto {
@IsString()
@IsDefined()
name: string;

@IsString()
@IsDefined()
type: StepTypeEnum;

@IsObject()
controlValues: Record<string, unknown>;
}

export class WorkflowCommonsFields {
@IsOptional()
@IsArray()
Expand All @@ -59,11 +62,11 @@ export class WorkflowCommonsFields {
active?: boolean;

@IsString()
@IsDefined()
@IsNotEmpty()
name: string;

@IsString()
@IsDefined()
@IsNotEmpty()
workflowId: string;

@IsString()
Expand Down
Loading