Skip to content

Commit

Permalink
fix: deeper validation on dtos
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Nov 6, 2024
1 parent 8bb51af commit 74e0458
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 31 deletions.
4 changes: 2 additions & 2 deletions apps/swap/src/swap.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('SwapService', () => {
},
},
target: {
invoice: {
payout: {
invoice: 'lnbtcexampleinvoicee',
},
},
Expand Down Expand Up @@ -429,7 +429,7 @@ describe('SwapService', () => {
amountFiat: '100',
target: {
currency: Currency.KES,
destination: {
payout: {
phone: '0700000000',
},
},
Expand Down
4 changes: 2 additions & 2 deletions apps/swap/src/swap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class SwapService {
reference: ref,
collectionTracker: id,
state: mapMpesaTxStateToSwapTxState(state),
lightning: target.invoice.invoice,
lightning: target.payout.invoice,
amountSats: amountSats.toFixed(2),
retryCount: 0,
rate,
Expand Down Expand Up @@ -249,7 +249,7 @@ export class SwapService {
lightning,
retryCount: 0,
reference: ref,
phone: target.destination.phone,
phone: target.payout.phone,
amountSats: amountSats.toFixed(2),
state: SwapTransactionState.PENDING,
},
Expand Down
2 changes: 1 addition & 1 deletion libs/common/src/dto/create-offramp-swap.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class OfframpSwapTargetDto implements OfframpSwapTarget {

@IsDefined()
@ApiProperty({ type: MobileMoneyDto })
destination: MobileMoneyDto;
payout: MobileMoneyDto;
}

export class CreateOfframpSwapDto implements OfframpSwapRequest {
Expand Down
17 changes: 14 additions & 3 deletions libs/common/src/dto/create-onramp-swap.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Validate,
IsDefined,
IsEnum,
ValidateNested,
} from 'class-validator';
import { Type } from 'class-transformer';
import { ApiProperty } from '@nestjs/swagger';
Expand All @@ -22,23 +23,29 @@ import { QuoteDto } from './quote.dto';

class OnrampSwapSourceDto implements OnrampSwapSource {
@IsEnum(Currency)
@ApiProperty({ enum: SupportedCurrencies, enumName: 'SupportedCurrencyType' })
@TransformToCurrency()
@ApiProperty({ enum: SupportedCurrencies, enumName: 'SupportedCurrencyType' })
currency: Currency;

@IsDefined()
@ValidateNested()
@Type(() => MobileMoneyDto)
@ApiProperty({ type: MobileMoneyDto })
origin: MobileMoneyDto;
origin: MobileMoneyDto | undefined;
}

class OnrampSwapTargetDto implements OnrampSwapTarget {
@IsDefined()
@ValidateNested()
@Type(() => Bolt11InvoiceDto)
@ApiProperty({ type: Bolt11InvoiceDto })
invoice: Bolt11InvoiceDto;
payout: Bolt11InvoiceDto;
}

export class CreateOnrampSwapDto implements OnrampSwapRequest {
@IsOptional()
@ValidateNested()
@Type(() => QuoteDto)
@ApiProperty({ type: QuoteDto })
quote: QuoteDto;

Expand All @@ -56,10 +63,14 @@ export class CreateOnrampSwapDto implements OnrampSwapRequest {
amountFiat: string;

@IsNotEmpty()
@ValidateNested()
@Type(() => OnrampSwapSourceDto)
@ApiProperty({ type: OnrampSwapSourceDto })
source: OnrampSwapSourceDto;

@IsNotEmpty()
@ValidateNested()
@Type(() => OnrampSwapTargetDto)
@ApiProperty({ type: OnrampSwapTargetDto })
target: OnrampSwapTargetDto;
}
10 changes: 7 additions & 3 deletions libs/common/src/dto/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { SupportedCurrencyType } from '../types';
import { mapToCurrency } from '../utils';

export function TransformToCurrency() {
return Transform(({ value }) =>
mapToCurrency(value as SupportedCurrencyType),
);
return Transform(({ value }) => {
if (typeof value === 'number') {
return value;
}

return mapToCurrency(value as SupportedCurrencyType);
});
}
12 changes: 6 additions & 6 deletions libs/common/src/types/proto/swap.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 5 additions & 14 deletions proto/swap.proto
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,12 @@ message OnrampSwapSource {
Currency currency = 1;

// Target destination
oneof origin {
// Mobile money destination
MobileMoney phone = 2;
}
MobileMoney origin = 2;
}

message OnrampSwapTarget {
// Payout destination
oneof payout {
// Lightning protocol destination
Bolt11 invoice = 2;
}
// Lightning protocol payout
Bolt11 payout = 2;
}

message OfframpSwapRequest {
Expand All @@ -135,11 +129,8 @@ message OfframpSwapTarget {
// Currency code for the target currency
Currency currency = 1;

// Payout destination
oneof payout {
// Mobile money destination
MobileMoney phone = 2;
}
// Mobile money payout destination
MobileMoney payout = 2;
}

message MobileMoney {
Expand Down

0 comments on commit 74e0458

Please sign in to comment.