Skip to content

Commit

Permalink
updated IsSwaggerEnum decorator for optional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay-Maury committed Oct 4, 2023
1 parent 93f4286 commit b1cb9b6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/request/dto/create-request.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class RequestStatusDto {
export class RequestFilterDto {
// Optional status filter, validate that it's a valid enum value.
@IsOptional()
@IsSwaggerEnum(RequestStatusEnum, { isOptional: true })
@IsEnum(RequestStatusEnum, { each: true })
status?: RequestStatusEnum;

Expand All @@ -116,6 +117,7 @@ export class RequestFilterDto {

// Optional request type filter, validate that it's a valid enum value.
@IsOptional()
@IsSwaggerEnum(RequestTypeEnum, { isOptional: true })
@IsEnum(RequestTypeEnum, { each: true })
type?: RequestTypeEnum;
}
5 changes: 4 additions & 1 deletion src/request/dto/response-request.dto.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { RequestStatusEnum, RequestTypeEnum } from '../enum/request.enum';
import { RequestStatusEnum, RequestTypeEnum } from '@prisma/client';
import { IsSwaggerEnum } from 'src/utils/decorator/decorators';

export class ResponseRequestDto {
readonly requestId: number;
readonly userId: number;
readonly title: string;
@IsSwaggerEnum(RequestTypeEnum)
readonly type: RequestTypeEnum;
readonly description: string;
@IsSwaggerEnum(RequestStatusEnum)
readonly status: RequestStatusEnum;
readonly requestContent: object;
readonly responseContent: object;
Expand Down
2 changes: 2 additions & 0 deletions src/settlement/dto/create-settlement.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ export class CreateSettlementDto {
export class SettlementFilterDto {
// Optional settlement status filter, validated that it's valid SettlementStatusEnum value
@IsOptional()
@IsSwaggerEnum(SettlementStatusEnum, { isOptional: true })
@IsEnum(SettlementStatusEnum, { each: true })
requestStatus?: SettlementStatusEnum;

// Optional third party status filter, validate that it's valid thirdPartyResponseStatusEnum value
@IsOptional()
@IsSwaggerEnum(thirdPartyResponseStatusEnum, { isOptional: true })
@IsEnum(thirdPartyResponseStatusEnum, { each: true })
thirdPartyResponseStatus?: thirdPartyResponseStatusEnum;

Expand Down
5 changes: 4 additions & 1 deletion src/settlement/dto/response-settlement.dto.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import {
SettlementStatusEnum,
thirdPartyResponseStatusEnum,
} from '../enum/settlement.enum';
} from '@prisma/client';
import { IsSwaggerEnum } from 'src/utils/decorator/decorators';

export class ResponseSettlementDto {
readonly SettlementId: number;
readonly requestId: number;
readonly userId: number;
readonly adminId: number;
@IsSwaggerEnum(thirdPartyResponseStatusEnum)
readonly type: thirdPartyResponseStatusEnum;
readonly transactionId: number;
@IsSwaggerEnum(SettlementStatusEnum)
readonly requestStatus: SettlementStatusEnum;
readonly createdAt: Date;
readonly updatedAt: Date;
Expand Down
42 changes: 34 additions & 8 deletions src/utils/decorator/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
import { ApiProperty } from '@nestjs/swagger';
import { ApiProperty, ApiPropertyOptions } from '@nestjs/swagger';

// eslint-disable-next-line @typescript-eslint/ban-types
export function IsSwaggerEnum(enumObject: Record<string, string>): Function {
// Decorator to document enum properties in Swagger
// @param enumObject: The enum object to document
// @param options: An object with options for the decorator (including isOptional)
export function IsSwaggerEnum(
enumObject: Record<string, string>,
options: {
isOptional?: boolean;
apiPropertyOptions?: ApiPropertyOptions; // Additional decorator options for ApiProperty
} = {},
) {
return (target: any, propertyKey: string) => {
const { isOptional = false, apiPropertyOptions = {} } = options; // Destructure options

// Get the enum values from the provided enumObject
const values = Object.values(enumObject);
ApiProperty({
type: enumObject,
enum: values,
example: values.join(' || '), // Set an example enum value for documentation purposes
})(target, propertyKey);

// Create decorator options for ApiProperty
const decoratorOptions = {
type: enumObject, // Use the cloned enumObject
enum: values, // Specify the enum values
...apiPropertyOptions, // Merge additional ApiProperty options
};

// If the property is not optional, add the example property
if (!isOptional) {
decoratorOptions.example = values.join(' || '); // Set an example enum value for documentation purposes
}

// If the property is optional, mark it as such in Swagger documentation
if (isOptional) {
decoratorOptions['required'] = false;
}

// Apply the ApiProperty decorator with the specified options to the property
ApiProperty(decoratorOptions)(target, propertyKey);
};
}

0 comments on commit b1cb9b6

Please sign in to comment.