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:
parent
755943d89d
commit
c6b57c98fa
|
@ -275,36 +275,59 @@ 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:
|
||||||
if (fieldIdentifierToCompareWith in formData) {
|
// field:[field_name_to_use]:[start or end]
|
||||||
const dateToCompareWith = formData[fieldIdentifierToCompareWith];
|
//
|
||||||
if (dateToCompareWith) {
|
// defaults to "start" in all cases
|
||||||
const dateStringToCompareWith = formatDateString(dateToCompareWith);
|
const [_, fieldIdentifierToCompareWith, startOrEnd] =
|
||||||
if (dateStringToCompareWith > formattedDateString) {
|
minimumDateCheck.split(':');
|
||||||
let fieldToCompareWithTitle = fieldIdentifierToCompareWith;
|
if (!(fieldIdentifierToCompareWith in formData)) {
|
||||||
if (
|
|
||||||
fieldIdentifierToCompareWith in jsonSchema.properties &&
|
|
||||||
jsonSchema.properties[fieldIdentifierToCompareWith].title
|
|
||||||
) {
|
|
||||||
fieldToCompareWithTitle =
|
|
||||||
jsonSchema.properties[fieldIdentifierToCompareWith].title;
|
|
||||||
}
|
|
||||||
errors[propertyKey].addError(
|
|
||||||
`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(
|
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`
|
||||||
);
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
if (dateStringToCompareWith > formattedDateString) {
|
||||||
|
let fieldToCompareWithTitle = fieldIdentifierToCompareWith;
|
||||||
|
if (
|
||||||
|
fieldIdentifierToCompareWith in jsonSchema.properties &&
|
||||||
|
jsonSchema.properties[fieldIdentifierToCompareWith].title
|
||||||
|
) {
|
||||||
|
fieldToCompareWithTitle =
|
||||||
|
jsonSchema.properties[fieldIdentifierToCompareWith].title;
|
||||||
|
}
|
||||||
|
errors[propertyKey].addError(
|
||||||
|
`must be equal to or greater than '${fieldToCompareWithTitle}'`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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') {
|
||||||
|
|
Loading…
Reference in New Issue