Skip to content

Commit

Permalink
refactor: make enum validation helper
Browse files Browse the repository at this point in the history
  • Loading branch information
IanFonzie committed Aug 2, 2024
1 parent e713d1b commit da1f815
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/shared/lib/validation/question-evaluation/sprint-with-us.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@ import {
} from "shared/lib/resources/question-evaluation/sprint-with-us";
import { invalid, valid, Validation } from "shared/lib/validation";

export function validateSWUTeamQuestionResponseEvaluationType(
raw: string,
isOneOf: SWUTeamQuestionResponseEvaluationType[]
): Validation<SWUTeamQuestionResponseEvaluationType> {
const parsed = parseSWUTeamQuestionResponseEvaluationType(raw);
if (!parsed) {
return invalid([
`"${raw}" is not a valid SprintWithUs team question response evaluation type.`
]);
}
if (!isOneOf.includes(parsed)) {
return invalid([`"${raw}" is not one of: ${isOneOf.join(", ")}`]);
}
return valid(parsed);
function makeValidateSWUTeamQuestionResponseEvaluationEnum<T>(
parse: (raw: string) => T | null,
parseErrorMsg: (raw: string) => string,
notOneOfErrorMsg: (raw: string, isOneOf: T[]) => string
): (raw: string, isOneOf: T[]) => Validation<T> {
return (raw, isOneOf) => {
const parsed = parse(raw);
if (!parsed) {
return invalid([parseErrorMsg(raw)]);
}

if (!isOneOf.includes(parsed)) {
return invalid([notOneOfErrorMsg(raw, isOneOf)]);
}

return valid(parsed);
};
}

export const validateSWUTeamQuestionResponseEvaluationType =
makeValidateSWUTeamQuestionResponseEvaluationEnum<SWUTeamQuestionResponseEvaluationType>(
parseSWUTeamQuestionResponseEvaluationType,
(raw) =>
`"${raw}" is not a valid SprintWithUs team question response evaluation type.`,
(raw, isOneOf) => `"${raw}" is not one of: ${isOneOf.join(", ")}`
);

0 comments on commit da1f815

Please sign in to comment.