export const POINT_MAX = 1_000_000; export const DESCRIPTION_MAX_LENGTH = 200; export interface PointStandardValues { id: string; point: number; limitPoint: number; isDuplicable: boolean; description: string; isActive: boolean; } export type PointStandardFormErrors = Partial< Record<'id' | 'point' | 'limitPoint' | 'description', string> >; export type PointStandardFormState = | { status: 'idle' } | { status: 'error'; message?: string; errors?: PointStandardFormErrors } | { status: 'success' }; export const INITIAL_POINT_STANDARD_FORM_STATE: PointStandardFormState = { status: 'idle', }; type ValidationResult = | { ok: true; values: PointStandardValues } | { ok: false; errors: PointStandardFormErrors }; function validatePointNumber( value: number, label: string ): { error?: string } { if (!Number.isInteger(value)) { return { error: `${label}는 숫자만 입력할 수 있습니다.` }; } if (value < 0) { return { error: `${label}는 0 이상으로 입력해 주세요.` }; } if (value > POINT_MAX) { return { error: `${label}는 ${POINT_MAX.toLocaleString('ko-KR')} 이하로 입력해 주세요.`, }; } return {}; } export function validatePointStandard( values: PointStandardValues ): ValidationResult { const errors: PointStandardFormErrors = {}; if (!values.id.trim()) { errors.id = '수정할 대상을 찾을 수 없습니다.'; } const point = validatePointNumber(values.point, '포인트'); if (point.error) { errors.point = point.error; } const limitPoint = validatePointNumber(values.limitPoint, '제한포인트'); if (limitPoint.error) { errors.limitPoint = limitPoint.error; } const description = values.description.trim(); if (!description) { errors.description = '이벤트 설명을 입력해 주세요.'; } else if (description.length > DESCRIPTION_MAX_LENGTH) { errors.description = `이벤트 설명은 ${DESCRIPTION_MAX_LENGTH}자 이내로 입력해 주세요.`; } if (Object.keys(errors).length > 0) { return { ok: false, errors }; } return { ok: true, values: { id: values.id.trim(), point: values.point, limitPoint: values.limitPoint, isDuplicable: values.isDuplicable, description, isActive: values.isActive, }, }; }