소개


체이닝 방식의 폼 유효성 검사 시스템을 사용합니다. 공용 FormValidator 클래스를 기반으로 각 기능별 전용 Validator를 구성하여 재사용 가능하고 확장 가능한 구조를 제공합니다.

설계 내용


1. 공용 FormValidator 클래스

위치: src/shared/lib/FormValidator.ts

제네릭을 활용한 범용 유효성 검사 클래스로, 모든 폼 검증의 기반이 됩니다.

주요 메서드

동작 원리

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,
    };
  }
}

2. 기능별 전용 Validator

예시: CreatePollFormValidator (투표 생성 폼)