Skip to content

Commit

Permalink
Refactored code - Part #2
Browse files Browse the repository at this point in the history
  • Loading branch information
mamartinezmejia committed Sep 25, 2023
1 parent 7e4a196 commit 26098fb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 79 deletions.
78 changes: 0 additions & 78 deletions frontend/src/helpers/validators/BCeIDFormValidations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,8 @@ import {
isNoSpecialCharacters,
isNot,
formFieldValidations,
getFieldValue,
getValidations
} from "@/helpers/validators/GlobalValidators";

import type { FormDataDto } from "@/dto/ApplyClientNumberDto";
import type { ValidationMessageType } from "@/dto/CommonTypesDto";

const errorBus = useEventBus<ValidationMessageType[]>(
"submission-error-notification"
);

// Step 1: Business Information
formFieldValidations["businessInformation.businessName"] = [
Expand Down Expand Up @@ -111,76 +103,6 @@ formFieldValidations["location.contacts.*.phoneNumber"] = [
isMinSize("Please provide a valid phone number")(10),
];

// This function will run the validators and return the errors
export const validate = (
keys: string[],
target: FormDataDto,
notify: boolean = false
): boolean => {
// For every received key we get the validations and run them
return keys.every((key) => {
// First we get all validators for that field
const validations: ((value: string) => string)[] = getValidations(key);
// We split the field key and the condition if it has one
const [fieldKey, fieldCondition] = key.includes("(")
? key.replace(")", "").split("(")
: [key, "true"];
// For every validator we run it and check if the result is empty
return validations.every((validation: (value: string) => string) => {
// We then load the field value
const fieldValue = getFieldValue(fieldKey, target);
// We define a function that will run the validation if the condition is true
const buildEval = (condition: string) =>
condition === "true" ? "true" : `target.${condition}`;
const runValidation = (
item: any,
condition: string,
fieldId: string = fieldKey
): string => {
// eslint-disable-next-line no-eval
if (eval(condition)) {
const validationResponse = validation(item);
if (notify && validationResponse) {
errorBus.emit([{ fieldId, errorMsg: validationResponse }]);
}
return validationResponse;
} else {
return "";
}
};
// If the field value is an array we run the validation for every item in the array
if (Array.isArray(fieldValue)) {
return fieldValue.every((item: any, index: number) => {
// And sometimes we can end up with another array inside, that's life
if (Array.isArray(item)) {
if (item.length === 0) item.push("");
return item.every(
(subItem: any) =>
runValidation(
subItem,
buildEval(fieldCondition.replace(".*.", `[${index}].`)),
fieldKey.replace(".*.", `[${index}].`)
) === ""
);
}
// If it is not an array here, just validate it
return (
runValidation(
item,
buildEval(fieldCondition.replace(".*.", `[${index}].`)),
fieldKey.replace(".*.", `[${index}].`)
) === ""
);
});
}
// If the field value is not an array we run the validation for the field
return runValidation(fieldValue, fieldCondition) === "";
});
});
};



export const addValidation = (
key: string,
validation: (value: string) => string
Expand Down
73 changes: 73 additions & 0 deletions frontend/src/helpers/validators/GlobalValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const notificationBus = useEventBus<ValidationMessageType | undefined>(
"error-notification"
);

const errorBus = useEventBus<ValidationMessageType[]>(
"submission-error-notification"
);

/**
* Checks if the value is not empty
* @param value - The value to check
Expand Down Expand Up @@ -381,3 +385,72 @@ export const runValidation = (
// If the field value is not an array we run the validation for the field
return executeValidation(fieldValue, fieldCondition) === "";
};


// This function will run the validators and return the errors
export const validate = (
keys: string[],
target: any,
notify: boolean = false
): boolean => {
// For every received key we get the validations and run them
return keys.every((key) => {
// First we get all validators for that field
const validations: ((value: string) => string)[] = getValidations(key);
// We split the field key and the condition if it has one
const [fieldKey, fieldCondition] = key.includes("(")
? key.replace(")", "").split("(")
: [key, "true"];
// For every validator we run it and check if the result is empty
return validations.every((validation: (value: string) => string) => {
// We then load the field value
const fieldValue = getFieldValue(fieldKey, target);
// We define a function that will run the validation if the condition is true
const buildEval = (condition: string) =>
condition === "true" ? "true" : `target.${condition}`;
const runValidation = (
item: any,
condition: string,
fieldId: string = fieldKey
): string => {
// eslint-disable-next-line no-eval
if (eval(condition)) {
const validationResponse = validation(item);
if (notify && validationResponse) {
errorBus.emit([{ fieldId, errorMsg: validationResponse }]);
}
return validationResponse;
} else {
return "";
}
};
// If the field value is an array we run the validation for every item in the array
if (Array.isArray(fieldValue)) {
return fieldValue.every((item: any, index: number) => {
// And sometimes we can end up with another array inside, that's life
if (Array.isArray(item)) {
if (item.length === 0) item.push("");
return item.every(
(subItem: any) =>
runValidation(
subItem,
buildEval(fieldCondition.replace(".*.", `[${index}].`)),
fieldKey.replace(".*.", `[${index}].`)
) === ""
);
}
// If it is not an array here, just validate it
return (
runValidation(
item,
buildEval(fieldCondition.replace(".*.", `[${index}].`)),
fieldKey.replace(".*.", `[${index}].`)
) === ""
);
});
}
// If the field value is not an array we run the validation for the field
return runValidation(fieldValue, fieldCondition) === "";
});
});
};
2 changes: 1 addition & 1 deletion frontend/src/pages/FormBCeIDPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import ForestClientUserSession from "@/helpers/ForestClientUserSession";
// Imported global validations
import {
addValidation,
validate,
} from "@/helpers/validators/BCeIDFormValidations";
import {
runValidation,
validate,
} from "@/helpers/validators/GlobalValidators";
// @ts-ignore
Expand Down

0 comments on commit 26098fb

Please sign in to comment.