From 43ff25b8055903d84a99969412c36196e979c6bd Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 31 Jul 2023 00:48:31 +0200 Subject: [PATCH] fix: add date validation check in date conversion helper MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils/Utils.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) 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; }; -- 2.34.1