Files
frontend/apps/sn/app/(with-base-layout)/fileValidation.tsx
T

56 lines
1.8 KiB
TypeScript

/**
* 📄 File Validation Utility
* 파일 확장자 검증 유틸리티
*
* - 파일 확장자 검증 함수 (단일 및 다중 파일 지원)
* - 검증 결과 타입 정의 및 에러 메시지 생성
* - 자주 사용되는 파일 확장자 상수 제공
* - 파일 업로드 전 검증 로직 구현
* - 사용자 친화적인 에러 메시지 출력
* - 다양한 파일 타입(INP, EXCEL, OFFICE 등) 지원
*/
// 파일 확장자 검증 함수
export const validateFileExtension = (file: File, allowedExtensions: string[]): boolean => {
const fileName = file.name.toLowerCase();
return allowedExtensions.some(ext => fileName.endsWith(ext.toLowerCase()));
};
// 파일 확장자 검증 결과 타입
export interface FileValidationResult {
isValid: boolean;
errorMessage?: string;
}
// 파일 검증 설정 타입
export interface FileValidationConfig {
file: File;
allowedExtensions: string[];
fileDescription: string;
}
/**
* 여러 파일의 확장자를 한 번에 검증하는 함수
* @param validationConfigs 검증할 파일들의 설정 배열
* @returns 검증 결과. 실패 시 첫 번째 에러 메시지 반환
*/
export const validateMultipleFiles = (validationConfigs: FileValidationConfig[]): FileValidationResult => {
for (const config of validationConfigs) {
if (!validateFileExtension(config.file, config.allowedExtensions)) {
const allowedExtStr = config.allowedExtensions.join(', ');
return {
isValid: false,
errorMessage: `${config.fileDescription} 파일은 ${allowedExtStr} 확장자만 업로드 가능합니다.`
};
}
}
return { isValid: true };
};
// 자주 사용되는 확장자 상수
export const FILE_EXTENSIONS = {
INP: ['.inp'],
EXCEL: ['.xls', '.xlsx', '.xlsm', '.xltx'],
ALL_OFFICE: ['.xls', '.xlsx', '.xlsm', '.xltx', '.doc', '.docx', '.ppt', '.pptx']
};