add validator for positive amount value with Decimal Type

This commit is contained in:
Einhornimmond 2023-08-09 14:08:29 +02:00
parent 191103a812
commit 5e3d732e20
10 changed files with 35 additions and 12 deletions

View File

@ -3,6 +3,7 @@ import { Decimal } from 'decimal.js-light'
import { ArgsType, Field, InputType } from 'type-graphql'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const'
import { IsPositiveDecimal } from '@/graphql/validator/Decimal'
@InputType()
@ArgsType()
@ -11,6 +12,7 @@ export class AdminCreateContributionArgs {
email: string
@Field(() => Decimal)
@IsPositiveDecimal()
amount: Decimal
@Field(() => String)

View File

@ -3,6 +3,7 @@ import { Decimal } from 'decimal.js-light'
import { ArgsType, Field, Int } from 'type-graphql'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const'
import { IsPositiveDecimal } from '@/graphql/validator/Decimal'
@ArgsType()
export class AdminUpdateContributionArgs {
@ -10,6 +11,7 @@ export class AdminUpdateContributionArgs {
id: number
@Field(() => Decimal)
@IsPositiveDecimal()
amount: Decimal
@Field(() => String)

View File

@ -3,11 +3,13 @@ import { Decimal } from 'decimal.js-light'
import { ArgsType, Field, InputType } from 'type-graphql'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const'
import { IsPositiveDecimal } from '@/graphql/validator/Decimal'
@InputType()
@ArgsType()
export class ContributionArgs {
@Field(() => Decimal)
@IsPositiveDecimal()
amount: Decimal
@Field(() => String)

View File

@ -8,10 +8,12 @@ import {
CONTRIBUTIONLINK_NAME_MIN_CHARS,
CONTRIBUTIONLINK_NAME_MAX_CHARS,
} from '@/graphql/resolver/const/const'
import { IsPositiveDecimal } from '@/graphql/validator/Decimal'
@ArgsType()
export class ContributionLinkArgs {
@Field(() => Decimal)
@IsPositiveDecimal()
amount: Decimal
@Field(() => String)

View File

@ -1,12 +1,14 @@
import { MaxLength, MinLength } from 'class-validator'
import { IsAlpha, MaxLength, MinLength } from 'class-validator'
import { Decimal } from 'decimal.js-light'
import { ArgsType, Field } from 'type-graphql'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const'
import { IsPositiveDecimal } from '@/graphql/validator/Decimal'
@ArgsType()
export class TransactionLinkArgs {
@Field(() => Decimal)
@IsPositiveDecimal()
amount: Decimal
@Field(() => String)

View File

@ -3,6 +3,7 @@ import { Decimal } from 'decimal.js-light'
import { ArgsType, Field } from 'type-graphql'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const'
import { IsPositiveDecimal } from '@/graphql/validator/Decimal'
@ArgsType()
export class TransactionSendArgs {
@ -10,6 +11,7 @@ export class TransactionSendArgs {
identifier: string
@Field(() => Decimal)
@IsPositiveDecimal()
amount: Decimal
@Field(() => String)

View File

@ -41,10 +41,6 @@ export class ContributionLinkResolver {
): Promise<ContributionLink> {
isStartEndDateValid(validFrom, validTo)
if (!new Decimal(amount).isPositive()) {
throw new LogError('The amount must be a positiv value', amount)
}
const dbContributionLink = new DbContributionLink()
dbContributionLink.amount = amount
dbContributionLink.name = name

View File

@ -73,10 +73,6 @@ export class TransactionLinkResolver {
const createdDate = new Date()
const validUntil = transactionLinkExpireDate(createdDate)
if (amount.lessThanOrEqualTo(0)) {
throw new LogError('Amount must be a positive number', amount)
}
const holdAvailableAmount = amount.minus(calculateDecay(amount, createdDate, validUntil).decay)
// validate amount

View File

@ -308,9 +308,6 @@ export class TransactionResolver {
@Ctx() context: Context,
): Promise<boolean> {
logger.info(`sendCoins(identifier=${identifier}, amount=${amount}, memo=${memo})`)
if (amount.lte(0)) {
throw new LogError('Amount to send must be positive', amount)
}
const senderUser = getUser(context)

View File

@ -0,0 +1,22 @@
import { registerDecorator, ValidationOptions, ValidationArguments } from 'class-validator'
import { Decimal } from 'decimal.js-light'
export function IsPositiveDecimal(validationOptions?: ValidationOptions) {
return function (object: NonNullable<unknown>, propertyName: string) {
registerDecorator({
name: 'isPositiveDecimal',
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
target: object.constructor,
propertyName,
options: validationOptions,
validator: {
validate(value: Decimal) {
return value.greaterThan(0)
},
defaultMessage(args: ValidationArguments) {
return `The ${propertyName} must be a positive value ${args.property}`
},
},
})
}
}