From: Jérôme Benoit Date: Sun, 30 Jul 2023 22:48:31 +0000 (+0200) Subject: fix: add date validation check in date conversion helper X-Git-Tag: v1.2.20~68 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=43ff25b8055903d84a99969412c36196e979c6bd;p=e-mobility-charging-stations-simulator.git fix: add date validation check in date conversion helper Signed-off-by: Jérôme Benoit --- diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index c5a276bc..b619144b 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -80,7 +80,11 @@ export const convertToDate = ( return value as Date; } if (isString(value) || typeof value === 'number') { - return new Date(value!); + value = new Date(value as string | number); + if (isNaN(value.getTime())) { + throw new Error(`Cannot convert to date: ${String(value)}`); + } + return value; } return null; }; @@ -100,8 +104,7 @@ export const convertToInt = (value: unknown): number => { changedValue = parseInt(value as string); } if (isNaN(changedValue)) { - // eslint-disable-next-line @typescript-eslint/no-base-to-string - throw new Error(`Cannot convert to integer: ${value.toString()}`); + throw new Error(`Cannot convert to integer: ${String(value)}`); } return changedValue; }; @@ -115,8 +118,7 @@ export const convertToFloat = (value: unknown): number => { changedValue = parseFloat(value as string); } if (isNaN(changedValue)) { - // eslint-disable-next-line @typescript-eslint/no-base-to-string - throw new Error(`Cannot convert to float: ${value.toString()}`); + throw new Error(`Cannot convert to float: ${String(value)}`); } return changedValue; };