체이닝 방식의 폼 유효성 검사 시스템을 사용합니다. 공용 FormValidator 클래스를 기반으로 각 기능별 전용 Validator를 구성하여 재사용 가능하고 확장 가능한 구조를 제공합니다.
위치: src/shared/lib/FormValidator.ts
제네릭을 활용한 범용 유효성 검사 클래스로, 모든 폼 검증의 기반이 됩니다.
주요 메서드
addRule(validate, errorMessage): 검증 규칙 추가 (체이닝 가능)validate(value): 모든 규칙 실행 후 { isValid, errors } 반환isValid(value): 유효성 여부만 boolean으로 반환동작 원리
class FormValidator<T> {
private rules: ValidationRule<T>[] = [];
addRule(validate: (value: T) => boolean, errorMessage: string): this {
this.rules.push({ validate, errorMessage });
return this; // 체이닝을 위해 this 반환
}
validate(value: T): { isValid: boolean; errors: string[] } {
const errors: string[] = [];
// 모든 규칙을 순회하며 검증
for (const rule of this.rules) {
if (!rule.validate(value)) {
errors.push(rule.errorMessage);
}
}
return {
isValid: errors.length === 0,
errors,
};
}
}
예시: CreatePollFormValidator (투표 생성 폼)