Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
mamartinezmejia authored and paulushcgcj committed Oct 10, 2023
1 parent e48a59a commit 123f4fe
Showing 1 changed file with 68 additions and 68 deletions.
136 changes: 68 additions & 68 deletions frontend/src/helpers/validators/GlobalValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,74 @@ export const formFieldValidations: Record<
export const getValidations = (key: string): ((value: string) => string)[] =>
formFieldValidations[key] || [];

// 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) === "";
});
});
};

// This function will run the validators and return the errors
export const runValidation = (
key: string,
Expand Down Expand Up @@ -374,71 +442,3 @@ 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) === "";
});
});
};

0 comments on commit 123f4fe

Please sign in to comment.