Skip to content

Commit

Permalink
chore(constants): deep readonly for all constants and consistent way …
Browse files Browse the repository at this point in the history
…of declare mongoose props (#568)

Closes #564
  • Loading branch information
antoinezanardi committed Oct 14, 2023
1 parent b4e8c98 commit 72ed7b3
Show file tree
Hide file tree
Showing 120 changed files with 17,698 additions and 17,950 deletions.
1 change: 1 addition & 0 deletions config/jest/jest-unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const JEST_UNIT_CONFIG: Config = {
"!src/**/*.repository.ts",
"!src/**/*.dto.ts",
"!src/**/*.schema.ts",
"!src/**/*.constant.ts",
],
coverageDirectory: "tests/unit/coverage",
coverageThreshold: {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/config/env/enums/env.enum.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
enum ENVIRONMENTS {
enum Environnements {
DEVELOPMENT = "development",
PRODUCTION = "production",
TEST = "test",
}

export { ENVIRONMENTS };
export { Environnements };
6 changes: 3 additions & 3 deletions src/modules/config/env/types/env.type.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { IsEnum, IsNotEmpty, IsNumber, IsOptional, IsString } from "class-validator";

import { ENVIRONMENTS } from "@/modules/config/env/enums/env.enum";
import { Environnements } from "@/modules/config/env/enums/env.enum";

class EnvironmentVariables {
@IsEnum(ENVIRONMENTS)
public ENVIRONMENT: ENVIRONMENTS;
@IsEnum(Environnements)
public ENVIRONMENT: Environnements;

@IsString()
@IsNotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ const DEFAULT_GAME_OPTIONS: ReadonlyDeep<GameOptions> = {
},
raven: { markPenalty: 2 },
},
} as const;
};

export { DEFAULT_GAME_OPTIONS };
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { IsBoolean, IsEnum } from "class-validator";

Expand All @@ -6,19 +7,19 @@ import { PLAYER_API_PROPERTIES } from "@/modules/game/schemas/player/player.sche
import { RoleNames } from "@/modules/role/enums/role.enum";

class GamePlayerRoleBaseDto {
@ApiProperty(PLAYER_API_PROPERTIES.role)
@ApiProperty(PLAYER_API_PROPERTIES.role as ApiPropertyOptions)
@IsEnum(RoleNames)
public name: RoleNames;

@ApiProperty(PLAYER_ROLE_API_PROPERTIES.original)
@ApiProperty(PLAYER_ROLE_API_PROPERTIES.original as ApiPropertyOptions)
@IsEnum(RoleNames)
public original: RoleNames;

@ApiProperty(PLAYER_ROLE_API_PROPERTIES.current)
@ApiProperty(PLAYER_ROLE_API_PROPERTIES.current as ApiPropertyOptions)
@IsEnum(RoleNames)
public current: RoleNames;

@ApiProperty(PLAYER_ROLE_API_PROPERTIES.isRevealed)
@ApiProperty(PLAYER_ROLE_API_PROPERTIES.isRevealed as ApiPropertyOptions)
@IsBoolean()
public isRevealed: boolean;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { IsEnum } from "class-validator";

import { PLAYER_SIDE_API_PROPERTIES } from "@/modules/game/schemas/player/player-side/player-side.schema.constant";
import { RoleSides } from "@/modules/role/enums/role.enum";

class GamePlayerSideBaseDto {
@ApiProperty(PLAYER_SIDE_API_PROPERTIES.original)
@ApiProperty(PLAYER_SIDE_API_PROPERTIES.original as ApiPropertyOptions)
@IsEnum(RoleSides)
public original: RoleSides;

@ApiProperty(PLAYER_SIDE_API_PROPERTIES.current)
@ApiProperty(PLAYER_SIDE_API_PROPERTIES.current as ApiPropertyOptions)
@IsEnum(RoleSides)
public current: RoleSides;
}
Expand Down
11 changes: 6 additions & 5 deletions src/modules/game/dto/base/game-player/game-player.base.dto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { Expose, Transform, Type } from "class-transformer";
import { IsInt, IsString, MaxLength, Min, MinLength, ValidateNested } from "class-validator";
Expand All @@ -9,30 +10,30 @@ import { playerRoleTransformer } from "@/modules/game/dto/base/game-player/trans
import { playerSideTransformer } from "@/modules/game/dto/base/game-player/transformers/player-side.transformer";

class GamePlayerBaseDto {
@ApiProperty(PLAYER_API_PROPERTIES.name)
@ApiProperty(PLAYER_API_PROPERTIES.name as ApiPropertyOptions)
@IsString()
@MinLength(PLAYER_FIELDS_SPECS.name.minLength)
@MaxLength(PLAYER_FIELDS_SPECS.name.maxLength)
@Expose()
public name: string;

@ApiProperty(PLAYER_API_PROPERTIES.role)
@ApiProperty(PLAYER_API_PROPERTIES.role as ApiPropertyOptions)
@Transform(playerRoleTransformer)
@Type(() => GamePlayerRoleBaseDto)
@ValidateNested()
@Expose()
public role: GamePlayerRoleBaseDto;

@ApiProperty(PLAYER_API_PROPERTIES.role)
@ApiProperty(PLAYER_API_PROPERTIES.role as ApiPropertyOptions)
@Transform(playerSideTransformer)
@Type(() => GamePlayerRoleBaseDto)
@ValidateNested()
@Expose()
public side: GamePlayerSideBaseDto;

@ApiProperty(PLAYER_API_PROPERTIES.position)
@ApiProperty(PLAYER_API_PROPERTIES.position as ApiPropertyOptions)
@IsInt()
@Min(PLAYER_FIELDS_SPECS.position.minimum)
@Min(PLAYER_FIELDS_SPECS.position.min)
@Expose()
public position: number;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { Equals, IsEnum } from "class-validator";
Expand All @@ -6,12 +7,12 @@ import { GAME_ADDITIONAL_CARDS_API_PROPERTIES } from "@/modules/game/schemas/gam
import { RoleNames } from "@/modules/role/enums/role.enum";

class CreateGameAdditionalCardDto {
@ApiProperty(GAME_ADDITIONAL_CARDS_API_PROPERTIES.roleName)
@ApiProperty(GAME_ADDITIONAL_CARDS_API_PROPERTIES.roleName as ApiPropertyOptions)
@IsEnum(RoleNames)
@Expose()
public roleName: RoleNames;

@ApiProperty(GAME_ADDITIONAL_CARDS_API_PROPERTIES.recipient)
@ApiProperty(GAME_ADDITIONAL_CARDS_API_PROPERTIES.recipient as ApiPropertyOptions)
@Equals(RoleNames.THIEF)
@Expose()
public recipient: RoleNames.THIEF;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { Type } from "class-transformer";
import { IsBoolean, IsOptional } from "class-validator";

import { COMPOSITION_GAME_OPTIONS_API_PROPERTIES, COMPOSITION_GAME_OPTIONS_FIELDS_SPECS } from "@/modules/game/schemas/game-options/composition-game-options/composition-game-options.schema.constant";
import { DEFAULT_GAME_OPTIONS } from "@/modules/game/constants/game-options/game-options.constant";
import { COMPOSITION_GAME_OPTIONS_API_PROPERTIES } from "@/modules/game/schemas/game-options/composition-game-options/composition-game-options.schema.constant";

class CreateCompositionGameOptionsDto {
@ApiProperty({
...COMPOSITION_GAME_OPTIONS_API_PROPERTIES.isHidden,
required: false,
})
} as ApiPropertyOptions)
@Type(() => Boolean)
@IsOptional()
@IsBoolean()
public isHidden: boolean = COMPOSITION_GAME_OPTIONS_FIELDS_SPECS.isHidden.default;
public isHidden: boolean = DEFAULT_GAME_OPTIONS.composition.isHidden;
}

export { CreateCompositionGameOptionsDto };
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { Type } from "class-transformer";
import { IsOptional, ValidateNested } from "class-validator";
Expand All @@ -11,7 +12,7 @@ class CreateGameOptionsDto {
@ApiProperty({
...GAME_OPTIONS_API_PROPERTIES.composition,
required: false,
})
} as ApiPropertyOptions)
@IsOptional()
@Type(() => CreateCompositionGameOptionsDto)
@ValidateNested()
Expand All @@ -20,7 +21,7 @@ class CreateGameOptionsDto {
@ApiProperty({
...GAME_OPTIONS_API_PROPERTIES.votes,
required: false,
})
} as ApiPropertyOptions)
@IsOptional()
@Type(() => CreateVotesGameOptionsDto)
@ValidateNested()
Expand All @@ -29,7 +30,7 @@ class CreateGameOptionsDto {
@ApiProperty({
...GAME_OPTIONS_API_PROPERTIES.roles,
required: false,
})
} as ApiPropertyOptions)
@IsOptional()
@Type(() => CreateRolesGameOptionsDto)
@ValidateNested()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { IsBoolean, IsInt, IsOptional, Max, Min } from "class-validator";

