diff --git a/packages/node-core/CHANGELOG.md b/packages/node-core/CHANGELOG.md index e07c886e59..6575c029d2 100644 --- a/packages/node-core/CHANGELOG.md +++ b/packages/node-core/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add `TextEncoder` in sandbox, some network package util method is depended on it +### Fixed +- Improved block range validation in POI endpoint with custom class-validator decorator + ## [16.1.0] - 2024-12-11 ### Changed - If any tests fail with the `test` subcommand the exit code will now be 1 instead of 0 (#2624) @@ -45,7 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add an `--enable-cache` flag, allowing you to choose between DB or cache for IO operations. ### Fixed -- When using a GET query to retrieve an entity, it will include a “store” field. +- When using a GET query to retrieve an entity, it will include a "store" field. - Support for historical indexing by timestamp as well as block height (#2584) - Subscriptions not emitting deleted mutation type with historical (#2600) @@ -144,7 +147,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [10.10.2] - 2024-07-10 ### Fixed -- Fix issue admin api can not get `dbSize` due to it not been set in \_metadata table +- Fix issue admin api can not get `dbSize` due to it not been set in _metadata table ## [10.10.1] - 2024-07-09 ### Added @@ -609,7 +612,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Base58 encoding check for POI (#1788) -- Fix project \_startHeight been blocked by poiSync (#1792) +- Fix project _startHeight been blocked by poiSync (#1792) ## [2.4.4] - 2023-06-07 ### Fixed diff --git a/packages/node-core/src/admin/admin.controller.ts b/packages/node-core/src/admin/admin.controller.ts index 19f9cdefb9..324a83936d 100644 --- a/packages/node-core/src/admin/admin.controller.ts +++ b/packages/node-core/src/admin/admin.controller.ts @@ -70,16 +70,6 @@ export class AdminController { @Get('poi/') async getPoisByRange(@Query(ValidationPipe) blockRange: BlockRangeDto): Promise { const {endBlock, startBlock} = blockRange; - // TODO, class validator seems not work properly, need to complete in future - if (endBlock && Number(startBlock) > Number(endBlock)) { - throw new HttpException( - { - status: HttpStatus.BAD_REQUEST, - error: 'startBlock must be greater than endBlock', - }, - HttpStatus.BAD_REQUEST - ); - } logger.info(`[POI] Getting poi history for blocks from ${startBlock} to ${endBlock}`); return handleServiceCall(async () => { const pois = await this.poiService.plainPoiRepo.getPoiBlocksByRange( diff --git a/packages/node-core/src/admin/blockRange.ts b/packages/node-core/src/admin/blockRange.ts index 651e3eaa3f..58a530278e 100644 --- a/packages/node-core/src/admin/blockRange.ts +++ b/packages/node-core/src/admin/blockRange.ts @@ -2,7 +2,30 @@ // SPDX-License-Identifier: GPL-3.0 import {Type} from 'class-transformer'; -import {IsInt, IsPositive, Min, IsOptional} from 'class-validator'; +import {IsInt, IsPositive, Min, IsOptional, ValidateIf, registerDecorator, ValidationOptions, ValidationArguments} from 'class-validator'; + +export function IsGreaterThan(property: string, validationOptions?: ValidationOptions) { + return function (object: any, propertyName: string) { + registerDecorator({ + name: 'isGreaterThan', + target: object.constructor, + propertyName: propertyName, + constraints: [property], + options: validationOptions, + validator: { + validate(value: any, args: ValidationArguments) { + const [relatedPropertyName] = args.constraints; + const relatedValue = (args.object as any)[relatedPropertyName]; + return value === undefined || value > relatedValue; + }, + defaultMessage(args: ValidationArguments) { + const [relatedPropertyName] = args.constraints; + return `${args.property} must be greater than ${relatedPropertyName}`; + }, + }, + }); + }; +} export interface BlockRangeDtoInterface { startBlock: number; @@ -26,5 +49,6 @@ export class BlockRangeDto implements BlockRangeDtoInterface { @IsPositive() @Min(0) @IsOptional() + @IsGreaterThan('startBlock') endBlock: number | undefined; }