diff --git a/backend/src/config/validate.spec.ts b/backend/src/config/validate.spec.ts new file mode 100644 index 0000000..e81a0f5 --- /dev/null +++ b/backend/src/config/validate.spec.ts @@ -0,0 +1,37 @@ +import 'reflect-metadata'; +import { validate } from './validate'; + +const validConfig = (overrides: Record = {}): Record => ({ + NODE_ENV: 'development', + BACKEND_PORT: 3000, + POSTGRES_USER: 'postgres', + POSTGRES_PASSWORD: 'postgres', + POSTGRES_DB: 'private_image', + POSTGRES_HOST: 'postgres', + POSTGRES_PORT: 5432, + POSTGRES_MIGRATIONS_RUN: 'false', + CORS_ORIGINS: 'http://localhost:5173', + MAX_IMAGES_PER_UPLOAD: 10, + MULTER_MAX_FILE_BYTES: 10485760, + ALTCHA_ENABLED: 'true', + ALTCHA_HMAC_KEY: 'secret', + ALTCHA_MAX_NUMBER: 50000, + NEVER_MODE_INACTIVITY_DAYS: 365, + THROTTLE_TTL_MS: 60000, + THROTTLE_LIMIT: 1000, + THROTTLE_UPLOAD_TTL_MS: 60000, + THROTTLE_UPLOAD_LIMIT: 20, + ...overrides +}); + +describe('validate', () => { + it('accepts true and false env boolean strings', () => { + expect(() => validate(validConfig({ POSTGRES_MIGRATIONS_RUN: 'true' }))).not.toThrow(); + expect(() => validate(validConfig({ ALTCHA_ENABLED: 'false' }))).not.toThrow(); + }); + + it('rejects non-literal boolean env values', () => { + expect(() => validate(validConfig({ ALTCHA_ENABLED: 'yes' }))).toThrow(); + expect(() => validate(validConfig({ POSTGRES_MIGRATIONS_RUN: '1' }))).toThrow(); + }); +}); diff --git a/backend/src/config/validate.ts b/backend/src/config/validate.ts index f95945d..65923e9 100644 --- a/backend/src/config/validate.ts +++ b/backend/src/config/validate.ts @@ -1,5 +1,7 @@ import { plainToClass } from 'class-transformer'; -import { IsBoolean, IsEnum, IsNotEmpty, IsNumber, IsString, Max, Min, validateSync } from 'class-validator'; +import { IsEnum, IsIn, IsNotEmpty, IsNumber, IsString, Max, Min, validateSync } from 'class-validator'; +import { BOOLEAN_STRING_VALUES } from '../consts/booleanStringValues'; +import type { BooleanStringValue } from '../types/BooleanStringValue'; import { NodeEnv } from '../types/NodeEnv'; class EnvironmentVariables { @@ -36,8 +38,8 @@ class EnvironmentVariables { POSTGRES_PORT: number; @IsNotEmpty() - @IsBoolean() - POSTGRES_MIGRATIONS_RUN: boolean; + @IsIn(BOOLEAN_STRING_VALUES) + POSTGRES_MIGRATIONS_RUN: BooleanStringValue; @IsNotEmpty() @IsString() @@ -56,8 +58,8 @@ class EnvironmentVariables { MULTER_MAX_FILE_BYTES: number; @IsNotEmpty() - @IsBoolean() - ALTCHA_ENABLED: boolean; + @IsIn(BOOLEAN_STRING_VALUES) + ALTCHA_ENABLED: BooleanStringValue; @IsNotEmpty() @IsString() diff --git a/backend/src/consts/booleanStringValues.ts b/backend/src/consts/booleanStringValues.ts new file mode 100644 index 0000000..309c04f --- /dev/null +++ b/backend/src/consts/booleanStringValues.ts @@ -0,0 +1 @@ +export const BOOLEAN_STRING_VALUES = ['true', 'false'] as const; diff --git a/backend/src/types/BooleanStringValue.ts b/backend/src/types/BooleanStringValue.ts new file mode 100644 index 0000000..40237ab --- /dev/null +++ b/backend/src/types/BooleanStringValue.ts @@ -0,0 +1,3 @@ +import { BOOLEAN_STRING_VALUES } from '../consts/booleanStringValues'; + +export type BooleanStringValue = (typeof BOOLEAN_STRING_VALUES)[number];