import { fileURLToPath } from 'node:url';
import type { DefinedError, ErrorObject, JSONSchemaType } from 'ajv';
+import { isDate } from 'date-fns';
import { OCPP16Constants } from './1.6/OCPP16Constants';
import { OCPP20Constants } from './2.0/OCPP20Constants';
public static convertDateToISOString<T extends JsonType>(obj: T): void {
for (const key in obj) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
- if (obj![key] instanceof Date) {
+ if (isDate(obj![key])) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
(obj![key] as string) = (obj![key] as Date).toISOString();
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
-import { hoursToMilliseconds, hoursToSeconds } from 'date-fns';
+import { hoursToMilliseconds, hoursToSeconds, isValid } from 'date-fns';
import { expect } from 'expect';
import { Constants } from '../../src/utils/Constants';
it('Verify convertToDate()', () => {
expect(convertToDate(undefined)).toBe(undefined);
expect(convertToDate(null)).toBe(null);
- const invalidDate = convertToDate('');
- expect(invalidDate instanceof Date && !isNaN(invalidDate.getTime())).toBe(false);
+ expect(isValid(convertToDate(''))).toBe(false);
expect(convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z'));
const dateStr = '2020-01-01T00:00:00.000Z';
let date = convertToDate(dateStr);