Skip to content

Commit

Permalink
fix: 🐛 Fix validations for ScopeType.Asset in ScopeDto
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
prashantasdeveloper committed Nov 26, 2024
1 parent 8849d34 commit 5ccb402
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/claims/decorators/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) &&
Expand All @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions src/common/decorators/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { isHexUuid, isUuid } from '@polymeshassociation/polymesh-sdk/utils';
import {
IsHexadecimal,
IsUppercase,
isUppercase,
Length,
Matches,
MaxLength,
maxLength,
registerDecorator,
ValidationArguments,
ValidationOptions,
Expand Down Expand Up @@ -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) {
Expand All @@ -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)`;
Expand Down

0 comments on commit 5ccb402

Please sign in to comment.