Skip to content

Commit

Permalink
fix: rename parseOptionalInt to parseOptionalNumber
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
DerMolly committed Mar 6, 2022
1 parent 151e12a commit 45df0e6
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/config/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { registerAs } from '@nestjs/config';
import * as Joi from 'joi';

import { Loglevel } from './loglevel.enum';
import { buildErrorMessage, parseOptionalInt } from './utils';
import { buildErrorMessage, parseOptionalNumber } from './utils';

export interface AppConfig {
domain: string;
Expand Down Expand Up @@ -48,7 +48,7 @@ export default registerAs('appConfig', () => {
{
domain: process.env.HD_DOMAIN,
rendererOrigin: process.env.HD_RENDERER_ORIGIN,
port: parseOptionalInt(process.env.PORT),
port: parseOptionalNumber(process.env.PORT),
loglevel: process.env.HD_LOGLEVEL,
},
{
Expand Down
6 changes: 3 additions & 3 deletions src/config/auth.config.ts
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -9,7 +9,7 @@ import * as Joi from 'joi';
import { GitlabScope, GitlabVersion } from './gitlab.enum';
import {
buildErrorMessage,
parseOptionalInt,
parseOptionalNumber,
replaceAuthErrorsWithEnvironmentVariables,
toArrayConfig,
} from './utils';
Expand Down Expand Up @@ -346,7 +346,7 @@ export default registerAs('authConfig', () => {
{
session: {
secret: process.env.HD_SESSION_SECRET,
lifetime: parseOptionalInt(process.env.HD_SESSION_LIFETIME),
lifetime: parseOptionalNumber(process.env.HD_SESSION_LIFETIME),
},
local: {
enableLogin: process.env.HD_AUTH_LOCAL_ENABLE_LOGIN,
Expand Down
6 changes: 3 additions & 3 deletions src/config/database.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
* 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
*/
import { registerAs } from '@nestjs/config';
import * as Joi from 'joi';

import { DatabaseDialect } from './database-dialect.enum';
import { buildErrorMessage, parseOptionalInt } from './utils';
import { buildErrorMessage, parseOptionalNumber } from './utils';

export interface DatabaseConfig {
username: string;
Expand Down Expand Up @@ -62,7 +62,7 @@ export default registerAs('databaseConfig', () => {
password: process.env.HD_DATABASE_PASS,
database: process.env.HD_DATABASE_NAME,
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,
dialect: process.env.HD_DATABASE_DIALECT,
},
Expand Down
6 changes: 3 additions & 3 deletions src/config/hsts.config.ts
Original file line number Diff line number Diff line change
@@ -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
*/
import { registerAs } from '@nestjs/config';
import * as Joi from 'joi';

import { buildErrorMessage, parseOptionalInt } from './utils';
import { buildErrorMessage, parseOptionalNumber } from './utils';

export interface HstsConfig {
enable: boolean;
Expand All @@ -32,7 +32,7 @@ export default registerAs('hstsConfig', () => {
const hstsConfig = hstsSchema.validate(
{
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,
preload: process.env.HD_HSTS_PRELOAD,
},
Expand Down
6 changes: 4 additions & 2 deletions src/config/note.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { registerAs } from '@nestjs/config';
import * as Joi from 'joi';

import { buildErrorMessage, parseOptionalInt, toArrayConfig } from './utils';
import { buildErrorMessage, parseOptionalNumber, toArrayConfig } from './utils';

export interface NoteConfig {
forbiddenNoteIds: string[];
Expand All @@ -29,7 +29,9 @@ export default registerAs('noteConfig', () => {
const noteConfig = schema.validate(
{
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,
Expand Down
15 changes: 9 additions & 6 deletions src/config/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -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
*/
import { Loglevel } from './loglevel.enum';
import {
needToLog,
parseOptionalInt,
parseOptionalNumber,
replaceAuthErrorsWithEnvironmentVariables,
toArrayConfig,
} from './utils';
Expand Down Expand Up @@ -84,12 +84,15 @@ describe('config utils', () => {
expect(needToLog(currentLevel, Loglevel.TRACE)).toBeTruthy();
});
});
describe('parseOptionalInt', () => {
describe('parseOptionalNumber', () => {
it('returns undefined on undefined parameter', () => {
expect(parseOptionalInt(undefined)).toEqual(undefined);
expect(parseOptionalNumber(undefined)).toEqual(undefined);
});
it('correctly parses a string', () => {
expect(parseOptionalInt('42')).toEqual(42);
it('correctly parses a integer string', () => {
expect(parseOptionalNumber('42')).toEqual(42);
});
it('correctly parses a float string', () => {
expect(parseOptionalNumber('3.14')).toEqual(3.14);
});
});
});
6 changes: 3 additions & 3 deletions src/config/utils.ts
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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) {
return undefined;
}
return parseInt(value);
return Number(value);
}

0 comments on commit 45df0e6

Please sign in to comment.