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:
parent
adb53c722a
commit
0b296f4c46
|
@ -8,6 +8,11 @@ import TypeaheadWidget from '../rjsf/custom_widgets/TypeaheadWidget/TypeaheadWid
|
|||
import MarkDownFieldWidget from '../rjsf/custom_widgets/MarkDownFieldWidget/MarkDownFieldWidget';
|
||||
import NumericRangeField from '../rjsf/custom_widgets/NumericRangeField/NumericRangeField';
|
||||
|
||||
enum DateCheckType {
|
||||
minimum = 'minimum',
|
||||
maximum = 'maximum',
|
||||
}
|
||||
|
||||
type OwnProps = {
|
||||
id: string;
|
||||
formData: any;
|
||||
|
@ -53,9 +58,10 @@ export default function CustomForm({
|
|||
};
|
||||
|
||||
const checkFieldComparisons = (
|
||||
checkType: DateCheckType,
|
||||
formDataToCheck: any,
|
||||
propertyKey: string,
|
||||
minimumDateCheck: string,
|
||||
dateCheck: string,
|
||||
formattedDateString: string,
|
||||
errors: any,
|
||||
jsonSchema: any
|
||||
|
@ -67,8 +73,7 @@ export default function CustomForm({
|
|||
// field:[field_name_to_use]:[start or end]
|
||||
//
|
||||
// defaults to "start" in all cases
|
||||
const [_, fieldIdentifierToCompareWith, startOrEnd] =
|
||||
minimumDateCheck.split(':');
|
||||
const [_, fieldIdentifierToCompareWith, startOrEnd] = dateCheck.split(':');
|
||||
if (!(fieldIdentifierToCompareWith in formDataToCheck)) {
|
||||
errors[propertyKey].addError(
|
||||
`was supposed to be compared against '${fieldIdentifierToCompareWith}' but it either doesn't have a value or does not exist`
|
||||
|
@ -100,8 +105,6 @@ export default function CustomForm({
|
|||
return;
|
||||
}
|
||||
|
||||
const dateStringToCompareWith = formatDateString(dateToCompareWith);
|
||||
if (dateStringToCompareWith > formattedDateString) {
|
||||
let fieldToCompareWithTitle = fieldIdentifierToCompareWith;
|
||||
if (
|
||||
fieldIdentifierToCompareWith in jsonSchema.properties &&
|
||||
|
@ -110,13 +113,64 @@ export default function CustomForm({
|
|||
fieldToCompareWithTitle =
|
||||
jsonSchema.properties[fieldIdentifierToCompareWith].title;
|
||||
}
|
||||
|
||||
const dateStringToCompareWith = formatDateString(dateToCompareWith);
|
||||
if (checkType === 'minimum') {
|
||||
if (dateStringToCompareWith > formattedDateString) {
|
||||
errors[propertyKey].addError(
|
||||
`must be equal to or greater than '${fieldToCompareWithTitle}'`
|
||||
);
|
||||
}
|
||||
// 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 (dateStringToCompareWith < formattedDateString) {
|
||||
errors[propertyKey].addError(
|
||||
`must be equal to or less 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,
|
||||
propertyKey: string,
|
||||
propertyMetadata: any,
|
||||
|
@ -131,25 +185,22 @@ export default function CustomForm({
|
|||
[dateString] = dateString.split(DATE_RANGE_DELIMITER);
|
||||
}
|
||||
const formattedDateString = formatDateString(dateString);
|
||||
const minimumDateChecks = propertyMetadata.minimumDate.split(',');
|
||||
minimumDateChecks.forEach((mdc: string) => {
|
||||
if (mdc === 'today') {
|
||||
const dateTodayString = formatDateString();
|
||||
if (dateTodayString > formattedDateString) {
|
||||
errors[propertyKey].addError('must be today or after');
|
||||
let dateChecks = null;
|
||||
if (checkType === 'minimum') {
|
||||
dateChecks = propertyMetadata.minimumDate.split(',');
|
||||
} else if (checkType === 'maximum') {
|
||||
dateChecks = propertyMetadata.maximumDate.split(',');
|
||||
}
|
||||
} else if (mdc.startsWith('field:')) {
|
||||
checkFieldComparisons(
|
||||
runDateChecks(
|
||||
checkType,
|
||||
dateChecks,
|
||||
formDataToCheck,
|
||||
propertyKey,
|
||||
mdc,
|
||||
formattedDateString,
|
||||
errors,
|
||||
jsonSchema
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// a required boolean field (checkbox) with default value of false
|
||||
|
@ -226,7 +277,18 @@ export default function CustomForm({
|
|||
currentUiSchema = uiSchemaToUse[propertyKey];
|
||||
}
|
||||
if ('minimumDate' in propertyMetadata) {
|
||||
checkMinimumDate(
|
||||
checkDateValidations(
|
||||
DateCheckType.minimum,
|
||||
formDataToCheck,
|
||||
propertyKey,
|
||||
propertyMetadata,
|
||||
errors,
|
||||
jsonSchemaToUse
|
||||
);
|
||||
}
|
||||
if ('maximumDate' in propertyMetadata) {
|
||||
checkDateValidations(
|
||||
DateCheckType.maximum,
|
||||
formDataToCheck,
|
||||
propertyKey,
|
||||
propertyMetadata,
|
||||
|
|
Loading…
Reference in New Issue