Expand All @@ -7,17 +8,17 @@ class CreateAncientGameOptionsDto {
@ApiProperty({
...ANCIENT_GAME_OPTIONS_API_PROPERTIES.livesCountAgainstWerewolves,
required: false,
})
} as ApiPropertyOptions)
@IsOptional()
@IsInt()
@Min(ANCIENT_GAME_OPTIONS_FIELDS_SPECS.livesCountAgainstWerewolves.minimum)
@Max(ANCIENT_GAME_OPTIONS_FIELDS_SPECS.livesCountAgainstWerewolves.maximum)
@Min(ANCIENT_GAME_OPTIONS_FIELDS_SPECS.livesCountAgainstWerewolves.min)
@Max(ANCIENT_GAME_OPTIONS_FIELDS_SPECS.livesCountAgainstWerewolves.max)
public livesCountAgainstWerewolves: number = ANCIENT_GAME_OPTIONS_FIELDS_SPECS.livesCountAgainstWerewolves.default;

@ApiProperty({
...ANCIENT_GAME_OPTIONS_API_PROPERTIES.doesTakeHisRevenge,
required: false,
})
} as ApiPropertyOptions)
@IsOptional()
@IsBoolean()
public doesTakeHisRevenge: boolean = ANCIENT_GAME_OPTIONS_FIELDS_SPECS.doesTakeHisRevenge.default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { IsBoolean, IsOptional } from "class-validator";

