Skip to content

Commit

Permalink
src/donations: Improve validation of metadata fields if not undefined
Browse files Browse the repository at this point in the history
-if metadata is included in the request body
    -it should not be empty
    -should contain either name or extraData
    -extraData should be an object
  • Loading branch information
sashko9807 committed Nov 8, 2023
1 parent 5a68fc4 commit 26837ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
17 changes: 15 additions & 2 deletions apps/api/src/affiliate/dto/create-affiliate-donation.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { ApiProperty } from '@nestjs/swagger'
import { Currency, DonationStatus, DonationType, PaymentProvider, Prisma } from '@prisma/client'
import { Expose, Type } from 'class-transformer'
import { Equals, IsBoolean, IsEnum, IsNumber, IsOptional, IsString, IsUUID } from 'class-validator'
import {
Equals,
IsBoolean,
IsEnum,
IsNotEmptyObject,
IsNumber,
IsOptional,
IsString,
IsUUID,
ValidateIf,
ValidateNested,
} from 'class-validator'
import { randomUUID } from 'crypto'
import { DonationMetadataDto } from '../../donations/dto/donation-metadata.dto'

Expand Down Expand Up @@ -64,8 +75,10 @@ export class CreateAffiliateDonationDto {

@ApiProperty()
@Expose()
@ValidateIf((req) => typeof req.metadata !== 'undefined')
@IsNotEmptyObject({ nullable: true })
@Type(() => DonationMetadataDto)
@IsOptional()
@ValidateNested({ each: true })
metadata: DonationMetadataDto | undefined

public toEntity(targetVaultId: string): Prisma.DonationCreateInput {
Expand Down
10 changes: 7 additions & 3 deletions apps/api/src/donations/dto/donation-metadata.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { ApiProperty } from '@nestjs/swagger'
import { Expose } from 'class-transformer'
import { Expose, Type } from 'class-transformer'
import { IsObject, IsOptional, IsString } from 'class-validator'

class TExtraData {
[key: string]: boolean | string | null
}

export class DonationMetadataDto {
@ApiProperty()
@Expose()
Expand All @@ -12,7 +16,7 @@ export class DonationMetadataDto {
@ApiProperty()
@Expose()
@IsObject()
@Type(() => TExtraData)
@IsOptional()
//eslint-disable-next-line
extraData: any
extraData: TExtraData | undefined
}

0 comments on commit 26837ce

Please sign in to comment.