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: add initial validation to widget #420

Merged
merged 17 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions apps/api/src/app/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ export class AuthService {

async apiKeyAuthenticate(apiKey: string) {
const environment = await this.environmentRepository.findByApiKey(apiKey);
if (!environment) throw new UnauthorizedException('API Key not found');
if (!environment) throw new UnauthorizedException('API Key not found!');

const key = environment.apiKeys.find((i) => i.key === apiKey);
if (!key) throw new UnauthorizedException('API Key not found');
if (!key) throw new UnauthorizedException('API Key not found!');

const user = await this.getUser({ _id: key._userId });
if (!user) throw new UnauthorizedException('User not found');
if (!user) throw new UnauthorizedException('User not found!');

return this.getSignedToken(
{
Expand Down
3 changes: 2 additions & 1 deletion apps/api/src/app/common/common.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ export class CommonController {
@ApiOperation({
summary: 'Check if request is valid (Checks Auth)',
})
async isRequestValid(@Body() body: ValidRequestDto): Promise<boolean> {
async isRequestValid(@Body() body: ValidRequestDto): Promise<{ success: boolean }> {
return this.validRequest.execute(
ValidRequestCommand.create({
projectId: body.projectId,
templateId: body.templateId,
schema: body.schema,
})
);
}
Expand Down
114 changes: 114 additions & 0 deletions apps/api/src/app/common/dtos/Schema.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import {
IsArray,
IsBoolean,
IsOptional,
IsString,
IsEnum,
IsNumber,
ValidateIf,
IsNotEmpty,
Validate,
ArrayMinSize,
} from 'class-validator';
import { Type } from 'class-transformer';
import { ColumnTypesEnum, Defaults } from '@impler/shared';
import { IsValidRegex } from '@shared/framework/is-valid-regex.validator';

export class SchemaDto {
@ApiProperty({
description: 'Name of the column',
type: 'string',
})
@IsString()
name: string;

@ApiProperty({
description: 'Key of the column',
})
@IsNotEmpty({ message: "'key' should not empty" })
@IsString({ message: "'key' must be a string" })
key: string;

@ApiProperty({
description: 'Alternative possible keys of the column',
type: Array<string>,
})
@IsArray()
@IsOptional()
@Type(() => Array<string>)
alternateKeys: string[];

@ApiPropertyOptional({
description: 'While true, it Indicates column value should exists in data',
})
@IsBoolean({ message: "'isRequired' must be boolean" })
@IsOptional()
isRequired = false;

@ApiPropertyOptional({
description: 'While true, it Indicates column value should be unique in data',
})
@IsBoolean({ message: "'isUnique' must be boolean" })
@IsOptional()
isUnique = false;

@ApiProperty({
description: 'Specifies the type of column',
enum: ColumnTypesEnum,
})
@IsEnum(ColumnTypesEnum, {
message: `entered type must be one of ${Object.values(ColumnTypesEnum).join(', ')}`,
})
type: ColumnTypesEnum;

@ApiPropertyOptional({
description: 'Regex if type is Regex',
})
@Validate(IsValidRegex, {
message: 'Invalid regex pattern',
})
@IsNotEmpty({
message: "'regex' should not empty, when type is Regex",
})
@ValidateIf((object) => object.type === ColumnTypesEnum.REGEX)
regex: string;

@ApiPropertyOptional({
description: 'Description of Regex',
})
@ValidateIf((object) => object.type === ColumnTypesEnum.REGEX)
@IsString()
@IsOptional()
regexDescription: string;

@ApiPropertyOptional({
description: 'List of possible values for column if type is Select',
})
@ValidateIf((object) => object.type === ColumnTypesEnum.SELECT, {
message: "'selectValues' should not empty, when type is Select",
})
@IsArray({ message: "'selectValues' must be an array, when type is Select" })
@ArrayMinSize(1, { message: "'selectValues' should not empty, when type is Select" })
@Type(() => Array)
@IsOptional()
selectValues: string[] = [];

@ApiPropertyOptional({
description: 'List of date formats for column if type is Date',
})
@ValidateIf((object) => object.type === ColumnTypesEnum.DATE, {
message: "'dateFormats' should not empty, when type is Date",
})
@Type(() => Array<string>)
@IsArray({ message: "'dateFormats' must be an array, when type is Date" })
@ArrayMinSize(1, { message: "'dateFormats' must not be empty, when type is Date" })
dateFormats: string[] = Defaults.DATE_FORMATS;

@ApiProperty({
description: 'Sequence of column',
})
@IsNumber()
@IsOptional()
sequence: number;
}
13 changes: 10 additions & 3 deletions apps/api/src/app/common/dtos/valid.dto.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsDefined, IsOptional, IsString, IsMongoId } from 'class-validator';
import { IsDefined, IsOptional, IsMongoId } from 'class-validator';

export class ValidRequestDto {
@ApiProperty({
description: 'Id of the project',
})
@IsMongoId()
@IsMongoId({
message: 'Invalid project id',
})
@IsDefined()
projectId: string;

@ApiProperty({
description: 'ID of the template',
})
@IsString()
@IsMongoId({
message: 'Invalid template id',
})
@IsOptional()
templateId?: string;

@IsOptional()
schema?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ export class ValidRequestCommand extends BaseCommand {
@IsString()
@IsOptional()
templateId: string;

@IsOptional()
schema: string;
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,93 @@
import { Injectable } from '@nestjs/common';
import { Injectable, HttpStatus, HttpException, UnauthorizedException } from '@nestjs/common';
import { ProjectRepository, TemplateRepository } from '@impler/dal';
import { ValidRequestCommand } from './valid-request.command';
import { DocumentNotFoundException } from '@shared/exceptions/document-not-found.exception';
import { APIMessages } from '@shared/constants';
import { plainToClass } from 'class-transformer';
import { validate } from 'class-validator';
import { SchemaDto } from 'app/common/dtos/Schema.dto';

@Injectable()
export class ValidRequest {
constructor(private projectRepository: ProjectRepository, private templateRepository: TemplateRepository) {}

async execute(command: ValidRequestCommand) {
if (command.templateId) {
const templateCount = await this.templateRepository.count({
_id: command.templateId,
_projectId: command.projectId,
});
if (!templateCount) {
throw new DocumentNotFoundException('Template', command.templateId, APIMessages.PROJECT_WITH_TEMPLATE_MISSING);
async execute(command: ValidRequestCommand): Promise<{ success: boolean }> {
try {
if (command.projectId) {
const projectCount = await this.projectRepository.count({
_id: command.projectId,
});

if (!projectCount) {
throw new DocumentNotFoundException('Project', command.projectId, APIMessages.INCORRECT_KEYS_FOUND);
}
}
} else {
const projectCount = await this.projectRepository.count({
_id: command.projectId,
});
if (!projectCount) {
throw new DocumentNotFoundException('Project', command.projectId);

if (command.templateId) {
const templateCount = await this.templateRepository.count({
_id: command.templateId,
_projectId: command.projectId,
});

if (!templateCount) {
throw new DocumentNotFoundException('Template', command.templateId, APIMessages.INCORRECT_KEYS_FOUND);
}
}
}

return true;
if (command.schema) {
const parsedSchema: SchemaDto = JSON.parse(command.schema);

const errors: string[] = [];
if (!Array.isArray(parsedSchema)) {
throw new DocumentNotFoundException(
'Schema',
command.schema,
'Invalid schema input. An array of objects is expected.'
);
}

for (const item of parsedSchema) {
const columnDto = plainToClass(SchemaDto, item);
const validationErrors = await validate(columnDto);

// eslint-disable-next-line no-magic-numbers
if (validationErrors.length > 0) {
errors.push(
`Schema Error : ${validationErrors
.map((err) => {
return Object.values(err.constraints);
})
.join(', ')}`
);

throw new DocumentNotFoundException('Schema', command.schema, errors.toString());
}
}
}

return { success: true };
} catch (error) {
if (error instanceof DocumentNotFoundException) {
throw new HttpException(
{
message: error.message,
errorCode: error.getStatus(),
},
HttpStatus.NOT_FOUND
);
}

if (error instanceof UnauthorizedException) {
throw new HttpException(
{
message: APIMessages.INVALID_AUTH_TOKEN,
errorCode: error.getStatus(),
},
HttpStatus.NOT_FOUND
);
}

throw new HttpException('Internal Server Error', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
2 changes: 2 additions & 0 deletions apps/api/src/app/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export const APIMessages = {
INCORRECT_LOGIN_CREDENTIALS: 'Incorrect email or password provided',
OPERATION_NOT_ALLOWED: `You're not allowed to perform this action.`,
EMAIL_ALREADY_EXISTS: 'Email already exists',
INCORRECT_KEYS_FOUND: 'Invalid keys found! Please check and correct them from web',
INVALID_AUTH_TOKEN: 'Invalid authentication token',
};

export const CONSTANTS = {
Expand Down
3 changes: 2 additions & 1 deletion apps/web/pages/imports/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default function ImportDetails({ template }: ImportDetailProps) {
</Group>
<Group spacing="xs">
<Button
// eslint-disable-next-line no-magic-numbers
disabled={!isImplerInitiated || columns?.length === 0}
color="green"
onClick={() => showWidget({ colorScheme })}
Expand Down Expand Up @@ -127,11 +128,11 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
const templateId = context.params?.id as string | undefined;
const authenticationToken = context.req.cookies[CONSTANTS.AUTH_COOKIE_NAME];
if (!templateId || !authenticationToken) throw new Error();

const template = await commonApi<ITemplate>(API_KEYS.TEMPLATE_DETAILS as any, {
parameters: [templateId],
cookie: `${CONSTANTS.AUTH_COOKIE_NAME}=${authenticationToken}`,
});

if (!template) throw new Error();

return {
Expand Down
2 changes: 1 addition & 1 deletion apps/widget/src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function App() {
<Router basename={CONTEXT_PATH}>
<Routes>
<Route
path="/:projectId"
path="/widget"
element={
<AppShell>
<WidgetShell>
Expand Down
Loading