/
AppFormValidators.js
159 lines (124 loc) · 5.05 KB
/
AppFormValidators.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import Util from "../../helpers/Util";
import HealthCheckProtocols from "../../constants/HealthCheckProtocols";
import HealthCheckPortTypes from "../../constants/HealthCheckPortTypes";
import ValidConstraints from "../../constants/ValidConstraints";
function isValidPort(value) {
if (value == null || Util.isStringAndEmpty(value)) {
return true;
}
if (!Util.isStringAndEmpty(value) && !/^[0-9]+$/.test(value.toString())) {
return false;
}
var port = parseInt(value, 10);
return (port >= 0 && port <= 65535);
}
function isValidPath(value) {
if (value == null || Util.isStringAndEmpty(value)) {
return true;
}
return value.match(/ /g) == null;
}
const AppFormValidators = {
appIdNotEmpty: (str) => !Util.isStringAndEmpty(str),
appIdNoWhitespaces: (str) => str.match(/ /g) == null,
appIdValidChars: (str) => str.match(/[^a-z0-9\-\.\/]/g) == null,
appIdWellFormedPath: (str) => {
// This RegExp is taken from the ID field explanation described here:
// https://mesosphere.github.io/marathon/docs/rest-api.html#post-v2-apps
var idMatchRegExp = "^(([a-z0-9]|[a-z0-9][a-z0-9\-]*" +
"[a-z0-9])\.)*([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])$";
return str.split("/").every((pathSegement) => {
return Util.isStringAndEmpty(pathSegement) ||
pathSegement === "." ||
pathSegement === ".." ||
!!pathSegement.match(idMatchRegExp);
});
},
containerVolumesContainerPathIsValid: (obj) => isValidPath(obj.containerPath),
containerVolumesHostPathIsValid: (obj) => isValidPath(obj.hostPath),
containerVolumesModeNotEmpty: (obj) => !Util.isStringAndEmpty(obj.mode),
constraints: (constraints) => Util.isStringAndEmpty(constraints) ||
constraints
.split(",")
.map((constraint) => constraint.split(":").map((value) => value.trim()))
.every((p) => {
if (p.length < 2 || p.length > 3) {
return false;
}
return ValidConstraints.indexOf(p[1].toLowerCase()) !== -1;
}),
cpus: (value) => !Util.isStringAndEmpty(value) &&
!!value.toString().match(/^[0-9\.]+$/) && parseFloat(value) >= 0.01,
disk: (value) => !Util.isStringAndEmpty(value) &&
!!value.toString().match(/^[0-9\.]+$/),
dockerImageNoWhitespaces: (str) => str.match(/ /g) == null,
dockerParameters: (obj) => obj.hasOwnProperty("key") &&
obj.hasOwnProperty("value") &&
!(Util.isStringAndEmpty(obj.key) &&
!Util.isStringAndEmpty(obj.value)),
dockerPortMappingsContainerPortIsValid: (obj) =>
isValidPort(obj.containerPort),
dockerPortMappingsHostPortIsValid: (obj) => isValidPort(obj.hostPort),
dockerPortMappingsServicePortIsValid: (obj) => isValidPort(obj.servicePort),
dockerPortMappingsProtocolValidType: (obj) =>
obj.protocol == null ||
Util.isStringAndEmpty(obj.protocol) ||
(obj.protocol != null &&
(obj.protocol === "tcp" || obj.protocol === "udp")),
env: (obj) => obj.hasOwnProperty("key") &&
obj.hasOwnProperty("value") &&
!(Util.isStringAndEmpty(obj.key) &&
!Util.isStringAndEmpty(obj.value)),
executor: (str) => Util.isString(str) &&
(new RegExp("^(|\\/\\/cmd|\\/?[^\\/]+(\\/[^\\/]+)*)$"))
.test(str),
healthChecksProtocol: (obj) => {
return !!Object.values(HealthCheckProtocols).find((protocol) => {
return protocol === obj.protocol;
});
},
healthChecksCommandNotEmpty: (obj) => {
return obj.protocol !== HealthCheckProtocols.COMMAND ||
!Util.isStringAndEmpty(obj.command);
},
healthChecksPathNotEmpty: (obj) => {
return obj.protocol !== HealthCheckProtocols.HTTP ||
!Util.isStringAndEmpty(obj.path);
},
healthChecksPortIndex: (obj) => {
return obj.protocol !== HealthCheckProtocols.HTTP &&
obj.protocol !== HealthCheckProtocols.TCP ||
obj.portType !== HealthCheckPortTypes.PORT_INDEX ||
!!obj.portIndex.toString().match(/^[0-9]+$/);
},
healthChecksPort: (obj) => {
return obj.protocol !== HealthCheckProtocols.HTTP &&
obj.protocol !== HealthCheckProtocols.TCP ||
obj.portType !== HealthCheckPortTypes.PORT_NUMBER ||
isValidPort(obj.port);
},
healthChecksGracePeriod: (obj) => {
return !!obj.gracePeriodSeconds.toString().match(/^[0-9]+$/);
},
healthChecksInterval: (obj) => {
return !!obj.intervalSeconds.toString().match(/^[0-9]+$/);
},
healthChecksTimeout: (obj) => {
return !!obj.timeoutSeconds.toString().match(/^[0-9]+$/);
},
healthChecksMaxConsecutiveFailures: (obj) => {
return !!obj.maxConsecutiveFailures.toString().match(/^[0-9]+$/);
},
instances: (value) => !Util.isStringAndEmpty(value) &&
!!value.toString().match(/^[0-9]+$/),
labels: (obj) => obj.hasOwnProperty("key") &&
obj.hasOwnProperty("value") &&
!(Util.isStringAndEmpty(obj.key) &&
!Util.isStringAndEmpty(obj.value)),
mem: (value) => !Util.isStringAndEmpty(value) &&
!!value.toString().match(/^[0-9\.]+$/) && parseInt(value) >= 32,
ports: (ports) => Util.isStringAndEmpty(ports) ||
ports.split(",")
.every((port) => port.toString().trim().match(/^[0-9]+$/))
};
export default Object.freeze(AppFormValidators);