added count validation

This commit is contained in:
hannathkadher
2024-12-11 12:03:09 +04:00
parent 69d0065ee6
commit 4f4fd7b734
2 changed files with 14 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import {
IsArray,
ValidateNested,
IsInt,
ArrayNotEmpty,
} from 'class-validator';
import { Type } from 'class-transformer';
import { CreateSpaceProductItemModelDto } from './create-space-product-item-model.dto';
@ -31,6 +32,7 @@ export class CreateSpaceProductModelDto {
type: [CreateSpaceProductItemModelDto],
})
@IsArray()
@ArrayNotEmpty()
@ValidateNested({ each: true })
@Type(() => CreateSpaceProductItemModelDto)
items: CreateSpaceProductItemModelDto[];

View File

@ -39,6 +39,8 @@ export class SpaceProductModelService {
spaceProductModelDto: CreateSpaceProductModelDto,
spaceModel: SpaceModelEntity,
) {
this.validateCount(spaceProductModelDto);
const product = await this.productRepository.findOneBy({
uuid: spaceProductModelDto.productId,
});
@ -62,4 +64,14 @@ export class SpaceProductModelService {
spaceModel,
);
}
private validateCount(spaceProductModelDto: CreateSpaceProductModelDto) {
const productItemCount = spaceProductModelDto.items.length;
if (spaceProductModelDto.productCount !== productItemCount) {
throw new HttpException(
`Product count (${spaceProductModelDto.productCount}) does not match the number of items (${productItemCount}) for product ID ${spaceProductModelDto.productId}.`,
HttpStatus.BAD_REQUEST,
);
}
}
}