-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.js
131 lines (125 loc) · 3.01 KB
/
validation.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { DEFAULT_NULL_INDEX } from "./constants";
import { getImage } from "./utils";
export const isResolutionValid = (
image,
resolutionType,
resolutionWidth = 0,
resolutionHeight = 1,
) => {
if (!resolutionWidth || !resolutionHeight || !image.width || !image.height) {
return true;
}
switch (resolutionType) {
case "absolute": {
if (image.width === resolutionWidth && image.height === resolutionHeight) {
return true;
}
break;
}
case "ratio": {
const ratio = resolutionWidth / resolutionHeight;
if (image.width / image.height === ratio) {
return true;
}
break;
}
case "less": {
if (image.width <= resolutionWidth && image.height <= resolutionHeight) {
return true;
}
break;
}
case "more": {
if (image.width >= resolutionWidth && image.height >= resolutionHeight) {
return true;
}
break;
}
default:
break;
}
return false;
};
export const isImageValid = (fileType) => {
if (fileType.includes("image")) {
return true;
}
return false;
};
export const isMaxFileSizeValid = (fileSize, maxFileSize) => {
return maxFileSize ? fileSize <= maxFileSize : true;
};
export const isAcceptTypeValid = (acceptType, fileName) => {
if (acceptType && acceptType.length > 0) {
const type = fileName.split(".").pop() || "";
if (acceptType.findIndex((item) => item.toLowerCase() === type.toLowerCase()) < 0) {
return false;
}
}
return true;
};
export const isMaxNumberValid = (totalNumber, maxNumber, keyUpdate) => {
if (maxNumber !== 0 && !maxNumber) {
return true;
}
if (keyUpdate === DEFAULT_NULL_INDEX) {
if (totalNumber <= maxNumber) {
return true;
}
} else if (totalNumber <= maxNumber + 1) {
return true;
}
return false;
};
export const getErrorValidation = async ({
fileList,
value,
maxNumber,
keyUpdate,
acceptType,
maxFileSize,
resolutionType,
resolutionWidth,
resolutionHeight,
}) => {
const newErrors = {};
if (!isMaxNumberValid(fileList.length + value.length, maxNumber, keyUpdate)) {
newErrors.maxNumber = true;
} else {
for (let i = 0; i < fileList.length; i += 1) {
const { file } = fileList[i];
if (!file) {
continue;
}
if (!isImageValid(file.type)) {
newErrors.acceptType = true;
break;
}
if (!isAcceptTypeValid(acceptType, file.name)) {
newErrors.acceptType = true;
break;
}
if (!isMaxFileSizeValid(file.size, maxFileSize)) {
newErrors.maxFileSize = true;
break;
}
if (resolutionType) {
const image = await getImage(file);
const checkRes = isResolutionValid(
image,
resolutionType,
resolutionWidth,
resolutionHeight,
);
if (!checkRes) {
newErrors.resolution = true;
break;
}
}
}
}
if (Object.values(newErrors).find(Boolean)) {
return newErrors;
}
return null;
};