From b9e70d12a6ef6b61f7e82f71044b0ca1be14da9d Mon Sep 17 00:00:00 2001 From: jasquat <2487833+jasquat@users.noreply.github.com> Date: Thu, 2 May 2024 18:08:36 +0000 Subject: [PATCH] do not invalidate a numeric range field if it is unset and not required (#1478) Co-authored-by: jasquat --- .../src/components/CustomForm.tsx | 81 ++++++++++--------- .../NumericRangeField/NumericRangeField.tsx | 3 + 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/spiffworkflow-frontend/src/components/CustomForm.tsx b/spiffworkflow-frontend/src/components/CustomForm.tsx index a8a00612e..ea115e6ad 100644 --- a/spiffworkflow-frontend/src/components/CustomForm.tsx +++ b/spiffworkflow-frontend/src/components/CustomForm.tsx @@ -256,6 +256,7 @@ export default function CustomForm({ errors: any, jsonSchema: any, _uiSchemaPassedIn?: any + // eslint-disable-next-line sonarjs/cognitive-complexity ) => { if ( jsonSchema.required && @@ -265,43 +266,51 @@ export default function CustomForm({ ) { errors[propertyKey].addError('must have valid Minimum and Maximum'); } - if ( - !formDataToCheck[propertyKey].min?.toString().match(matchNumberRegex) || - !formDataToCheck[propertyKey].max?.toString().match(matchNumberRegex) - ) { - errors[propertyKey].addError('must have valid numbers'); + if (formDataToCheck[propertyKey].min) { + if ( + !formDataToCheck[propertyKey].min.toString().match(matchNumberRegex) + ) { + errors[propertyKey].addError('must have valid numbers'); + } + if ( + formDataToCheck[propertyKey].min < + jsonSchema.properties[propertyKey].minimum + ) { + errors[propertyKey].addError( + `must have min greater than or equal to ${jsonSchema.properties[propertyKey].minimum}` + ); + } + if ( + formDataToCheck[propertyKey].min > + jsonSchema.properties[propertyKey].maximum + ) { + errors[propertyKey].addError( + `must have min less than or equal to ${jsonSchema.properties[propertyKey].maximum}` + ); + } } - if ( - formDataToCheck[propertyKey].min < - jsonSchema.properties[propertyKey].minimum - ) { - errors[propertyKey].addError( - `must have min greater than or equal to ${jsonSchema.properties[propertyKey].minimum}` - ); - } - if ( - formDataToCheck[propertyKey].min > - jsonSchema.properties[propertyKey].maximum - ) { - errors[propertyKey].addError( - `must have min less than or equal to ${jsonSchema.properties[propertyKey].maximum}` - ); - } - if ( - formDataToCheck[propertyKey].max < - jsonSchema.properties[propertyKey].minimum - ) { - errors[propertyKey].addError( - `must have max greater than or equal to ${jsonSchema.properties[propertyKey].minimum}` - ); - } - if ( - formDataToCheck[propertyKey].max > - jsonSchema.properties[propertyKey].maximum - ) { - errors[propertyKey].addError( - `must have max less than or equal to ${jsonSchema.properties[propertyKey].maximum}` - ); + if (formDataToCheck[propertyKey].max) { + if ( + !formDataToCheck[propertyKey].max.toString().match(matchNumberRegex) + ) { + errors[propertyKey].addError('must have valid numbers'); + } + if ( + formDataToCheck[propertyKey].max < + jsonSchema.properties[propertyKey].minimum + ) { + errors[propertyKey].addError( + `must have max greater than or equal to ${jsonSchema.properties[propertyKey].minimum}` + ); + } + if ( + formDataToCheck[propertyKey].max > + jsonSchema.properties[propertyKey].maximum + ) { + errors[propertyKey].addError( + `must have max less than or equal to ${jsonSchema.properties[propertyKey].maximum}` + ); + } } if (formDataToCheck[propertyKey].min > formDataToCheck[propertyKey].max) { errors[propertyKey].addError(`must have min less than or equal to max`); diff --git a/spiffworkflow-frontend/src/rjsf/custom_widgets/NumericRangeField/NumericRangeField.tsx b/spiffworkflow-frontend/src/rjsf/custom_widgets/NumericRangeField/NumericRangeField.tsx index 790ecc016..601426a16 100644 --- a/spiffworkflow-frontend/src/rjsf/custom_widgets/NumericRangeField/NumericRangeField.tsx +++ b/spiffworkflow-frontend/src/rjsf/custom_widgets/NumericRangeField/NumericRangeField.tsx @@ -83,6 +83,9 @@ export default function NumericRangeField({ }; const parseNumberString = (numberString: string) => { + if (!numberString.match(matchNumberRegex)) { + return numberString; + } if ( (numberString === '-' && numberString.length === 1) || numberString.endsWith('.')