Skip to content

Commit

Permalink
Allow null numbers and validate them correctly (#182)
Browse files Browse the repository at this point in the history
* Allow null numbers and validate them correctly

* Revert string number validator. Handle null numbers

* Validate null number
  • Loading branch information
AramAlsabti authored Jun 8, 2022
1 parent 7949ac3 commit 13e6ede
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/config/constants/pagination-constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const DefaultLimit = 100;
export const DefaultOffset = 0;
28 changes: 24 additions & 4 deletions src/entities/dto/list-all-applications.dto.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
import { ApiPropertyOptional } from "@nestjs/swagger";

import { DefaultLimit, DefaultOffset } from "@config/constants/pagination-constants";
import { ListAllEntitiesDto } from "@dto/list-all-entities.dto";
import { StringToNumber } from "@helpers/string-to-number-validator";
import { IsSwaggerOptional } from "@helpers/optional-validator";
import {
NullableStringToNumber,
StringToNumber,
} from "@helpers/string-to-number-validator";
import { ApiProperty, OmitType } from "@nestjs/swagger";
import { Transform } from "class-transformer";
import { IsOptional, IsNumber } from "class-validator";

export class ListAllApplicationsDto extends ListAllEntitiesDto {
export class ListAllApplicationsDto extends OmitType(ListAllEntitiesDto, [
"limit",
"offset",
]) {
@IsSwaggerOptional({ description: "Filter to one organization" })
@StringToNumber()
organizationId?: number;

@IsSwaggerOptional({ description: "Filter to one permission" })
@StringToNumber()
permissionId?: number;

@ApiProperty({ type: Number, required: false })
@IsOptional()
@IsNumber()
@Transform(({ value }) => NullableStringToNumber(value))
limit? = DefaultLimit;

@ApiProperty({ type: Number, required: false })
@IsOptional()
@IsNumber()
@Transform(({ value }) => NullableStringToNumber(value))
offset? = DefaultOffset;
}
5 changes: 3 additions & 2 deletions src/entities/dto/list-all-entities.dto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DefaultLimit, DefaultOffset } from "@config/constants/pagination-constants";
import { StringToNumber } from "@helpers/string-to-number-validator";
import { ApiProperty } from "@nestjs/swagger";
import { IsOptional, IsString } from "class-validator";
Expand All @@ -6,11 +7,11 @@ export class ListAllEntitiesDto {
@ApiProperty({ type: Number, required: false })
@IsOptional()
@StringToNumber()
limit? = 100;
limit? = DefaultLimit;
@ApiProperty({ type: Number, required: false })
@IsOptional()
@StringToNumber()
offset? = 0;
offset? = DefaultOffset;
@ApiProperty({ type: String, required: false })
@IsOptional()
@IsString()
Expand Down
14 changes: 14 additions & 0 deletions src/helpers/string-to-number-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,17 @@ export const StringToNumber = (): PropertyDecorator => {
IsNumber()(propertyValue, propertyName);
};
};

/**
* Fixes unexpected behaviour when casting using @Type() decorator. When casting a query parameter using
* Type() or Transform() from class-transformer, all parameters with the same decorator are casted.
* That's unexpected behaviour.
* @param value
*/
export const NullableStringToNumber = (value: unknown): number | null => {
if (value === null || value === "null") {
return null;
}

return Number(value);
};

0 comments on commit 13e6ede

Please sign in to comment.