From 22d5573608245d45bb2e645a9a3c8a0843fb0ae3 Mon Sep 17 00:00:00 2001 From: Antanina Druzhkina Date: Sun, 12 Apr 2026 01:37:09 +0400 Subject: [PATCH 1/2] Added validation fot ISO Date --- src/common/utilities/GeneralHelper.ts | 5 +++++ src/services/SPService.ts | 12 +++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/common/utilities/GeneralHelper.ts b/src/common/utilities/GeneralHelper.ts index 8e81b8ed3..4b3815ef4 100644 --- a/src/common/utilities/GeneralHelper.ts +++ b/src/common/utilities/GeneralHelper.ts @@ -1,4 +1,5 @@ import '../extensions/String.extensions'; +import { parseISO, isValid } from 'date-fns'; import * as strings from 'ControlStrings'; @@ -405,3 +406,7 @@ export function dateToNumber(date: string | number | Date): number { return dateObj.getTime(); } + +export function isValidISODateString(dateString: string): boolean { + return isValid(parseISO(dateString)); +} \ No newline at end of file diff --git a/src/services/SPService.ts b/src/services/SPService.ts index 6dd506945..32689ab3b 100644 --- a/src/services/SPService.ts +++ b/src/services/SPService.ts @@ -3,7 +3,7 @@ import { ISPHttpClientOptions, SPHttpClient } from "@microsoft/sp-http"; import filter from 'lodash/filter'; import find from 'lodash/find'; import { ISPContentType, ISPField, ISPList, ISPLists, IUploadImageResult, ISPViews } from "../common/SPEntities"; -import { SPHelper, urlCombine } from "../common/utilities"; +import { isValidISODateString, SPHelper, urlCombine } from "../common/utilities"; import { IContentTypesOptions, IFieldsOptions, ILibsOptions, IRenderListDataAsStreamClientFormResult, ISPService, LibsOrderBy } from "./ISPService"; import {orderBy } from '../controls/viewPicker/IViewPicker'; @@ -584,8 +584,7 @@ export default class SPService implements ISPService { if (isArray) { result[fieldName].forEach((element: any) => { // eslint-disable-line @typescript-eslint/no-explicit-any let value = element[lookupFieldName || 'Title']; - const dateVal = Date.parse(value); - if (!Number.isNaN(dateVal)) { + if (isValidISODateString(value)) { value = new Date(value).toLocaleDateString(); } lookups.push({ key: element.ID, name: value }); @@ -595,10 +594,9 @@ export default class SPService implements ISPService { else { const singleItem = result[fieldName]; let value = singleItem[lookupFieldName || 'Title']; - const dateVal = Date.parse(value); - if (!Number.isNaN(dateVal)) { - value = new Date(value).toLocaleDateString(); - } + if (isValidISODateString(value)) { + value = new Date(value).toLocaleDateString(); + } lookups.push({ key: singleItem.ID, name: value }); } return lookups; From 57a6a2cad2f2789a8dd651c2766cf4128a2039e1 Mon Sep 17 00:00:00 2001 From: Antanina Druzhkina Date: Sun, 12 Apr 2026 02:08:52 +0400 Subject: [PATCH 2/2] refactor getLookupValues --- src/services/SPService.ts | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/services/SPService.ts b/src/services/SPService.ts index 32689ab3b..5b0f25aa5 100644 --- a/src/services/SPService.ts +++ b/src/services/SPService.ts @@ -578,27 +578,15 @@ export default class SPService implements ISPService { if (data.ok) { const result = await data.json(); if (result && result[fieldName]) { - const lookups = []; - const isArray = Array.isArray(result[fieldName]); - //multiselect lookups are arrays - if (isArray) { - result[fieldName].forEach((element: any) => { // eslint-disable-line @typescript-eslint/no-explicit-any - let value = element[lookupFieldName || 'Title']; - if (isValidISODateString(value)) { - value = new Date(value).toLocaleDateString(); - } - lookups.push({ key: element.ID, name: value }); - }); - } - //single select lookups are objects - else { - const singleItem = result[fieldName]; - let value = singleItem[lookupFieldName || 'Title']; - if (isValidISODateString(value)) { + const lookups: { key: number; name: string }[] = []; + const items = Array.isArray(result[fieldName]) ? result[fieldName] : Array.of(result[fieldName]); + items.forEach((element: any) => { // eslint-disable-line @typescript-eslint/no-explicit-any + let value = element[lookupFieldName || 'Title']; + if (isValidISODateString(value)) { value = new Date(value).toLocaleDateString(); - } - lookups.push({ key: singleItem.ID, name: value }); - } + } + lookups.push({ key: element.ID, name: value }); + }); return lookups; } }