-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathValidator.js
More file actions
53 lines (46 loc) · 1.71 KB
/
Validator.js
File metadata and controls
53 lines (46 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { MIN_CAR_NUM, MAX_CAR_NAME_LENGTH } from "../constants/gameNumbers.js";
import { ERROR_MESSAGES } from "../constants/message.js";
class Validator {
// 자동차 이름 전체 검증
static validateCarNames(carNames) {
this.checkSeparated(carNames);
this.checkMinimumCars(carNames);
this.checkEmptyNames(carNames);
this.checkNameLength(carNames);
}
// 입력이 배열로 잘 분리되었는지, 비어있지는 않은지 검증
static checkSeparated(carNames) {
if (!Array.isArray(carNames) || carNames.length === 0) {
throw new Error(`${ERROR_MESSAGES.PREFIX} ${ERROR_MESSAGES.INPUT}`);
}
}
// 최소 2대 이상인지 검증
static checkMinimumCars(carNames) {
if (carNames.length < MIN_CAR_NUM) {
throw new Error(`${ERROR_MESSAGES.PREFIX} ${ERROR_MESSAGES.MIN_CARS}`);
}
}
// 빈 문자열 이름이 있는지 검증
static checkEmptyNames(carNames) {
carNames.forEach(name => {
if (!name.trim()) {
throw new Error(`${ERROR_MESSAGES.PREFIX} ${ERROR_MESSAGES.INPUT}`);
}
})
}
// 이름 길이가 5자 이하인지 검증
static checkNameLength(carNames) {
carNames.forEach(name => {
if (name.length > MAX_CAR_NAME_LENGTH) {
throw new Error(`${ERROR_MESSAGES.PREFIX} ${ERROR_MESSAGES.NAME_LENGTH}`);
}
});
}
// 시도 횟수가 숫자인지, 한 번 이상인지 확인
static validateTryCount(count) {
if(isNaN(count) || count < 1) {
throw new Error(`${ERROR_MESSAGES.PREFIX} ${ERROR_MESSAGES.TRY_COUNT}`);
}
}
}
export default Validator;