40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import {
|
|
IsEnum,
|
|
IsNotEmpty,
|
|
IsObject,
|
|
Validate,
|
|
ValidatorConstraint,
|
|
type ValidatorConstraintInterface,
|
|
type ValidationArguments
|
|
} from 'class-validator';
|
|
import { ShopFiatCurrency } from '../../../types/ShopFiatCurrency';
|
|
|
|
@ValidatorConstraint({ name: 'coingeckoMoneroFiat' })
|
|
class CoingeckoMoneroFiatConstraint implements ValidatorConstraintInterface {
|
|
validate(monero: Record<string, number>, args: ValidationArguments): boolean {
|
|
const { shopFiatCurrency } = args.object as CoingeckoSimplePriceResponseDto;
|
|
|
|
if (!shopFiatCurrency) {
|
|
return false;
|
|
}
|
|
|
|
const rate = monero[shopFiatCurrency.toLowerCase()];
|
|
|
|
return typeof rate === 'number' && Number.isFinite(rate) && rate > 0;
|
|
}
|
|
|
|
defaultMessage(): string {
|
|
return 'CoinGecko monero fiat rate must be a positive number';
|
|
}
|
|
}
|
|
|
|
export class CoingeckoSimplePriceResponseDto {
|
|
@IsEnum(ShopFiatCurrency)
|
|
shopFiatCurrency: ShopFiatCurrency;
|
|
|
|
@IsNotEmpty()
|
|
@IsObject()
|
|
@Validate(CoingeckoMoneroFiatConstraint)
|
|
monero: Record<string, number>;
|
|
}
|