Expand All @@ -7,7 +8,7 @@ class CreateBearTamerGameOptionsDto {
@ApiProperty({
...BEAR_TAMER_GAME_OPTIONS_API_PROPERTIES.doesGrowlIfInfected,
required: false,
})
} as ApiPropertyOptions)
@IsOptional()
@IsBoolean()
public doesGrowlIfInfected: boolean = BEAR_TAMER_GAME_OPTIONS_FIELDS_SPECS.doesGrowlIfInfected.default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { IsBoolean, IsOptional } from "class-validator";

Expand All @@ -7,7 +8,7 @@ class CreateBigBadWolfGameOptionsDto {
@ApiProperty({
...BIG_BAD_WOLF_GAME_OPTIONS_API_PROPERTIES.isPowerlessIfWerewolfDies,
required: false,
})
} as ApiPropertyOptions)
@IsOptional()
@IsBoolean()
public isPowerlessIfWerewolfDies: boolean = BIG_BAD_WOLF_GAME_OPTIONS_FIELDS_SPECS.isPowerlessIfWerewolfDies.default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { IsBoolean, IsOptional } from "class-validator";

Expand All @@ -8,7 +9,7 @@ class CreateDogWolfGameOptionsDto {
@ApiProperty({
...DOG_WOLF_GAME_OPTIONS_API_PROPERTIES.isChosenSideRevealed,
required: false,
})
} as ApiPropertyOptions)
@IsOptional()
@IsBoolean()
public isChosenSideRevealed: boolean = WILD_CHILD_GAME_OPTIONS_FIELDS_SPECS.isTransformationRevealed.default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { IsBoolean, IsOptional } from "class-validator";

Expand All @@ -7,7 +8,7 @@ class CreateFoxGameOptionsDto {
@ApiProperty({
...FOX_GAME_OPTIONS_API_PROPERTIES.isPowerlessIfMissesWerewolf,
required: false,
})
} as ApiPropertyOptions)
@IsOptional()
@IsBoolean()
public isPowerlessIfMissesWerewolf: boolean = FOX_GAME_OPTIONS_FIELDS_SPECS.isPowerlessIfMissesWerewolf.default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { IsBoolean, IsOptional } from "class-validator";

