mirror of
https://github.com/sartography/spiff-arena.git
synced 2025-01-27 09:45:12 +00:00
allow comparing dates in json schema forms and also allow checking nested fields w/ burnettk
This commit is contained in:
parent
68af0ad1dc
commit
8c8c9635d0
@ -167,6 +167,7 @@ export default function TaskShow() {
|
||||
if (disabled) {
|
||||
return;
|
||||
}
|
||||
return;
|
||||
|
||||
const dataToSubmit = formObject?.formData;
|
||||
if (!dataToSubmit) {
|
||||
@ -270,32 +271,74 @@ export default function TaskShow() {
|
||||
formData: any,
|
||||
errors: any
|
||||
) => {
|
||||
if ('properties' in jsonSchema) {
|
||||
Object.keys(jsonSchema.properties).forEach((propertyKey: string) => {
|
||||
const propertyMetadata = jsonSchema.properties[propertyKey];
|
||||
// if the jsonSchema has an items attribute then assume the element itself
|
||||
// doesn't have a custom validation but it's children could so use that
|
||||
const jsonSchemaToUse =
|
||||
'items' in jsonSchema ? jsonSchema.items : jsonSchema;
|
||||
|
||||
if ('properties' in jsonSchemaToUse) {
|
||||
Object.keys(jsonSchemaToUse.properties).forEach((propertyKey: string) => {
|
||||
const propertyMetadata = jsonSchemaToUse.properties[propertyKey];
|
||||
if (
|
||||
typeof propertyMetadata === 'object' &&
|
||||
'minimumDate' in propertyMetadata &&
|
||||
propertyMetadata.minimumDate === 'today'
|
||||
'minimumDate' in propertyMetadata
|
||||
) {
|
||||
const dateToday = new Date();
|
||||
const dateValue = formData[propertyKey];
|
||||
if (dateValue) {
|
||||
const dateValueObject = new Date(dateValue);
|
||||
const dateValueString = dateValueObject.toISOString().split('T')[0];
|
||||
const dateTodayString = dateToday.toISOString().split('T')[0];
|
||||
if (dateTodayString > dateValueString) {
|
||||
errors[propertyKey].addError('must be today or after');
|
||||
if (propertyMetadata.minimumDate === 'today') {
|
||||
const dateToday = new Date();
|
||||
const dateTodayString = dateToday.toISOString().split('T')[0];
|
||||
if (dateTodayString > dateValueString) {
|
||||
errors[propertyKey].addError('must be today or after');
|
||||
}
|
||||
} else if (propertyMetadata.minimumDate.startsWith('field:')) {
|
||||
const fieldIdentifierToCompareWith = propertyMetadata.minimumDate
|
||||
.split(':')
|
||||
.slice(1)
|
||||
.join(':');
|
||||
if (fieldIdentifierToCompareWith in formData) {
|
||||
const dateToCompareWith =
|
||||
formData[fieldIdentifierToCompareWith];
|
||||
const dateObjectToCompareWith = new Date(dateToCompareWith);
|
||||
const dateStringToCompareWith = dateObjectToCompareWith
|
||||
.toISOString()
|
||||
.split('T')[0];
|
||||
if (dateToCompareWith) {
|
||||
if (dateStringToCompareWith > dateValueString) {
|
||||
errors[propertyKey].addError(
|
||||
`this field must be equal to greater than '${fieldIdentifierToCompareWith}'`
|
||||
);
|
||||
}
|
||||
} else {
|
||||
errors[propertyKey].addError(
|
||||
`this field was supposed to be compared against '${fieldIdentifierToCompareWith}' but that field did not have a value`
|
||||
);
|
||||
}
|
||||
} else {
|
||||
errors[propertyKey].addError(
|
||||
`this field was supposed to be compared against '${fieldIdentifierToCompareWith}' but that field cannot be found`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// recurse through all nested properties as well
|
||||
getFieldsWithDateValidations(
|
||||
propertyMetadata,
|
||||
formData[propertyKey],
|
||||
errors[propertyKey]
|
||||
);
|
||||
let formDataToSend = formData[propertyKey];
|
||||
if (formDataToSend) {
|
||||
if (formDataToSend.constructor.name !== 'Array') {
|
||||
formDataToSend = [formDataToSend];
|
||||
}
|
||||
formDataToSend.forEach((item: any, index: number) => {
|
||||
let errorsToSend = errors[propertyKey];
|
||||
if (index in errorsToSend) {
|
||||
errorsToSend = errorsToSend[index];
|
||||
}
|
||||
getFieldsWithDateValidations(propertyMetadata, item, errorsToSend);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
return errors;
|
||||
|
Loading…
x
Reference in New Issue
Block a user