Hi all,
I would love some constructive feedback / criticism on my validation function. It basically loops over the entries of an Object entity and checks if a given key matches against a pre-set series of validators using a switch
statement. There’s no need to understand the validator functions, just know that they exist elsewhere in my code
Is this a clunky approach? Is it a good idea? It could become a “BIG” function, but it’s really just a series of validation checks against any known keys in an entity Object.
Is there a better approach? Or any other feedback would be MUCH APPRECIATED Thank you
/**
* Validate a single entity Object
* @param entity
*/
export function validateEntity(entity: { [key: string]: any }): string[] {
const invalidEntries: string[] = [];
Object.entries(entity).forEach(([key, val]: [string, string]) => {
switch (key) {
case "GuidePdfFormID":
case "GuideID":
if (uuidValidate(val) === false) {
invalidEntries.push(key);
}
break;
case "ReferenceID":
case "ServiceResourceReferenceID":
case "AnswerID":
case "QuestionID":
if (isInt(val) === false) {
invalidEntries.push(key);
}
break;
case "Email":
if (isEmail(val) === false) {
invalidEntries.push(key);
}
break;
case "Category":
if (validateCategory(val) === false) {
invalidEntries.push(key);
}
break;
case "BusinessType":
if (validateBusinessType(val) === false) {
invalidEntries.push(key);
}
break;
case "ClientNumber":
if (isAlphanumeric(val) === false) {
invalidEntries.push(key);
}
break;
default:
if (!val) {
invalidEntries.push(key);
}
}
return invalidEntries;
});
return invalidEntries;
}