165 lines
4.5 KiB
TypeScript
165 lines
4.5 KiB
TypeScript
/**
|
|
* 📄 Selection Validation Utility
|
|
* 선택 항목 검증 유틸리티
|
|
*
|
|
* - 선택 항목 검증 함수 (단일 및 다중 선택 지원)
|
|
* - 검증 결과 타입 정의 및 에러 메시지 생성
|
|
* - 자주 사용되는 선택 항목 상수 제공
|
|
* - 선택 항목 검증 전 로직 구현
|
|
* - 사용자 친화적인 에러 메시지 출력
|
|
* - 다양한 선택 타입 지원 (환경청, 시도, 시군구 등)
|
|
*/
|
|
export interface ApiSelectOption {
|
|
text: string;
|
|
val: string;
|
|
}
|
|
|
|
// 선택 항목 검증 설정 타입
|
|
export interface SelectionValidationConfig {
|
|
value: string | number | ApiSelectOption;
|
|
description: string;
|
|
required: boolean;
|
|
}
|
|
|
|
// 선택 항목 검증 결과 타입
|
|
export interface SelectionValidationResult {
|
|
isValid: boolean;
|
|
errorMessage?: string;
|
|
}
|
|
|
|
export type SelectionValidationNotifier = (message: string) => void;
|
|
|
|
/**
|
|
* 단일 선택 항목이 유효한지 검증하는 함수
|
|
* @param value 검증할 값
|
|
* @param isRequired 필수 선택 여부
|
|
* @returns 유효한지 여부
|
|
*/
|
|
export const isValidSelection = (
|
|
value: string | number | ApiSelectOption,
|
|
isRequired: boolean = true,
|
|
): boolean => {
|
|
if (!isRequired) return true;
|
|
|
|
if (typeof value === "object" && value !== null) {
|
|
// ApiSelectOption 객체인 경우
|
|
return (
|
|
value.val !== "0" &&
|
|
value.val !== "" &&
|
|
value.val !== null &&
|
|
value.val !== undefined
|
|
);
|
|
}
|
|
|
|
// 문자열 또는 숫자인 경우
|
|
return (
|
|
value !== "0" &&
|
|
value !== 0 &&
|
|
value !== "" &&
|
|
value !== null &&
|
|
value !== undefined
|
|
);
|
|
};
|
|
|
|
/**
|
|
* 여러 선택 항목을 한 번에 검증하는 함수
|
|
* @param validationConfigs 검증할 선택 항목들의 설정 배열
|
|
* @returns 검증 결과. 실패 시 첫 번째 에러 메시지 반환
|
|
*/
|
|
export const validateMultipleSelections = (
|
|
validationConfigs: SelectionValidationConfig[],
|
|
): SelectionValidationResult => {
|
|
for (const config of validationConfigs) {
|
|
if (!isValidSelection(config.value, config.required)) {
|
|
return {
|
|
isValid: false,
|
|
errorMessage: `${config.description}을(를) 선택해주세요.`,
|
|
};
|
|
}
|
|
}
|
|
|
|
return { isValid: true };
|
|
};
|
|
|
|
/**
|
|
* 여러 선택 항목을 한 번에 검증하고 주입된 CS 알림을 표시하는 함수
|
|
* @param validationConfigs 검증할 선택 항목들의 설정 배열
|
|
* @param notify 호출자가 주입하는 CS 알림 함수
|
|
* @returns 검증 결과
|
|
*/
|
|
export const validateMultipleSelectionsWithAlert = (
|
|
validationConfigs: SelectionValidationConfig[],
|
|
notify: SelectionValidationNotifier,
|
|
): boolean => {
|
|
const result = validateMultipleSelections(validationConfigs);
|
|
if (!result.isValid && result.errorMessage) {
|
|
notify(result.errorMessage);
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* 기본 선택 항목들 검증 (환경청, 시도, 시군구, 하수처리장)
|
|
* @param select1 환경청코드
|
|
* @param select2 시도코드
|
|
* @param select3 시군구 객체
|
|
* @param select4 하수처리장코드
|
|
* @param options 각 항목의 필수 여부 설정
|
|
* @returns 검증 결과
|
|
*/
|
|
export const validateBasicSelections = (
|
|
select1: string | number,
|
|
options: {
|
|
requireEnv?: boolean;
|
|
requireSido?: boolean;
|
|
requireSigungu?: boolean;
|
|
requireTreatmentPlant?: boolean;
|
|
treatmentPlantLabel?: string;
|
|
} = {},
|
|
): SelectionValidationResult => {
|
|
const {
|
|
requireEnv = true,
|
|
requireSido = true,
|
|
requireSigungu = true,
|
|
requireTreatmentPlant = true,
|
|
treatmentPlantLabel = "하수처리장",
|
|
} = options;
|
|
|
|
const validationConfigs: SelectionValidationConfig[] = [
|
|
{
|
|
value: select1,
|
|
description: treatmentPlantLabel,
|
|
required: requireTreatmentPlant,
|
|
},
|
|
];
|
|
|
|
return validateMultipleSelections(validationConfigs);
|
|
};
|
|
|
|
/**
|
|
* 기본 선택 항목들 검증 및 주입된 CS 알림 표시
|
|
* @param select1 하수처리장코드
|
|
* @param notify 호출자가 주입하는 CS 알림 함수
|
|
* @param options 각 항목의 필수 여부 설정
|
|
* @returns 검증 결과
|
|
*/
|
|
export const validateBasicSelectionsWithAlert = (
|
|
select1: string | number,
|
|
notify: SelectionValidationNotifier,
|
|
options: {
|
|
requireEnv?: boolean;
|
|
requireSido?: boolean;
|
|
requireSigungu?: boolean;
|
|
requireTreatmentPlant?: boolean;
|
|
treatmentPlantLabel?: string;
|
|
} = {},
|
|
): boolean => {
|
|
const result = validateBasicSelections(select1, options);
|
|
if (!result.isValid && result.errorMessage) {
|
|
notify(result.errorMessage);
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|