Skip to content

Commit 5d48626

Browse files
fix: parameter validations issues (#1126)
* fix: added api param validations Signed-off-by: bhavanakarwade <[email protected]> * fix: removed unnecessary validations Signed-off-by: bhavanakarwade <[email protected]> * fix:resolved validations issue Signed-off-by: bhavanakarwade <[email protected]> * added comment on function for understanding Signed-off-by: bhavanakarwade <[email protected]> --------- Signed-off-by: bhavanakarwade <[email protected]>
1 parent d75859a commit 5d48626

File tree

8 files changed

+63
-12
lines changed

8 files changed

+63
-12
lines changed

apps/api-gateway/src/connection/connection.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export class ConnectionController {
217217
@ApiResponse({ status: HttpStatus.CREATED, description: 'Created', type: ApiResponseDto })
218218
async sendQuestion(
219219
@Param('orgId') orgId: string,
220-
@Param('connectionId') connectionId: string,
220+
@Param('connectionId', TrimStringParamPipe) connectionId: string,
221221
@Body() questionDto: QuestionDto,
222222
@User() reqUser: IUserRequestInterface,
223223
@Res() res: Response

apps/api-gateway/src/credential-definition/credential-definition.controller.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { CreateCredentialDefinitionDto } from './dto/create-cred-defs.dto';
1616
import { OrgRoles } from 'libs/org-roles/enums';
1717
import { Roles } from '../authz/decorators/roles.decorator';
1818
import { CustomExceptionFilter } from 'apps/api-gateway/common/exception-handler';
19-
import { TrimStringParamPipe } from '@credebl/common/cast.helper';
19+
import { EmptyStringParamPipe, TrimStringParamPipe } from '@credebl/common/cast.helper';
2020

2121

2222
@ApiBearerAuth()
@@ -46,8 +46,8 @@ export class CredentialDefinitionController {
4646
@Roles(OrgRoles.OWNER, OrgRoles.ADMIN, OrgRoles.ISSUER, OrgRoles.VERIFIER, OrgRoles.MEMBER)
4747
@UseGuards(AuthGuard('jwt'), OrgRolesGuard)
4848
async getCredentialDefinitionById(
49-
@Param('orgId', new ParseUUIDPipe({exceptionFactory: (): Error => { throw new BadRequestException(ResponseMessages.organisation.error.invalidOrgId); }})) orgId: string,
50-
@Param('credDefId') credentialDefinitionId: string,
49+
@Param('orgId') orgId: string,
50+
@Param('credDefId', TrimStringParamPipe, EmptyStringParamPipe.forParam('credDefId')) credentialDefinitionId: string,
5151
@Res() res: Response
5252
): Promise<Response> {
5353
const credentialsDefinitionDetails = await this.credentialDefinitionService.getCredentialDefinitionById(credentialDefinitionId, orgId);
@@ -77,6 +77,10 @@ export class CredentialDefinitionController {
7777
@Res() res: Response
7878
): Promise<Response> {
7979

80+
if (!schemaId) {
81+
throw new BadRequestException(ResponseMessages.schema.error.invalidSchemaId);
82+
}
83+
8084
const credentialsDefinitions = await this.credentialDefinitionService.getCredentialDefinitionBySchemaId(schemaId);
8185
const credDefResponse: IResponse = {
8286
statusCode: HttpStatus.OK,

apps/api-gateway/src/credential-definition/dto/get-all-platform-cred-defs.dto.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import { IsEnum, IsOptional, IsUUID } from 'class-validator';
88
export class GetAllPlatformCredDefsDto extends PaginationDto {
99

1010
@ApiProperty({ example: '1a7eac11-ff05-40d7-8351-4d7467687cad'})
11-
@Transform(({ value }) => trim(value))
1211
@ApiPropertyOptional()
12+
@Transform(({ value }) => ('string' === typeof value && '' === value.trim() ? undefined : value.trim()))
1313
@IsOptional()
1414
@IsUUID('4', { message: 'Invalid format of ledgerId' })
15-
ledgerId: string;
15+
ledgerId?: string;
1616

1717
@ApiProperty({
1818
required: false

apps/api-gateway/src/schema/dtos/get-all-schema.dto.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export class GetAllSchemaByPlatformDto {
8989

9090
@ApiProperty({ example: '1a7eac11-ff05-40d7-8351-4d7467687cad'})
9191
@ApiPropertyOptional()
92+
@Transform(({ value }) => ('string' === typeof value && '' === value.trim() ? undefined : value.trim()))
9293
@IsOptional()
9394
@IsUUID('4', { message: 'Invalid format of ledgerId' })
9495
ledgerId?: string;

apps/api-gateway/src/schema/schema.controller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ export class SchemaController {
5151
@ApiResponse({ status: HttpStatus.OK, description: 'Success', type: ApiResponseDto })
5252
async getSchemaById(
5353
@Res() res: Response,
54-
@Param('orgId', new ParseUUIDPipe({exceptionFactory: (): Error => { throw new BadRequestException(ResponseMessages.organisation.error.invalidOrgId); }})) orgId: string,
54+
@Param('orgId') orgId: string,
5555
@Param('schemaId', TrimStringParamPipe) schemaId: string
5656
): Promise<Response> {
5757

5858
if (!schemaId) {
5959
throw new BadRequestException(ResponseMessages.schema.error.invalidSchemaId);
6060
}
61+
6162
const schemaDetails = await this.appService.getSchemaById(schemaId, orgId);
6263
const finalResponse: IResponse = {
6364
statusCode: HttpStatus.OK,
@@ -92,7 +93,7 @@ export class SchemaController {
9293
@UseGuards(AuthGuard('jwt'), OrgRolesGuard)
9394
async getcredDeffListBySchemaId(
9495
@Param('orgId', new ParseUUIDPipe({exceptionFactory: (): Error => { throw new BadRequestException(ResponseMessages.organisation.error.invalidOrgId); }})) orgId: string,
95-
@Param('schemaId') schemaId: string,
96+
@Param('schemaId', TrimStringParamPipe) schemaId: string,
9697
@Query() getCredentialDefinitionBySchemaIdDto: GetCredentialDefinitionBySchemaIdDto,
9798
@Res() res: Response,
9899
@User() user: IUserRequestInterface): Promise<Response> {

apps/connection/src/connection.service.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable camelcase */
22
import { CommonService } from '@credebl/common';
33
import { CommonConstants } from '@credebl/common/common.constant';
4-
import { HttpException, Inject, Injectable, Logger, NotFoundException } from '@nestjs/common';
4+
import { HttpException, HttpStatus, Inject, Injectable, Logger, NotFoundException } from '@nestjs/common';
55
import { ClientProxy, RpcException } from '@nestjs/microservices';
66
import { from, map } from 'rxjs';
77
import {
@@ -466,8 +466,16 @@ export class ConnectionService {
466466
const createConnectionInvitation = await this._receiveInvitationUrl(url, orgId, receiveInvitationUrl);
467467
return createConnectionInvitation.response;
468468
} catch (error) {
469-
this.logger.error(`[receiveInvitationUrl] - error in receive invitation url : ${JSON.stringify(error)}`);
469+
this.logger.error(`[receiveInvitationUrl] - error in receive invitation url : ${JSON.stringify(error, null, 2)}`);
470470

471+
const customErrorMessage = error?.status?.message?.error?.message;
472+
if (customErrorMessage) {
473+
throw new RpcException({
474+
statusCode: HttpStatus.CONFLICT,
475+
message: customErrorMessage,
476+
error: ResponseMessages.errorMessages.conflict
477+
});
478+
} else
471479
if (error?.response?.error?.reason) {
472480
throw new RpcException({
473481
message: ResponseMessages.connection.error.connectionNotFound,
@@ -487,9 +495,22 @@ export class ConnectionService {
487495
): Promise<{
488496
response;
489497
}> {
498+
490499
const pattern = { cmd: 'agent-receive-invitation-url' };
491500
const payload = { url, orgId, receiveInvitationUrl };
492-
return this.natsCall(pattern, payload);
501+
502+
try {
503+
return await this.natsCall(pattern, payload);
504+
} catch (error) {
505+
this.logger.error(`catch: ${JSON.stringify(error)}`);
506+
throw new HttpException(
507+
{
508+
status: error.status,
509+
error: error.message
510+
},
511+
error.status
512+
);
513+
}
493514
}
494515

495516
async receiveInvitation(

libs/common/src/cast.helper.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,30 @@ export class TrimStringParamPipe implements PipeTransform {
156156
}
157157
}
158158

159+
//TODO: Need to add this logic in `trimstringpipe`
160+
export class EmptyStringParamPipe implements PipeTransform {
161+
private paramName: string;
162+
163+
static forParam(paramName: string): PipeTransform {
164+
return new EmptyStringParamPipe(paramName);
165+
}
166+
167+
private constructor(paramName: string) {
168+
this.paramName = paramName;
169+
}
170+
171+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/explicit-function-return-type
172+
transform(value: string) {
173+
const trimmedValue = value.trim();
174+
175+
if (!trimmedValue) {
176+
throw new BadRequestException(`${this.paramName} is required`);
177+
}
178+
179+
return plainToClass(String, trimmedValue);
180+
}
181+
}
182+
159183
// export const IsNotUUID = (validationOptions?: ValidationOptions): PropertyDecorator => (object: object, propertyName: string) => {
160184
// registerDecorator({
161185
// name: 'isNotUUID',

libs/common/src/response-messages/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export const ResponseMessages = {
151151
create: 'Schema created successfully.'
152152
},
153153
error: {
154-
invalidSchemaId: 'Invalid schema Id provided.',
154+
invalidSchemaId: 'Please provide valid schema Id',
155155
invalidData: 'Invalid data provided.',
156156
nameNotEmpty: 'Schema name is required',
157157
versionNotEmpty: 'Schema version is required',

0 commit comments

Comments
 (0)