added validation for maximum date w/ burnettk (#851)

* added validation for maximum date w/ burnettk

* Update spiffworkflow-frontend/src/components/CustomForm.tsx

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update spiffworkflow-frontend/src/components/CustomForm.tsx

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

---------

Co-authored-by: jasquat <jasquat@users.noreply.github.com>
Co-authored-by: Kevin Burnett <18027+burnettk@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
jasquat 2024-01-03 14:37:29 -05:00 committed by GitHub
parent adb53c722a
commit 0b296f4c46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 96 additions and 34 deletions

View File

@ -8,6 +8,11 @@ import TypeaheadWidget from '../rjsf/custom_widgets/TypeaheadWidget/TypeaheadWid
import MarkDownFieldWidget from '../rjsf/custom_widgets/MarkDownFieldWidget/MarkDownFieldWidget'; import MarkDownFieldWidget from '../rjsf/custom_widgets/MarkDownFieldWidget/MarkDownFieldWidget';
import NumericRangeField from '../rjsf/custom_widgets/NumericRangeField/NumericRangeField'; import NumericRangeField from '../rjsf/custom_widgets/NumericRangeField/NumericRangeField';
enum DateCheckType {
minimum = 'minimum',
maximum = 'maximum',
}
type OwnProps = { type OwnProps = {
id: string; id: string;
formData: any; formData: any;
@ -53,9 +58,10 @@ export default function CustomForm({
}; };
const checkFieldComparisons = ( const checkFieldComparisons = (
checkType: DateCheckType,
formDataToCheck: any, formDataToCheck: any,
propertyKey: string, propertyKey: string,
minimumDateCheck: string, dateCheck: string,
formattedDateString: string, formattedDateString: string,
errors: any, errors: any,
jsonSchema: any jsonSchema: any
@ -67,8 +73,7 @@ export default function CustomForm({
// field:[field_name_to_use]:[start or end] // field:[field_name_to_use]:[start or end]
// //
// defaults to "start" in all cases // defaults to "start" in all cases
const [_, fieldIdentifierToCompareWith, startOrEnd] = const [_, fieldIdentifierToCompareWith, startOrEnd] = dateCheck.split(':');
minimumDateCheck.split(':');
if (!(fieldIdentifierToCompareWith in formDataToCheck)) { if (!(fieldIdentifierToCompareWith in formDataToCheck)) {
errors[propertyKey].addError( errors[propertyKey].addError(
`was supposed to be compared against '${fieldIdentifierToCompareWith}' but it either doesn't have a value or does not exist` `was supposed to be compared against '${fieldIdentifierToCompareWith}' but it either doesn't have a value or does not exist`
@ -100,23 +105,72 @@ export default function CustomForm({
return; return;
} }
let fieldToCompareWithTitle = fieldIdentifierToCompareWith;
if (
fieldIdentifierToCompareWith in jsonSchema.properties &&
jsonSchema.properties[fieldIdentifierToCompareWith].title
) {
fieldToCompareWithTitle =
jsonSchema.properties[fieldIdentifierToCompareWith].title;
}
const dateStringToCompareWith = formatDateString(dateToCompareWith); const dateStringToCompareWith = formatDateString(dateToCompareWith);
if (dateStringToCompareWith > formattedDateString) { if (checkType === 'minimum') {
let fieldToCompareWithTitle = fieldIdentifierToCompareWith; if (dateStringToCompareWith > formattedDateString) {
if ( errors[propertyKey].addError(
fieldIdentifierToCompareWith in jsonSchema.properties && `must be equal to or greater than '${fieldToCompareWithTitle}'`
jsonSchema.properties[fieldIdentifierToCompareWith].title );
) { }
fieldToCompareWithTitle = // best NOT to merge this with nested if statement in case we add more or move code around
jsonSchema.properties[fieldIdentifierToCompareWith].title; // eslint-disable-next-line sonarjs/no-collapsible-if
} else if (checkType === 'maximum') {
if (dateStringToCompareWith < formattedDateString) {
errors[propertyKey].addError(
`must be equal to or less than '${fieldToCompareWithTitle}'`
);
} }
errors[propertyKey].addError(
`must be equal to or greater than '${fieldToCompareWithTitle}'`
);
} }
}; };
const checkMinimumDate = ( const runDateChecks = (
checkType: DateCheckType,
dateChecks: string[],
formDataToCheck: any,
propertyKey: string,
formattedDateString: string,
errors: any,
jsonSchema: any
) => {
dateChecks.forEach((mdc: string) => {
if (mdc === 'today') {
const dateTodayString = formatDateString();
if (checkType === 'minimum') {
if (dateTodayString > formattedDateString) {
errors[propertyKey].addError('must be today or after');
}
// best NOT to merge this with nested if statement in case we add more or move code around
// eslint-disable-next-line sonarjs/no-collapsible-if
} else if (checkType === 'maximum') {
if (dateTodayString < formattedDateString) {
errors[propertyKey].addError('must be today or before');
}
}
} else if (mdc.startsWith('field:')) {
checkFieldComparisons(
checkType,
formDataToCheck,
propertyKey,
mdc,
formattedDateString,
errors,
jsonSchema
);
}
});
};
const checkDateValidations = (
checkType: DateCheckType,
formDataToCheck: any, formDataToCheck: any,
propertyKey: string, propertyKey: string,
propertyMetadata: any, propertyMetadata: any,
@ -131,24 +185,21 @@ export default function CustomForm({
[dateString] = dateString.split(DATE_RANGE_DELIMITER); [dateString] = dateString.split(DATE_RANGE_DELIMITER);
} }
const formattedDateString = formatDateString(dateString); const formattedDateString = formatDateString(dateString);
const minimumDateChecks = propertyMetadata.minimumDate.split(','); let dateChecks = null;
minimumDateChecks.forEach((mdc: string) => { if (checkType === 'minimum') {
if (mdc === 'today') { dateChecks = propertyMetadata.minimumDate.split(',');
const dateTodayString = formatDateString(); } else if (checkType === 'maximum') {
if (dateTodayString > formattedDateString) { dateChecks = propertyMetadata.maximumDate.split(',');
errors[propertyKey].addError('must be today or after'); }
} runDateChecks(
} else if (mdc.startsWith('field:')) { checkType,
checkFieldComparisons( dateChecks,
formDataToCheck, formDataToCheck,
propertyKey, propertyKey,
mdc, formattedDateString,
formattedDateString, errors,
errors, jsonSchema
jsonSchema );
);
}
});
} }
}; };
@ -226,7 +277,18 @@ export default function CustomForm({
currentUiSchema = uiSchemaToUse[propertyKey]; currentUiSchema = uiSchemaToUse[propertyKey];
} }
if ('minimumDate' in propertyMetadata) { if ('minimumDate' in propertyMetadata) {
checkMinimumDate( checkDateValidations(
DateCheckType.minimum,
formDataToCheck,
propertyKey,
propertyMetadata,
errors,
jsonSchemaToUse
);
}
if ('maximumDate' in propertyMetadata) {
checkDateValidations(
DateCheckType.maximum,
formDataToCheck, formDataToCheck,
propertyKey, propertyKey,
propertyMetadata, propertyMetadata,