Expand All @@ -7,7 +8,7 @@ class CreateGuardGameOptionsDto {
@ApiProperty({
...GUARD_GAME_OPTIONS_API_PROPERTIES.canProtectTwice,
required: false,
})
} as ApiPropertyOptions)
@IsOptional()
@IsBoolean()
public canProtectTwice: boolean = GUARD_GAME_OPTIONS_FIELDS_SPECS.canProtectTwice.default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { IsBoolean, IsOptional } from "class-validator";

Expand All @@ -7,7 +8,7 @@ class CreateIdiotGameOptionsDto {
@ApiProperty({
...IDIOT_GAME_OPTIONS_API_PROPERTIES.doesDieOnAncientDeath,
required: false,
})
} as ApiPropertyOptions)
@IsOptional()
@IsBoolean()
public doesDieOnAncientDeath: boolean = IDIOT_GAME_OPTIONS_FIELDS_SPECS.doesDieOnAncientDeath.default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { IsBoolean, IsOptional } from "class-validator";

Expand All @@ -7,7 +8,7 @@ class CreateLittleGirlGameOptionsDto {
@ApiProperty({
...LITTLE_GIRL_GAME_OPTIONS_API_PROPERTIES.isProtectedByGuard,
required: false,
})
} as ApiPropertyOptions)
@IsOptional()
@IsBoolean()
public isProtectedByGuard: boolean = LITTLE_GIRL_GAME_OPTIONS_SPECS_FIELDS.isProtectedByGuard.default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { IsBoolean, IsInt, IsOptional, Max, Min } from "class-validator";

Expand All @@ -7,17 +8,17 @@ class CreatePiedPiperGameOptionsDto {
@ApiProperty({
...PIED_PIPER_GAME_OPTIONS_API_PROPERTIES.charmedPeopleCountPerNight,
required: false,
})
} as ApiPropertyOptions)
@IsOptional()
@IsInt()
@Min(PIED_PIPER_GAME_OPTIONS_FIELDS_SPECS.charmedPeopleCountPerNight.minimum)
@Max(PIED_PIPER_GAME_OPTIONS_FIELDS_SPECS.charmedPeopleCountPerNight.maximum)
@Min(PIED_PIPER_GAME_OPTIONS_FIELDS_SPECS.charmedPeopleCountPerNight.min)
@Max(PIED_PIPER_GAME_OPTIONS_FIELDS_SPECS.charmedPeopleCountPerNight.max)
public charmedPeopleCountPerNight: number = PIED_PIPER_GAME_OPTIONS_FIELDS_SPECS.charmedPeopleCountPerNight.default;

@ApiProperty({
...PIED_PIPER_GAME_OPTIONS_API_PROPERTIES.isPowerlessIfInfected,
required: false,
})
} as ApiPropertyOptions)
@IsOptional()
@IsBoolean()
public isPowerlessIfInfected: boolean = PIED_PIPER_GAME_OPTIONS_FIELDS_SPECS.isPowerlessIfInfected.default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { IsInt, IsOptional, Max, Min } from "class-validator";

Expand All @@ -7,11 +8,11 @@ class CreateRavenGameOptionsDto {
@ApiProperty({
...RAVEN_GAME_OPTIONS_API_PROPERTIES.markPenalty,
required: false,
})
} as ApiPropertyOptions)
@IsOptional()
@IsInt()
@Min(RAVEN_GAME_OPTIONS_FIELDS_SPECS.markPenalty.minimum)
@Max(RAVEN_GAME_OPTIONS_FIELDS_SPECS.markPenalty.maximum)
@Min(RAVEN_GAME_OPTIONS_FIELDS_SPECS.markPenalty.min)
@Max(RAVEN_GAME_OPTIONS_FIELDS_SPECS.markPenalty.max)
public markPenalty: number = RAVEN_GAME_OPTIONS_FIELDS_SPECS.markPenalty.default;
}

Expand Down

0 comments on commit 72ed7b3

Please sign in to comment.