Skip to content

Commit 8575f6b

Browse files
committed
perf: [PROD-10196] convert min & max dates before validating all rows
1 parent 3af2701 commit 8575f6b

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/FileUtils/AgGridUtils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) Cosmo Tech.
22
// Licensed under the MIT license.
3+
import DateUtils from '../DateUtils/DateUtils';
34
import { ValidationUtils } from '../ValidationUtils';
45
import { Error as PanelError } from '../models';
56
import CSV from './CSVUtils';
@@ -101,6 +102,14 @@ const _validateFormat = (rows, hasHeader, cols, options) => {
101102
maxValue: col.maxValue,
102103
},
103104
}));
105+
// For date columns, convert min & max values to do it only once
106+
colMeta.forEach((col) => {
107+
if (col.type === 'date' && options?.dateFormat) {
108+
const colOptions = col.colOptions;
109+
if (colOptions.minValue != null) colOptions.minDate = DateUtils.parse(colOptions.minValue, options?.dateFormat);
110+
if (colOptions.maxValue != null) colOptions.maxDate = DateUtils.parse(colOptions.maxValue, options?.dateFormat);
111+
}
112+
});
104113

105114
for (let rowIndex = startIndex; rowIndex < rows.length; rowIndex++) {
106115
const row = rows[rowIndex];

src/ValidationUtils/ValidationUtils.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ const castToDate = (dateOrStrValue, dateFormat) => {
6969
return DateUtils.parse(dateOrStrValue, dateFormat);
7070
};
7171

72-
const isDateInRange = (value, minValue, maxValue, dateFormat) => {
73-
const minDate = castToDate(minValue, dateFormat);
74-
const maxDate = castToDate(maxValue, dateFormat);
72+
const isDateInRange = (value, minDate, maxDate, dateFormat) => {
7573
const format = DateUtils.format;
7674
if (value == null) return null;
7775
if (dateFormat == null) return forgeConfigError("Missing option dateFormat, can't perform date validation.");
@@ -97,8 +95,11 @@ const isValid = (dataStr, type, options, canBeEmpty = false) => {
9795
if (!options?.dateFormat) return forgeConfigError("Missing option dateFormat, can't perform date validation.");
9896

9997
const valueAsDate = DateUtils.parse(dataStr, options?.dateFormat);
100-
if (isNaN(valueAsDate.getTime())) return forgeTypeError(dataStr, type, options);
101-
return isDateInRange(valueAsDate, options?.minValue, options?.maxValue, options?.dateFormat);
98+
if (isNaN(valueAsDate.getTime())) return forgeTypeError(dataStr, type, options); // Invalid date
99+
100+
const minDate = options?.minDate ?? castToDate(options?.minValue, options?.dateFormat);
101+
const maxDate = options?.maxDate ?? castToDate(options?.maxValue, options?.dateFormat);
102+
return isDateInRange(valueAsDate, minDate, maxDate, options?.dateFormat);
102103
}
103104
case 'enum':
104105
if (!options.enumValues) return forgeConfigError("Missing option enumValues, can't perform enum validation.");

0 commit comments

Comments
 (0)