Skip to content

Commit 801bbdb

Browse files
refactor: convert validation validators from Flow to TypeScript (#4323)
* refactor: convert validation validators from Flow to TypeScript - Convert areRelativeRangeValuesSupported.js to TypeScript - Convert all files in validators/form folder to TypeScript: - dateValidator.ts - ageValidator.ts - dateTimeValidator.ts - getDateRangeValidator.ts - getDateTimeRangeValidator.ts - getNumberRangeValidator.ts - getTimeRangeValidator.ts - isValidNonFutureDate.ts - isValidNonFutureAge.ts - expiredPeriod.ts - index.ts - Remove all original .js files - Update type definitions to be compatible with existing codebase - Follow minimalistic conversion approach without moving code around - Maintain existing functionality and API compatibility Co-Authored-By: [email protected] <[email protected]> * refactor: remove original areRelativeRangeValuesSupported.js file Complete the Flow to TypeScript conversion by removing the original .js file Co-Authored-By: [email protected] <[email protected]> * fix: update TeiSearchResults types to accept null selectedProgramId - Change selectedProgramId type from string to string | null - Fixes TypeScript compilation error in TeiSearch component - Resolves type mismatch where TeiSearch passes string | null | undefined but TeiSearchResults expected only string | undefined Co-Authored-By: [email protected] <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
1 parent 827b2da commit 801bbdb

File tree

12 files changed

+48
-94
lines changed

12 files changed

+48
-94
lines changed

src/core_modules/capture-core/utils/validation/validators/areRelativeRangeValuesSupported.js renamed to src/core_modules/capture-core/utils/validation/validators/areRelativeRangeValuesSupported.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// @flow
2-
3-
export const areRelativeRangeValuesSupported = (startBuffer: ?number, endBuffer: ?number) =>
1+
export const areRelativeRangeValuesSupported = (startBuffer: number | null | undefined, endBuffer: number | null | undefined) =>
42
startBuffer !== undefined &&
53
startBuffer !== null &&
64
Number.isInteger(startBuffer) &&

src/core_modules/capture-core/utils/validation/validators/form/ageValidator.js renamed to src/core_modules/capture-core/utils/validation/validators/form/ageValidator.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
// @flow
21
import i18n from '@dhis2/d2-i18n';
32
import { isValidZeroOrPositiveInteger } from 'capture-core-utils/validators/form';
43
import { isValidDate } from './dateValidator';
54

65
type AgeValues = {
7-
date?: ?string,
8-
years?: ?string,
9-
months?: ?string,
10-
days?: ?string,
6+
date?: string | null;
7+
years?: string | null;
8+
months?: string | null;
9+
days?: string | null;
1110
}
1211

1312
const errorMessages = {
@@ -18,12 +17,12 @@ const errorMessages = {
1817

1918
};
2019

21-
function isValidNumberPart(value: ?string) {
20+
function isValidNumberPart(value: string | null | undefined) {
2221
return !value || isValidZeroOrPositiveInteger(value);
2322
}
2423

25-
function validateNumbers(years: ?string, months: ?string, days: ?string) {
26-
const errorResult = [];
24+
function validateNumbers(years: string | null | undefined, months: string | null | undefined, days: string | null | undefined) {
25+
const errorResult: any[] = [];
2726

2827
if (!isValidNumberPart(years)) {
2928
errorResult.push({ years: errorMessages.years });
@@ -38,14 +37,13 @@ function validateNumbers(years: ?string, months: ?string, days: ?string) {
3837
if (errorResult.length > 0) {
3938
return {
4039
valid: false,
41-
// $FlowFixMe[exponential-spread] automated comment
4240
errorMessage: errorResult.reduce((map, error) => ({ ...map, ...error }), {}),
4341
};
4442
}
4543
return { valid: true };
4644
}
4745

48-
function validateDate(date: ?string, internalComponentError?: ?{error: ?string, errorCode: ?string}) {
46+
function validateDate(date: string | null | undefined, internalComponentError?: {error?: string | null | undefined, errorCode?: string | null | undefined} | null | undefined) {
4947
const { valid } = isValidDate(date, internalComponentError);
5048
return valid ?
5149
{ valid: true } :
@@ -57,7 +55,7 @@ function isAllEmpty(value: AgeValues) {
5755
}
5856

5957

60-
export function isValidAge(value: Object, internalComponentError?: ?{error: ?string, errorCode: ?string}) {
58+
export function isValidAge(value: any, internalComponentError?: {error?: string | null | undefined, errorCode?: string | null | undefined} | null | undefined) {
6159
if (isAllEmpty(value)) {
6260
return false;
6361
}

src/core_modules/capture-core/utils/validation/validators/form/dateTimeValidator.js renamed to src/core_modules/capture-core/utils/validation/validators/form/dateTimeValidator.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
// @flow
21
import i18n from '@dhis2/d2-i18n';
32
import { isValidTime } from 'capture-core-utils/validators/form';
43
import { isValidDate } from './dateValidator';
54

65
type DateTimeValue = {
7-
date?: ?string,
8-
time?: ?string,
6+
date?: string | null;
7+
time?: string | null;
98
};
109

1110
type ValidationResult = {
12-
valid: boolean,
11+
valid: boolean;
1312
errorMessage?: {
14-
timeError?: ?string,
15-
dateError?: ?string
16-
},
17-
data?: any,
13+
timeError?: string | null;
14+
dateError?: string | null;
15+
};
16+
data?: any;
1817
};
1918

2019
const CUSTOM_VALIDATION_MESSAGES = {
@@ -23,8 +22,8 @@ const CUSTOM_VALIDATION_MESSAGES = {
2322
MISSING_DATE: i18n.t('Please enter a date'),
2423
};
2524

26-
export function isValidDateTime(value: ?DateTimeValue,
27-
internalComponentError?: ?{error: ?string, errorCode: ?string}): ValidationResult {
25+
export function isValidDateTime(value: DateTimeValue | null | undefined,
26+
internalComponentError?: {error?: string | null | undefined, errorCode?: string | null | undefined} | null | undefined): ValidationResult {
2827
if (!value) {
2928
return { valid: true };
3029
}
@@ -40,7 +39,7 @@ export function isValidDateTime(value: ?DateTimeValue,
4039
} else {
4140
const dateValidation = isValidDate(date, internalComponentError);
4241
if (!dateValidation.valid) {
43-
dateError = dateValidation?.errorMessage;
42+
dateError = dateValidation?.errorMessage || '';
4443
isValid = false;
4544
}
4645
}

src/core_modules/capture-core/utils/validation/validators/form/dateValidator.js renamed to src/core_modules/capture-core/utils/validation/validators/form/dateValidator.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// @flow
2-
3-
export function isValidDate(value: ?string, internalComponentError?: ?{error: ?string, errorCode: ?string}) {
1+
export function isValidDate(value: string | null | undefined, internalComponentError?: {error?: string | null | undefined, errorCode?: string | null | undefined} | null | undefined) {
42
if (!value) {
53
return { valid: false, errorMessage: null };
64
}

src/core_modules/capture-core/utils/validation/validators/form/expiredPeriod.js renamed to src/core_modules/capture-core/utils/validation/validators/form/expiredPeriod.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
// @flow
21
import { getFixedPeriodByDate } from '@dhis2/multi-calendar-dates';
32
import { convertClientToServer, convertClientToView } from '../../../../converters';
43
import { dataElementTypes } from '../../../../metaData';
54
import { dateUtils } from '../../../../rules/converters';
65

76
export const isValidPeriod = (
87
reportDate: string,
9-
expiryPeriod?: ?{
10-
expiryPeriodType: ?string,
11-
expiryDays: ?number,
12-
},
8+
expiryPeriod?: {
9+
expiryPeriodType?: string | null | undefined;
10+
expiryDays?: number | null | undefined;
11+
} | null | undefined,
1312
) => {
1413
if (!expiryPeriod) {
1514
return { isWithinValidPeriod: true, firstValidDate: undefined };
@@ -21,17 +20,17 @@ export const isValidPeriod = (
2120
return { isWithinValidPeriod: true, firstValidDate: undefined };
2221
}
2322

24-
const reportDateServer = ((convertClientToServer(reportDate, dataElementTypes.DATE): any): string);
23+
const reportDateServer = convertClientToServer(reportDate, dataElementTypes.DATE) as string;
2524
const today = dateUtils.getToday();
2625

2726
const threshold = expiryDays
2827
? dateUtils.addDays(today, -expiryDays)
2928
: today;
3029

3130
const thresholdPeriod = getFixedPeriodByDate({
32-
periodType: expiryPeriodType,
31+
periodType: expiryPeriodType as any,
3332
date: threshold,
34-
calendar: 'gregorian',
33+
calendar: 'gregorian' as any,
3534
});
3635

3736
const firstValidDateServer = thresholdPeriod.startDate;

src/core_modules/capture-core/utils/validation/validators/form/getDateRangeValidator.js renamed to src/core_modules/capture-core/utils/validation/validators/form/getDateRangeValidator.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
// @flow
21
import { Temporal } from '@js-temporal/polyfill';
32
import { isValidDate } from './dateValidator';
43
import { convertLocalToIsoCalendar } from '../../../converters/date';
5-
/**
6-
*
7-
* @export
8-
* @param {string} value
9-
* @returns {boolean}
10-
*/
114

12-
function isValidDateWithEmptyCheck(value: ?string, internalError?: ?{error: ?string, errorCode: ?string}) {
5+
function isValidDateWithEmptyCheck(value: string | null | undefined, internalError?: {error?: string | null | undefined, errorCode?: string | null | undefined} | null | undefined) {
136
return isValidDate(value, internalError);
147
}
158

169
export const getDateRangeValidator = (invalidDateMessage: string) =>
17-
(value: { from?: ?string, to?: ?string}, internalComponentError?: ?{fromError: ?{error: ?string, errorCode: ?string}, toError: ?{error: ?string, errorCode: ?string}}) => {
18-
const errorResult = [];
10+
(value: { from?: string | null, to?: string | null}, internalComponentError?: {fromError?: {error?: string | null | undefined, errorCode?: string | null | undefined} | null | undefined, toError?: {error?: string | null | undefined, errorCode?: string | null | undefined} | null | undefined} | null | undefined) => {
11+
const errorResult: any[] = [];
1912
if (!isValidDateWithEmptyCheck(value.from, internalComponentError?.fromError).valid) {
2013
errorResult.push({ from: invalidDateMessage });
2114
}
@@ -27,14 +20,12 @@ export const getDateRangeValidator = (invalidDateMessage: string) =>
2720
if (errorResult.length > 0) {
2821
return {
2922
valid: false,
30-
// $FlowFixMe[exponential-spread] automated comment
3123
errorMessage: errorResult.reduce((map, error) => ({ ...map, ...error }), {}),
3224
};
3325
}
3426
const { from, to } = value;
35-
// $FlowFixMe
36-
const isoFrom = convertLocalToIsoCalendar(from);
37-
const isoTo = convertLocalToIsoCalendar(to);
27+
const isoFrom = convertLocalToIsoCalendar(from || null);
28+
const isoTo = convertLocalToIsoCalendar(to || null);
3829

3930
if (!isoFrom || !isoTo) {
4031
return {

src/core_modules/capture-core/utils/validation/validators/form/getDateTimeRangeValidator.js renamed to src/core_modules/capture-core/utils/validation/validators/form/getDateTimeRangeValidator.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
// @flow
21
import { Temporal } from '@js-temporal/polyfill';
32
import { convertLocalToIsoCalendar } from 'capture-core/utils/converters/date';
43
import { isValidDateTime } from './dateTimeValidator';
54

6-
function isValidDateTimeWithEmptyCheck(value: ?{date?: ?string, time?: ?string}, internalError?: ?{error: ?string, errorCode: ?string}) {
5+
function isValidDateTimeWithEmptyCheck(value: {date?: string | null, time?: string | null} | null | undefined, internalError?: {error?: string | null | undefined, errorCode?: string | null | undefined} | null | undefined) {
76
return isValidDateTime(value, internalError);
87
}
9-
/* eslint-disable complexity */
10-
const convertDateTimeToIsoTemporal = (value: ?Object) => {
8+
9+
const convertDateTimeToIsoTemporal = (value: any | null | undefined) => {
1110
if (!value || !value.date || !value.time) {
1211
return null;
1312
}
@@ -46,15 +45,14 @@ const convertDateTimeToIsoTemporal = (value: ?Object) => {
4645
};
4746

4847
export const getDateTimeRangeValidator = (invalidDateTimeMessage: string) =>
49-
// eslint-disable-next-line complexity
50-
(value: { from?: ?Object, to?: ?Object }, internalComponentError?: ?{fromDateError: ?{error: ?string, errorCode: ?string}, toDateError: ?{error: ?string, errorCode: ?string}}) => {
48+
(value: { from?: any | null, to?: any | null }, internalComponentError?: {fromDateError?: {error?: string | null | undefined, errorCode?: string | null | undefined} | null | undefined, toDateError?: {error?: string | null | undefined, errorCode?: string | null | undefined} | null | undefined} | null | undefined) => {
5149
if (!value?.from && value?.to) {
5250
return {
5351
valid: false,
5452
errorMessage: { from: invalidDateTimeMessage, to: invalidDateTimeMessage },
5553
};
5654
}
57-
const errorResult = [];
55+
const errorResult: any[] = [];
5856
if (!isValidDateTimeWithEmptyCheck(value?.from, internalComponentError?.fromDateError).valid) {
5957
errorResult.push({ from: invalidDateTimeMessage });
6058
}
@@ -66,7 +64,6 @@ export const getDateTimeRangeValidator = (invalidDateTimeMessage: string) =>
6664
if (errorResult.length > 0) {
6765
return {
6866
valid: false,
69-
// $FlowFixMe[exponential-spread] automated comment
7067
errorMessage: errorResult.reduce((map, error) => ({ ...map, ...error }), {}),
7168
};
7269
}

src/core_modules/capture-core/utils/validation/validators/form/getNumberRangeValidator.js renamed to src/core_modules/capture-core/utils/validation/validators/form/getNumberRangeValidator.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
// @flow
21
import { parseNumber } from 'capture-core-utils/parsers';
3-
/**
4-
*
5-
* @export
6-
* @param {string} value
7-
* @returns {boolean}
8-
*/
92

10-
11-
function isValid(value: any, validatorContainer: Object) {
3+
function isValid(value: any, validatorContainer: any) {
124
return value && validatorContainer.validator(value);
135
}
146

15-
export const getNumberRangeValidator = (validatorContainer: Object) =>
7+
export const getNumberRangeValidator = (validatorContainer: any) =>
168
(value: { from?: any, to?: any}) => {
17-
const errorResult = [];
9+
const errorResult: any[] = [];
1810

1911
if (!isValid(value.from, validatorContainer)) {
2012
errorResult.push({ from: validatorContainer.message });
@@ -25,10 +17,8 @@ export const getNumberRangeValidator = (validatorContainer: Object) =>
2517
if (errorResult.length > 0) {
2618
return {
2719
valid: false,
28-
// $FlowFixMe[exponential-spread] automated comment
2920
errorMessage: errorResult.reduce((map, error) => ({ ...map, ...error }), {}),
3021
};
3122
}
32-
// $FlowFixMe
3323
return parseNumber(value.from) <= parseNumber(value.to);
3424
};

src/core_modules/capture-core/utils/validation/validators/form/getTimeRangeValidator.js renamed to src/core_modules/capture-core/utils/validation/validators/form/getTimeRangeValidator.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
// @flow
21
import { isValidTime } from 'capture-core-utils/validators/form';
32
import moment from 'moment';
4-
/**
5-
*
6-
* @export
7-
* @param {string} value
8-
* @returns {boolean}
9-
*/
103

11-
function isValidTimeWithEmptyCheck(value: ?string) {
4+
function isValidTimeWithEmptyCheck(value: string | null | undefined) {
125
return value && isValidTime(value);
136
}
147

@@ -25,16 +18,14 @@ const convertTimeToMoment = (value: string, baseDate: any) => {
2518
minutes = value.substring(3, 4);
2619
}
2720
const momentDateTime = moment(baseDate);
28-
// $FlowFixMe
2921
momentDateTime.hour(hour);
30-
// $FlowFixMe
3122
momentDateTime.minute(minutes);
3223
return momentDateTime;
3324
};
3425

3526
export const getTimeRangeValidator = (invalidTimeMessage: string) =>
36-
(value: { from?: ?string, to?: ?string}) => {
37-
const errorResult = [];
27+
(value: { from?: string | null, to?: string | null}) => {
28+
const errorResult: any[] = [];
3829
if (!isValidTimeWithEmptyCheck(value.from)) {
3930
errorResult.push({ from: invalidTimeMessage });
4031
}
@@ -46,16 +37,12 @@ export const getTimeRangeValidator = (invalidTimeMessage: string) =>
4637
if (errorResult.length > 0) {
4738
return {
4839
valid: false,
49-
// $FlowFixMe[exponential-spread] automated comment
5040
errorMessage: errorResult.reduce((map, error) => ({ ...map, ...error }), {}),
5141
};
5242
}
5343
const baseDate = moment();
5444

55-
// $FlowFixMe
56-
const fromParsed = convertTimeToMoment(value.from, baseDate);
57-
// $FlowFixMe
58-
const toParsed = convertTimeToMoment(value.to, baseDate);
59-
// $FlowFixMe
45+
const fromParsed = convertTimeToMoment(value.from || '', baseDate);
46+
const toParsed = convertTimeToMoment(value.to || '', baseDate);
6047
return fromParsed <= toParsed;
6148
};

src/core_modules/capture-core/utils/validation/validators/form/index.js renamed to src/core_modules/capture-core/utils/validation/validators/form/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @flow
21
export { isValidDate } from './dateValidator';
32
export { isValidDateTime } from './dateTimeValidator';
43
export { isValidAge } from './ageValidator';

0 commit comments

Comments
 (0)