Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FSADT1-1053: Improve validation when values are empty #675

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion frontend/src/helpers/validators/BCeIDFormValidations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
isGreaterThan,
} from "@/helpers/validators/GlobalValidators";


// Step 1: Business Information
formFieldValidations["businessInformation.businessName"] = [
isNotEmpty("Business Name cannot be empty"),
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/helpers/validators/GlobalValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export const isUniqueDescriptive = (): {
if (
values.some(
(entry: string) => entry.toLowerCase() === value.toLowerCase()
)
) && value.trim() !== ""
) {
return message;
}
Expand Down
56 changes: 32 additions & 24 deletions frontend/src/helpers/validators/SubmissionValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,44 @@
*
* @see {@link SubmissionValidators.spec.ts}
*/
import { useEventBus } from '@vueuse/core'
import type { ValidationMessageType } from '@/dto/CommonTypesDto'
import { useEventBus } from "@vueuse/core";
import type { ValidationMessageType } from "@/dto/CommonTypesDto";

/**
* Event bus for submission error notifications.
*/
const errorBus = useEventBus<ValidationMessageType[]>('submission-error-notification')
const errorBus = useEventBus<ValidationMessageType[]>(
"submission-error-notification"
);

/**
* Event bus for revalidating the submission.
*/
const revalidateBus = useEventBus<void>('revalidate-bus')
const revalidateBus = useEventBus<void>("revalidate-bus");

/**
* Event bus for error notifications bar.
*/
const notificationBus = useEventBus<ValidationMessageType|undefined>("error-notification");
const notificationBus = useEventBus<ValidationMessageType | undefined>(
"error-notification"
);

/**
* Array of submission validators.
*/
let submissionValidators : ValidationMessageType[] = []
let submissionValidators: ValidationMessageType[] = [];

/**
* Register a listener for submission errors on the error bus.
* When an error is received, update the submission validators array.
*/
errorBus.on((errors) => {
submissionValidators = errors.map((error: ValidationMessageType) => {
notificationBus.emit(error)
return { ...error, originalValue: '' }
})
revalidateBus.emit()
})
notificationBus.emit(error);
return { ...error, originalValue: "" };
});
revalidateBus.emit();
});

/**
* Update the submission validators array with the provided fieldId and value.
Expand All @@ -60,13 +64,15 @@ errorBus.on((errors) => {
* @param value - The new value for the validator's originalValue property.
*/
const updateValidators = (fieldId: string, value: string): void => {
submissionValidators = submissionValidators.map((validator : ValidationMessageType) => {
if (validator.fieldId === fieldId) {
return { ...validator, originalValue: value }
submissionValidators = submissionValidators.map(
(validator: ValidationMessageType) => {
if (validator.fieldId === fieldId) {
return { ...validator, originalValue: value };
}
return validator;
}
return validator
})
}
);
};

/**
* Create a submission validation function for the specified fieldName.
Expand All @@ -78,14 +84,16 @@ export const submissionValidation = (
fieldName: string
): ((value: string) => string) => {
return (value: string) => {
const foundError = submissionValidators.find((validator: ValidationMessageType) => validator.fieldId === fieldName)
const foundError = submissionValidators.find(
(validator: ValidationMessageType) => validator.fieldId === fieldName
);
if (
foundError &&
(foundError.originalValue === value || foundError.originalValue === '')
(foundError.originalValue === value || foundError.originalValue === "")
) {
updateValidators(fieldName, value)
return foundError.errorMsg
updateValidators(fieldName, value);
return foundError.errorMsg;
}
return ''
}
}
return "";
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useFetchTo } from "@/composables/useFetch";
import {
BusinessSearchResult,
ClientTypeEnum,
ProgressNotification
ProgressNotification,
} from "@/dto/CommonTypesDto";
import { BusinessTypeEnum } from "@/dto/CommonTypesDto";
import type {
Expand Down
Loading