템플릿관리, 컴포넌트 관리
This commit is contained in:
101
backend-node/src/utils/validation.ts
Normal file
101
backend-node/src/utils/validation.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* 유효성 검증 유틸리티
|
||||
*/
|
||||
|
||||
/**
|
||||
* 필수 값 검증
|
||||
*/
|
||||
export const validateRequired = (value: any, fieldName: string): void => {
|
||||
if (value === null || value === undefined || value === "") {
|
||||
throw new Error(`${fieldName}은(는) 필수 입력값입니다.`);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 여러 필수 값 검증
|
||||
*/
|
||||
export const validateRequiredFields = (
|
||||
data: Record<string, any>,
|
||||
requiredFields: string[]
|
||||
): void => {
|
||||
for (const field of requiredFields) {
|
||||
validateRequired(data[field], field);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 문자열 길이 검증
|
||||
*/
|
||||
export const validateStringLength = (
|
||||
value: string,
|
||||
fieldName: string,
|
||||
minLength?: number,
|
||||
maxLength?: number
|
||||
): void => {
|
||||
if (minLength !== undefined && value.length < minLength) {
|
||||
throw new Error(
|
||||
`${fieldName}은(는) 최소 ${minLength}자 이상이어야 합니다.`
|
||||
);
|
||||
}
|
||||
|
||||
if (maxLength !== undefined && value.length > maxLength) {
|
||||
throw new Error(`${fieldName}은(는) 최대 ${maxLength}자 이하여야 합니다.`);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 이메일 형식 검증
|
||||
*/
|
||||
export const validateEmail = (email: string): boolean => {
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
return emailRegex.test(email);
|
||||
};
|
||||
|
||||
/**
|
||||
* 숫자 범위 검증
|
||||
*/
|
||||
export const validateNumberRange = (
|
||||
value: number,
|
||||
fieldName: string,
|
||||
min?: number,
|
||||
max?: number
|
||||
): void => {
|
||||
if (min !== undefined && value < min) {
|
||||
throw new Error(`${fieldName}은(는) ${min} 이상이어야 합니다.`);
|
||||
}
|
||||
|
||||
if (max !== undefined && value > max) {
|
||||
throw new Error(`${fieldName}은(는) ${max} 이하여야 합니다.`);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 배열이 비어있지 않은지 검증
|
||||
*/
|
||||
export const validateNonEmptyArray = (
|
||||
array: any[],
|
||||
fieldName: string
|
||||
): void => {
|
||||
if (!Array.isArray(array) || array.length === 0) {
|
||||
throw new Error(`${fieldName}은(는) 비어있을 수 없습니다.`);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 필수 필드 검증 후 누락된 필드 목록 반환
|
||||
*/
|
||||
export const checkMissingFields = (
|
||||
data: Record<string, any>,
|
||||
requiredFields: string[]
|
||||
): string[] => {
|
||||
const missingFields: string[] = [];
|
||||
|
||||
for (const field of requiredFields) {
|
||||
const value = data[field];
|
||||
if (value === null || value === undefined || value === "") {
|
||||
missingFields.push(field);
|
||||
}
|
||||
}
|
||||
|
||||
return missingFields;
|
||||
};
|
||||
Reference in New Issue
Block a user