fix: rename parseOptionalInt to parseOptionalNumber

This allows us to handle the possible errors due to non-integer numbers with joi and return more precise error messages.

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2022-03-04 23:43:09 +01:00
parent 151e12a8a7
commit 45df0e6edb
7 changed files with 27 additions and 22 deletions

View file

@ -7,7 +7,7 @@ import { registerAs } from '@nestjs/config';
import * as Joi from 'joi'; import * as Joi from 'joi';
import { Loglevel } from './loglevel.enum'; import { Loglevel } from './loglevel.enum';
import { buildErrorMessage, parseOptionalInt } from './utils'; import { buildErrorMessage, parseOptionalNumber } from './utils';
export interface AppConfig { export interface AppConfig {
domain: string; domain: string;
@ -48,7 +48,7 @@ export default registerAs('appConfig', () => {
{ {
domain: process.env.HD_DOMAIN, domain: process.env.HD_DOMAIN,
rendererOrigin: process.env.HD_RENDERER_ORIGIN, rendererOrigin: process.env.HD_RENDERER_ORIGIN,
port: parseOptionalInt(process.env.PORT), port: parseOptionalNumber(process.env.PORT),
loglevel: process.env.HD_LOGLEVEL, loglevel: process.env.HD_LOGLEVEL,
}, },
{ {

View file

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
@ -9,7 +9,7 @@ import * as Joi from 'joi';
import { GitlabScope, GitlabVersion } from './gitlab.enum'; import { GitlabScope, GitlabVersion } from './gitlab.enum';
import { import {
buildErrorMessage, buildErrorMessage,
parseOptionalInt, parseOptionalNumber,
replaceAuthErrorsWithEnvironmentVariables, replaceAuthErrorsWithEnvironmentVariables,
toArrayConfig, toArrayConfig,
} from './utils'; } from './utils';
@ -346,7 +346,7 @@ export default registerAs('authConfig', () => {
{ {
session: { session: {
secret: process.env.HD_SESSION_SECRET, secret: process.env.HD_SESSION_SECRET,
lifetime: parseOptionalInt(process.env.HD_SESSION_LIFETIME), lifetime: parseOptionalNumber(process.env.HD_SESSION_LIFETIME),
}, },
local: { local: {
enableLogin: process.env.HD_AUTH_LOCAL_ENABLE_LOGIN, enableLogin: process.env.HD_AUTH_LOCAL_ENABLE_LOGIN,

View file

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
@ -7,7 +7,7 @@ import { registerAs } from '@nestjs/config';
import * as Joi from 'joi'; import * as Joi from 'joi';
import { DatabaseDialect } from './database-dialect.enum'; import { DatabaseDialect } from './database-dialect.enum';
import { buildErrorMessage, parseOptionalInt } from './utils'; import { buildErrorMessage, parseOptionalNumber } from './utils';
export interface DatabaseConfig { export interface DatabaseConfig {
username: string; username: string;
@ -62,7 +62,7 @@ export default registerAs('databaseConfig', () => {
password: process.env.HD_DATABASE_PASS, password: process.env.HD_DATABASE_PASS,
database: process.env.HD_DATABASE_NAME, database: process.env.HD_DATABASE_NAME,
host: process.env.HD_DATABASE_HOST, host: process.env.HD_DATABASE_HOST,
port: parseOptionalInt(process.env.HD_DATABASE_PORT), port: parseOptionalNumber(process.env.HD_DATABASE_PORT),
storage: process.env.HD_DATABASE_STORAGE, storage: process.env.HD_DATABASE_STORAGE,
dialect: process.env.HD_DATABASE_DIALECT, dialect: process.env.HD_DATABASE_DIALECT,
}, },

View file

@ -1,12 +1,12 @@
/* /*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { registerAs } from '@nestjs/config'; import { registerAs } from '@nestjs/config';
import * as Joi from 'joi'; import * as Joi from 'joi';
import { buildErrorMessage, parseOptionalInt } from './utils'; import { buildErrorMessage, parseOptionalNumber } from './utils';
export interface HstsConfig { export interface HstsConfig {
enable: boolean; enable: boolean;
@ -32,7 +32,7 @@ export default registerAs('hstsConfig', () => {
const hstsConfig = hstsSchema.validate( const hstsConfig = hstsSchema.validate(
{ {
enable: process.env.HD_HSTS_ENABLE, enable: process.env.HD_HSTS_ENABLE,
maxAgeSeconds: parseOptionalInt(process.env.HD_HSTS_MAX_AGE), maxAgeSeconds: parseOptionalNumber(process.env.HD_HSTS_MAX_AGE),
includeSubdomains: process.env.HD_HSTS_INCLUDE_SUBDOMAINS, includeSubdomains: process.env.HD_HSTS_INCLUDE_SUBDOMAINS,
preload: process.env.HD_HSTS_PRELOAD, preload: process.env.HD_HSTS_PRELOAD,
}, },

View file

@ -6,7 +6,7 @@
import { registerAs } from '@nestjs/config'; import { registerAs } from '@nestjs/config';
import * as Joi from 'joi'; import * as Joi from 'joi';
import { buildErrorMessage, parseOptionalInt, toArrayConfig } from './utils'; import { buildErrorMessage, parseOptionalNumber, toArrayConfig } from './utils';
export interface NoteConfig { export interface NoteConfig {
forbiddenNoteIds: string[]; forbiddenNoteIds: string[];
@ -29,7 +29,9 @@ export default registerAs('noteConfig', () => {
const noteConfig = schema.validate( const noteConfig = schema.validate(
{ {
forbiddenNoteIds: toArrayConfig(process.env.HD_FORBIDDEN_NOTE_IDS, ','), forbiddenNoteIds: toArrayConfig(process.env.HD_FORBIDDEN_NOTE_IDS, ','),
maxDocumentLength: parseOptionalInt(process.env.HD_MAX_DOCUMENT_LENGTH), maxDocumentLength: parseOptionalNumber(
process.env.HD_MAX_DOCUMENT_LENGTH,
),
}, },
{ {
abortEarly: false, abortEarly: false,

View file

@ -1,12 +1,12 @@
/* /*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { Loglevel } from './loglevel.enum'; import { Loglevel } from './loglevel.enum';
import { import {
needToLog, needToLog,
parseOptionalInt, parseOptionalNumber,
replaceAuthErrorsWithEnvironmentVariables, replaceAuthErrorsWithEnvironmentVariables,
toArrayConfig, toArrayConfig,
} from './utils'; } from './utils';
@ -84,12 +84,15 @@ describe('config utils', () => {
expect(needToLog(currentLevel, Loglevel.TRACE)).toBeTruthy(); expect(needToLog(currentLevel, Loglevel.TRACE)).toBeTruthy();
}); });
}); });
describe('parseOptionalInt', () => { describe('parseOptionalNumber', () => {
it('returns undefined on undefined parameter', () => { it('returns undefined on undefined parameter', () => {
expect(parseOptionalInt(undefined)).toEqual(undefined); expect(parseOptionalNumber(undefined)).toEqual(undefined);
}); });
it('correctly parses a string', () => { it('correctly parses a integer string', () => {
expect(parseOptionalInt('42')).toEqual(42); expect(parseOptionalNumber('42')).toEqual(42);
});
it('correctly parses a float string', () => {
expect(parseOptionalNumber('3.14')).toEqual(3.14);
}); });
}); });
}); });

View file

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
@ -113,9 +113,9 @@ function transformLoglevelToInt(loglevel: Loglevel): number {
} }
} }
export function parseOptionalInt(value?: string): number | undefined { export function parseOptionalNumber(value?: string): number | undefined {
if (value === undefined) { if (value === undefined) {
return undefined; return undefined;
} }
return parseInt(value); return Number(value);
} }