Feature/date range validations (#406)

* added ability to compare date field against a date range field start or end

* added a couple of comments

---------

Co-authored-by: jasquat <jasquat@users.noreply.github.com>
This commit is contained in:
jasquat 2023-07-25 13:27:07 -04:00 committed by GitHub
parent 755943d89d
commit c6b57c98fa
1 changed files with 51 additions and 27 deletions

View File

@ -275,13 +275,46 @@ export default function TaskShow() {
errors: any, errors: any,
jsonSchema: any jsonSchema: any
) => { ) => {
const fieldIdentifierToCompareWith = minimumDateCheck.replace( // field format:
/^field:/, // field:[field_name_to_use]
'' //
// if field is a range:
// field:[field_name_to_use]:[start or end]
//
// defaults to "start" in all cases
const [_, fieldIdentifierToCompareWith, startOrEnd] =
minimumDateCheck.split(':');
if (!(fieldIdentifierToCompareWith in formData)) {
errors[propertyKey].addError(
`was supposed to be compared against '${fieldIdentifierToCompareWith}' but it either doesn't have a value or does not exist`
); );
if (fieldIdentifierToCompareWith in formData) { return;
const dateToCompareWith = formData[fieldIdentifierToCompareWith]; }
if (dateToCompareWith) {
const rawDateToCompareWith = formData[fieldIdentifierToCompareWith];
if (!rawDateToCompareWith) {
errors[propertyKey].addError(
`was supposed to be compared against '${fieldIdentifierToCompareWith}' but that field did not have a value`
);
return;
}
const [startDate, endDate] =
rawDateToCompareWith.split(DATE_RANGE_DELIMITER);
let dateToCompareWith = startDate;
if (startOrEnd && startOrEnd === 'end') {
dateToCompareWith = endDate;
}
if (!dateToCompareWith) {
const errorMessage = `was supposed to be compared against '${[
fieldIdentifierToCompareWith,
startOrEnd,
].join(':')}' but that field did not have a value`;
errors[propertyKey].addError(errorMessage);
return;
}
const dateStringToCompareWith = formatDateString(dateToCompareWith); const dateStringToCompareWith = formatDateString(dateToCompareWith);
if (dateStringToCompareWith > formattedDateString) { if (dateStringToCompareWith > formattedDateString) {
let fieldToCompareWithTitle = fieldIdentifierToCompareWith; let fieldToCompareWithTitle = fieldIdentifierToCompareWith;
@ -296,16 +329,6 @@ export default function TaskShow() {
`must be equal to or greater than '${fieldToCompareWithTitle}'` `must be equal to or greater than '${fieldToCompareWithTitle}'`
); );
} }
} else {
errors[propertyKey].addError(
`was supposed to be compared against '${fieldIdentifierToCompareWith}' but that field did not have a value`
);
}
} else {
errors[propertyKey].addError(
`was supposed to be compared against '${fieldIdentifierToCompareWith}' but it either doesn't have a value or does not exist`
);
}
}; };
const checkMinimumDate = ( const checkMinimumDate = (
@ -315,6 +338,7 @@ export default function TaskShow() {
errors: any, errors: any,
jsonSchema: any jsonSchema: any
) => { ) => {
// can be either "today" or another field
let dateString = formData[propertyKey]; let dateString = formData[propertyKey];
if (dateString) { if (dateString) {
if (typeof dateString === 'string') { if (typeof dateString === 'string') {