This repository was archived by the owner on Aug 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsjcpp_validators.cpp
More file actions
390 lines (324 loc) · 11.4 KB
/
Copy pathwsjcpp_validators.cpp
File metadata and controls
390 lines (324 loc) · 11.4 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
MIT License
Copyright (c) 2020-2026 wsjcpp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "wsjcpp_validators.h"
// #include <arpa/inet.h>
#include <wsjcpp_core.h>
namespace wsjcpp {
validator_date::validator_date() : validator<std::string>("date") {
}
bool validator_date::is_valid(const std::string &value, std::string &error) {
return wsjcpp::is_valid_date(value, error);
}
validator_regexp::validator_regexp(const std::string &name, const std::string &_regexp)
: validator<std::string>(name) {
m_sValidator = _regexp;
m_rxValidator = std::regex(_regexp);
}
bool validator_regexp::is_valid(const std::string &value, std::string &error) {
if (!std::regex_match(value, m_rxValidator)) {
error = name() + " - Value must match regular expression " + m_sValidator;
return false;
}
return true;
}
// ----------------------------------------------------------------------
// validator_str_list
validator_str_list::validator_str_list(const std::string &sTypeName, const std::vector<std::string> &vListValues)
: validator<std::string>(sTypeName) {
m_vListValues = vListValues;
}
bool validator_str_list::is_valid(const std::string &value, std::string &error) {
if (std::find(m_vListValues.begin(), m_vListValues.end(), value) != m_vListValues.end()) {
return true;
}
error = name() + " expected one of [";
for (int i = 0; i < m_vListValues.size(); i++) {
error += "'" + m_vListValues[i] + "'";
if (i < m_vListValues.size() - 1) {
error += ", ";
}
}
error += "]";
return false;
}
// ----------------------------------------------------------------------
// validator_email
validator_email::validator_email()
: validator_regexp("email", "^[0-9a-zA-Z]{1}[0-9a-zA-Z-._]*[0-9a-zA-Z]{1}@[0-9a-zA-Z]{1}"
"[-.0-9a-zA-Z]*[0-9a-zA-Z]{1}\\.[a-zA-Z]{2,6}$") {
}
// ----------------------------------------------------------------------
// validator_uuid
validator_uuid::validator_uuid()
: validator_regexp("uuid", "^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-"
"f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$") {
}
// ----------------------------------------------------------------------
// validator_strlen
validator_strlen::validator_strlen(int min_length, int max_length)
: validator<std::string>("string_length") {
m_min_length = min_length;
m_max_length = max_length;
}
bool validator_strlen::is_valid(const std::string &value, std::string &error) {
if (value.length() < m_min_length) {
error = "Value must have more than " + std::to_string(m_min_length) + " symbols";
return false;
}
if (value.length() > m_max_length) {
error = "Value must have less than " + std::to_string(m_max_length) + " symbols";
return false;
}
return true;
}
// ----------------------------------------------------------------------
// validator_jwt
validator_jwt::validator_jwt()
: validator_regexp("jwt", "^[A-Za-z0-9-_=]+\\.[A-Za-z0-9-_=]+\\.?[A-Za-z0-9-_.+/=]*$") {
}
// ----------------------------------------------------------------------
// validator_time24
validator_time24::validator_time24() : validator<std::string>("time_h24") {
}
bool validator_time24::is_valid(const std::string &value, std::string &error) {
return wsjcpp::is_valid_time24(value, error);
}
// ----------------------------------------------------------------------
// validator_datetime
validator_datetime::validator_datetime() : validator<std::string>("datetime") {
}
bool validator_datetime::is_valid(const std::string &value, std::string &error) {
int nSize = value.size();
// '2020-01-01T00:00:00'
if (nSize != 19) {
error = "Invalid size format expected length 19";
return false;
}
std::string sDate = value.substr(0, 10);
if (!wsjcpp::is_valid_date(sDate, error)) {
return false;
}
if (value[10] != 'T') {
error = "Expected 'T' in 10 position, but got '";
error += value[10];
error += "'";
return false;
}
std::string sTime = value.substr(11, 8);
if (!wsjcpp::is_valid_time24(sTime, error)) {
return false;
}
return true;
}
// ----------------------------------------------------------------------
// validator_url
validator_url::validator_url() : validator<std::string>("url") {
m_rx_ip4_format = std::regex("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$");
}
bool validator_url::is_valid(const std::string &value, std::string &error) {
if (value.size() == 0) {
error = "Value is empty";
return false;
}
if (value.find(".") == std::string::npos) {
error = "Must contain at least one dot";
return false;
}
std::string sProtocol = wsjcpp::extract_url_protocol(value);
if (!wsjcpp::is_valid_url_protocol(sProtocol, error)) {
return false;
}
int nStartPos = sProtocol.length() + 3;
std::string sAuthorityAddressPath = "";
for (int i = nStartPos; i < value.size(); i++) {
char c = value[i];
if (c == '?') {
break;
}
sAuthorityAddressPath += c;
}
std::string sQuery = value.substr(sProtocol.length() + 3 + sAuthorityAddressPath.size());
std::string sAddressAndPath = sAuthorityAddressPath;
int nPosAuthority = sAuthorityAddressPath.find("@");
std::string sAuthority = "";
if (nPosAuthority != std::string::npos) {
sAuthority = sAuthorityAddressPath.substr(0, nPosAuthority);
sAddressAndPath = sAuthorityAddressPath.substr(nPosAuthority + 1);
}
if (sAuthority != "") {
// error = "TODO check username and password sAuthority= [" + sAuthority +
// "]";
// -.~_!$&'()*+,;=:%40:80%2f::::::
// WsjcppLog::warn(TAG, error);
// return false;
}
std::string sAddress = sAddressAndPath;
std::string sPath = "";
int nPosAddress = sAddressAndPath.find("/");
if (nPosAddress != std::string::npos) {
sAddress = sAddressAndPath.substr(0, nPosAddress);
sPath = sAddressAndPath.substr(nPosAddress);
}
if (sAddress.size() == 0) {
error = "Address could not be empty";
return false;
}
int nPosPort = sAddress.find(":");
std::string sPort = "";
if (sAddress.find(":") != std::string::npos) {
sPort = sAddress.substr(nPosPort + 1);
sAddress = sAddress.substr(0, nPosPort);
}
if (sPort != "") {
if (!wsjcpp::is_valid_port(sPort, error)) {
return false;
}
}
if (std::regex_match(sAddress, m_rx_ip4_format)) {
if (!wsjcpp::is_valid_ip4(sAddress, error)) {
return false;
}
if (sPort != "") {
int nPort = std::atoi(sPort.c_str());
if (nPort < 1 || nPort > 65535) {
error = "Port '" + std::to_string(nPort) + "' must be more then 0 and less then 65536";
return false;
}
}
} else {
if (!wsjcpp::is_valid_domain_name(sAddress, error)) {
return false;
}
}
if ((sPath == "" || sPath == "/") && sQuery == "") {
return true;
}
if (sPath != "") {
for (int i = 0; i < sPath.length(); i++) {
char c = sPath[i];
if (c == ' ') {
error = "Path could not contains whitespace ' '";
return false;
}
}
}
if (sQuery != "") {
for (int i = 0; i < sQuery.length(); i++) {
char c = sQuery[i];
if (c == ' ') {
error = "Query could not contains whitespace ' ' (must be encoded)";
return false;
}
}
}
// error = "sAddressAndPath=[" + sAddressAndPath + "], , sAddress=[" +
// sAddress + "]";
return true;
}
// ----------------------------------------------------------------------
// validator_domain_name
validator_domain_name::validator_domain_name() : validator<std::string>("domain_name") {
}
bool validator_domain_name::is_valid(const std::string &value, std::string &error) {
return wsjcpp::is_valid_domain_name(value, error);
}
// ----------------------------------------------------------------------
// validator_base64
validator_base64::validator_base64() : validator<std::string>("base64") {
}
// ----------------------------------------------------------------------
bool validator_base64::is_valid(const std::string &value, std::string &error) {
return wsjcpp::is_valid_base64(value, error);
}
// ----------------------------------------------------------------------
// validator_int
validator_int::validator_int() : validator<std::string>("int") {
}
// ----------------------------------------------------------------------
bool validator_int::is_valid(const std::string &value, std::string &error) {
int nSize = value.size();
bool bHasOneAndMoreNumbers = false;
for (int i = 0; i < nSize; i++) {
char c = value[i];
if (c == '-' && i == 0) {
continue;
}
if (c < '0' || c > '9') {
error = "Unexpected char '";
error += c;
error += "' in " + std::to_string(i) + " position";
return false;
}
bHasOneAndMoreNumbers = true;
}
return bHasOneAndMoreNumbers;
}
// ----------------------------------------------------------------------
// validator_hex
validator_hex::validator_hex() : validator<std::string>("hex") {
}
// ----------------------------------------------------------------------
bool validator_hex::is_valid(const std::string &value, std::string &error) {
int nSize = value.size();
if (nSize == 0) {
error = "Empty string";
return false;
}
for (int i = 0; i < nSize; i++) {
char c = value[i];
if (c == '-' && i == 0) {
continue;
}
if ((c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F')) {
error = "Unexpected char '";
error += c;
error += "' in " + std::to_string(i) + " position";
return false;
}
}
return true;
}
// ----------------------------------------------------------------------
// validator_int_min
validator_int_min::validator_int_min(int min_value) : validator<int>("int_min") {
m_min_value = min_value;
}
// ----------------------------------------------------------------------
bool validator_int_min::is_valid(const int &value, std::string &error) {
if (value < m_min_value) {
error = "Value must be more or equal then " + std::to_string(m_min_value);
return false;
}
return true;
}
// ----------------------------------------------------------------------
// validator_int_max
validator_int_max::validator_int_max(int max_value) : validator<int>("int_max") {
m_max_value = max_value;
}
// ----------------------------------------------------------------------
bool validator_int_max::is_valid(const int &value, std::string &error) {
if (value > m_max_value) {
error = "Value must be less or equal then " + std::to_string(m_max_value);
return false;
}
return true;
}
} // namespace wsjcpp