From 5ccb402e4db3256037492029508520ad2c1447b8 Mon Sep 17 00:00:00 2001 From: Prashant Bajpai <34747455+prashantasdeveloper@users.noreply.github.com> Date: Tue, 26 Nov 2024 17:52:52 +0530 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Fix=20validations=20for?= =?UTF-8?q?=20`ScopeType.Asset`=20in=20`ScopeDto`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `ScopeDto` was allowing all values of asset ID to pass when `type` was set to `ScopeType.Asset`. This adds in check for it to be a valid Asset ID --- src/claims/decorators/validation.ts | 10 ++++++---- src/common/decorators/validation.ts | 10 +++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/claims/decorators/validation.ts b/src/claims/decorators/validation.ts index 3a808520..4f4e486c 100644 --- a/src/claims/decorators/validation.ts +++ b/src/claims/decorators/validation.ts @@ -6,19 +6,17 @@ import { ScopeType } from '@polymeshassociation/polymesh-sdk/types'; import { IsHexadecimal, isHexadecimal, - isUppercase, Length, length, Matches, matches, - maxLength, registerDecorator, ValidationArguments, ValidationOptions, } from 'class-validator'; import { isString } from 'lodash'; -import { MAX_TICKER_LENGTH } from '~/assets/assets.consts'; +import { isAssetId, isTicker } from '~/common/decorators'; import { CDD_ID_LENGTH, DID_LENGTH } from '~/identities/identities.consts'; export function IsCddId() { @@ -54,8 +52,10 @@ export function IsValidScopeValue(property: string, validationOptions?: Validati // eslint-disable-next-line @typescript-eslint/no-explicit-any const scopeType = (args.object as any)[scopeTypeField]; switch (scopeType) { + case ScopeType.Asset: + return isString(value) && (isAssetId(value) || isTicker(value)); case ScopeType.Ticker: - return maxLength(value, MAX_TICKER_LENGTH) && isUppercase(value); + return isString(value) && isTicker(value); case ScopeType.Identity: return ( isHexadecimal(value) && @@ -74,6 +74,8 @@ export function IsValidScopeValue(property: string, validationOptions?: Validati // eslint-disable-next-line @typescript-eslint/no-explicit-any const scopeType = (args.object as any)[scopeTypeField]; switch (scopeType) { + case ScopeType.Asset: + return 'value must be a valid Asset ID either in hex or UUID format'; case ScopeType.Ticker: return `value must be all uppercase and no longer than 12 characters for type: ${scopeType}`; case ScopeType.Identity: diff --git a/src/common/decorators/validation.ts b/src/common/decorators/validation.ts index 44accb3a..15bbfa94 100644 --- a/src/common/decorators/validation.ts +++ b/src/common/decorators/validation.ts @@ -7,9 +7,11 @@ import { isHexUuid, isUuid } from '@polymeshassociation/polymesh-sdk/utils'; import { IsHexadecimal, IsUppercase, + isUppercase, Length, Matches, MaxLength, + maxLength, registerDecorator, ValidationArguments, ValidationOptions, @@ -47,6 +49,10 @@ export const isAssetId = (id: string): boolean => { return isHexUuid(id) || isUuid(id); }; +export const isTicker = (ticker: string): boolean => { + return maxLength(ticker, MAX_TICKER_LENGTH) && isUppercase(ticker); +}; + export function IsAsset(validationOptions?: ValidationOptions) { // eslint-disable-next-line @typescript-eslint/ban-types return function (object: Object, propertyName: string) { @@ -57,9 +63,7 @@ export function IsAsset(validationOptions?: ValidationOptions) { options: validationOptions, validator: { validate(value: string) { - return ( - isAssetId(value) || (value.length <= MAX_TICKER_LENGTH && value === value.toUpperCase()) - ); + return isAssetId(value) || isTicker(value); }, defaultMessage(args: ValidationArguments) { return `${args.property} must be either a Ticker (${MAX_TICKER_LENGTH} characters uppercase string) or an Asset ID (${ASSET_ID_LENGTH} characters long hex string)`;