-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsjcpp_validators.cpp
More file actions
799 lines (681 loc) · 24.4 KB
/
wsjcpp_validators.cpp
File metadata and controls
799 lines (681 loc) · 24.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
#include "wsjcpp_validators.h"
#include <arpa/inet.h>
#include <wsjcpp_core.h>
bool WsjcppValidators::isValidDate(const std::string &sValue, std::string &sError) {
int nSize = sValue.size();
if (nSize != 10) {
sError = "Invalid size format expected length 10";
return false;
}
for (int i = 0; i < 10; i++) {
char c = sValue[i];
if (i == 4 || i == 7) {
if (c != '-') {
sError = "Expected '-' in " + std::to_string(i) + " position, but got '";
sError += c;
sError += "'";
return false;
}
continue;
}
if (c < '0' || c > '9') {
sError = "Unexpected char '";
sError += c;
sError += "' in " + std::to_string(i) + " position";
return false;
}
}
// 2020-01-01
std::string sYear = sValue.substr(0,4);
int nYear = std::atoi(sYear.c_str());
std::string sMonth = sValue.substr(5,2);
int nMonth = std::atoi(sMonth.c_str());
if (nMonth < 1 || nMonth > 12) {
sError = "Invalid value nunber of month '" + std::to_string(nMonth) + "' expected 01..12";
return false;
}
int nMaxDay = 0;
if (nMonth == 1 || nMonth == 3 || nMonth == 5 || nMonth == 7 || nMonth == 8 || nMonth == 10 || nMonth == 12) {
nMaxDay = 31;
} else if (nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11) {
nMaxDay = 30;
} else if (nMonth == 2 && nYear % 4 == 0) {
nMaxDay = 29;
} else if (nMonth == 2 && nYear % 4 != 0) {
nMaxDay = 28;
}
std::string sDay = sValue.substr(8,2);
int nDay = std::atoi(sDay.c_str());
if (nDay < 1 || nDay > nMaxDay) {
sError = "Invalid value number of day '" + std::to_string(nDay) + "' expected 01.." + std::to_string(nMaxDay);
return false;
}
return true;
}
// ----------------------------------------------------------------------
bool WsjcppValidators::isValidTimeH24(const std::string &sValue, std::string &sError) {
int nSize = sValue.size();
if (nSize != 8) {
sError = "Invalid size format expected length 8";
return false;
}
for (int i = 0; i < 8; i++) {
char c = sValue[i];
if (i == 2 || i == 5) {
if (c != ':') {
sError = "Expected ':' in " + std::to_string(i) + " position, but got '";
sError += c;
sError += "'";
return false;
}
continue;
}
if (c < '0' || c > '9') {
sError = "Unexpected char '";
sError += c;
sError += "' in " + std::to_string(i) + " position";
return false;
}
}
std::string sHours = sValue.substr(0,2);
int nHours = std::atoi(sHours.c_str());
if (nHours > 23) {
sError = "Invalid value of hours '" + std::to_string(nHours) + "' expected 00..23";
return false;
}
std::string sMinutes = sValue.substr(3,2);
int nMinutes = std::atoi(sMinutes.c_str());
if (nMinutes > 59) {
sError = "Invalid value of minutes '" + std::to_string(nMinutes) + "' expected 00..59";
return false;
}
std::string sSeconds = sValue.substr(6,2);
int nSeconds = std::atoi(sSeconds.c_str());
if (nSeconds > 59) {
sError = "Invalid value of seconds '" + std::to_string(nSeconds) + "' expected 00..59";
return false;
}
return true;
}
// ----------------------------------------------------------------------
bool WsjcppValidators::isValidDomainName(const std::string &sValue, std::string &sError) {
std::vector<std::string> vSubDomains;
std::string sTmpDomain = "";
int nAddressLen = sValue.size();
char cPrev = 0;
for (int i = 0; i < nAddressLen; i++) {
char c = sValue[i];
if (i == 0 && c == '.') {
sError = "Domain Name '" + sValue + "' could not be start on '.'";
return false;
}
if (c == '.') {
if (sTmpDomain != "") {
vSubDomains.push_back(sTmpDomain);
sTmpDomain = "";
continue;
} else {
sError = "Domain Name '" + sValue + "' could not contains '..'";
return false;
}
}
if (
(c >= 'A' && c <= 'Z')
|| (c >= 'a' && c <= 'z')
|| (c >= '0' && c <= '9')
|| c == '-'
) {
sTmpDomain += c;
if (cPrev == '-' && c == cPrev) {
sError = "Domain Name '" + sValue + "' could not two times in a row '";
sError += c;
sError += c;
sError += "'";
return false;
}
cPrev = c;
} else {
sError = "Domain Name '" + sValue + "' contains unexpected '";
sError += c;
sError += "'";
return false;
}
}
if (sTmpDomain != "") {
vSubDomains.push_back(sTmpDomain);
sTmpDomain = "";
} else {
sError = "Domain Name '" + sValue + "' could not contains '.' on end";
return false;
}
if (vSubDomains.size() < 2) {
sError = "Domain Name '" + sValue + "' must contains least one dot";
return false;
}
std::string sRootDomain = vSubDomains[vSubDomains.size()-1];
if (sRootDomain.size() < 2) {
sError = "Domain Name '" + sValue + "' has wrong root domain '" + sRootDomain + "' length must be more then 1";
return false;
}
for (int i = 0; i < sRootDomain.size(); i++) {
char c = sRootDomain[i];
if (
(c < 'A' || c > 'Z')
&& (c < 'a' || c > 'z')
) {
sError = "Domain Name '" + sValue + "' has wrong root domain '" + sRootDomain + "' must have only chars";
return false;
}
}
for (int i = 0; i < vSubDomains.size(); i++) {
std::string sDomain = vSubDomains[i];
char c = sDomain[0];
if (
(c < 'A' || c > 'Z')
&& (c < 'a' || c > 'z')
&& (c < '0' || c > '9')
) {
sError = "Subdomain '" + sDomain + "' could not start on '";
sError += c;
sError += "'";
return false;
}
c = sDomain[sDomain.size()-1];
if (
(c < 'A' || c > 'Z')
&& (c < 'a' || c > 'z')
&& (c < '0' || c > '9')
) {
sError = "Subdomain '" + sDomain + "' could not end on '";
sError += c;
sError += "'";
return false;
}
}
return true;
}
// ----------------------------------------------------------------------
bool WsjcppValidators::isValidPort(const std::string &sValue, std::string &sError) {
int nPort = std::atoi(sValue.c_str());
return WsjcppValidators::isValidPort(nPort, sError);
}
// ----------------------------------------------------------------------
bool WsjcppValidators::isValidPort(int nValue, std::string &sError) {
if (nValue < 1 || nValue > 65535) {
sError = "Port '" + std::to_string(nValue) + "' must be more then 0 and less then 65536";
return false;
}
return true;
}
// ----------------------------------------------------------------------
bool WsjcppValidators::isValidURLProtocol(const std::string &sValue, std::string &sError) {
if (
sValue != "http"
&& sValue != "https"
&& sValue != "ws"
&& sValue != "wss"
&& sValue != "ftp"
&& sValue != "ssl"
) {
sError = "Unexpected protocol '" + sValue + "'";
return false;
}
return true;
}
// ----------------------------------------------------------------------
bool WsjcppValidators::isValidBase64(const std::string &sValue, std::string &sError) {
int nSize = sValue.size();
if (nSize % 4 != 0) {
sError = "Value size must be a multiple of 4";
return false;
}
bool bLastChar = false;
for (int i = 0; i < nSize; i++) {
char c = sValue[i];
if (!bLastChar && c == '=') {
bLastChar = true;
continue;
}
if (bLastChar && c == '=') {
continue;
}
if (bLastChar && c != '=') {
sError = "Unexpected char '";
sError += c;
sError += "' after '=' in " + std::to_string(i) + " position";
return false;
}
if (
(c < 'A' || c > 'Z')
&& (c < 'a' || c > 'z')
&& (c < '0' || c > '9')
&& c != '+'
&& c != '/'
) {
sError = "Unexpected char '";
sError += c;
sError += "' in " + std::to_string(i) + " position";
return false;
}
}
return true;
}
// ----------------------------------------------------------------------
bool WsjcppValidators::isValidIPv4(const std::string &sValue, std::string &sError) {
int n = 0;
std::string s[4] = {"", "", "", ""};
for (int i = 0; i < sValue.length(); i++) {
char c = sValue[i];
if (n > 3) {
sError = "Groups number must be less than 5 (like '0.0.0.0')";
return false;
}
if (c >= '0' && c <= '9') {
s[n] += c;
} else if (c == '.') {
n++;
} else {
sError = "Unexpected character '";
sError += c;
sError += "'";
return false;
}
}
for (int i = 0; i < 4; i++) {
if (s[i].length() > 3) {
sError = "Value '" + s[i] + "' could not contains more than 3 digits in a row";
return false;
}
int p = std::stoi(s[i]);
if (p > 255 || p < 0) {
sError = "Value '" + std::to_string(p) + "' must be 0..255";
return false;
}
}
return true;
}
// ----------------------------------------------------------------------
bool WsjcppValidators::isValidIPv6(const std::string &sValue, std::string &sError) {
// TODO redesign without arpa
unsigned char buf[sizeof(struct in6_addr)];
bool isValid = inet_pton(AF_INET6, sValue.c_str(), buf);
if (!isValid) {
sError = "Format of ipv6 invalid";
}
return isValid;
}
// ----------------------------------------------------------------------
// WsjcppValidatorStringBase
WsjcppValidatorStringBase::WsjcppValidatorStringBase(const std::string &sTypeName) {
TAG = "WsjcppValidatorStringBase";
m_sTypeName = sTypeName;
}
// ----------------------------------------------------------------------
WsjcppValidatorType WsjcppValidatorStringBase::getBaseType() {
return WsjcppValidatorType::WSJCPP_VALIDATOR_STRING;
}
// ----------------------------------------------------------------------
std::string WsjcppValidatorStringBase::getTypeName() {
return m_sTypeName;
}
// ----------------------------------------------------------------------
// WsjcppValidatorStringRegexpBase
WsjcppValidatorStringRegexpBase::WsjcppValidatorStringRegexpBase(const std::string &typeName, const std::string &sValidator)
: WsjcppValidatorStringBase(typeName) {
TAG = "ValidatorStringRegexpBase";
m_sValidator = sValidator;
m_rxValidator = std::regex(sValidator);
}
// ----------------------------------------------------------------------
bool WsjcppValidatorStringRegexpBase::isValid(const std::string &sValue, std::string &sError) {
if (!std::regex_match(sValue, m_rxValidator)) {
sError = getTypeName() + " - Value must match regular expression " + m_sValidator;
return false;
}
return true;
}
// ----------------------------------------------------------------------
// WsjcppValidatorStringListBase
WsjcppValidatorStringListBase::WsjcppValidatorStringListBase(const std::string &sTypeName, const std::vector<std::string> &vListValues)
: WsjcppValidatorStringBase(sTypeName) {
m_vListValues = vListValues;
}
// ----------------------------------------------------------------------
bool WsjcppValidatorStringListBase::isValid(const std::string &sValue, std::string &sError) {
if (std::find(m_vListValues.begin(), m_vListValues.end(), sValue) != m_vListValues.end()) {
return true;
}
sError = getTypeName() + " expected one of [";
for (int i = 0; i < m_vListValues.size(); i++) {
sError += "'" + m_vListValues[i] + "'";
if (i < m_vListValues.size() - 1) {
sError += ", ";
}
}
sError += "]";
return false;
}
// ----------------------------------------------------------------------
// WsjcppValidatorEmail
WsjcppValidatorEmail::WsjcppValidatorEmail()
: WsjcppValidatorStringRegexpBase(
"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}$"
) {
TAG = "WsjcppValidatorEmail";
}
// ----------------------------------------------------------------------
// WsjcppValidatorUUID
WsjcppValidatorUUID::WsjcppValidatorUUID()
: WsjcppValidatorStringRegexpBase(
"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}$"
) {
TAG = "WsjcppValidatorUUID";
}
// ----------------------------------------------------------------------
// WsjcppValidatorStringLenght
WsjcppValidatorStringLength::WsjcppValidatorStringLength(int nMinLength, int nMaxLength)
: WsjcppValidatorStringBase("string_length") {
TAG = "WsjcppValidatorStringLenght";
m_nMinLength = nMinLength;
m_nMaxLength = nMaxLength;
}
// ----------------------------------------------------------------------
bool WsjcppValidatorStringLength::isValid(const std::string &sValue, std::string &sError) {
if (sValue.length() < m_nMinLength) {
sError = "Value must have more than " + std::to_string(m_nMinLength) + " simbols";
return false;
}
if (sValue.length() > m_nMaxLength) {
sError = "Value must have less than " + std::to_string(m_nMaxLength) + " simbols";
return false;
}
return true;
}
// ----------------------------------------------------------------------
// WsjcppValidatorJWT
WsjcppValidatorJWT::WsjcppValidatorJWT()
: WsjcppValidatorStringRegexpBase(
"jwt",
"^[A-Za-z0-9-_=]+\\.[A-Za-z0-9-_=]+\\.?[A-Za-z0-9-_.+/=]*$"
) {
TAG = "WsjcppValidatorJWT";
}
// ----------------------------------------------------------------------
// WsjcppValidatorDate
WsjcppValidatorDate::WsjcppValidatorDate()
: WsjcppValidatorStringBase("date") {
TAG = "WsjcppValidatorDate";
}
// ----------------------------------------------------------------------
bool WsjcppValidatorDate::isValid(const std::string &sValue, std::string &sError) {
return WsjcppValidators::isValidDate(sValue, sError);
}
// ----------------------------------------------------------------------
// WsjcppValidatorTimeH24
WsjcppValidatorTimeH24::WsjcppValidatorTimeH24()
: WsjcppValidatorStringBase("time_h24") {
TAG = "WsjcppValidatorTime";
}
// ----------------------------------------------------------------------
bool WsjcppValidatorTimeH24::isValid(const std::string &sValue, std::string &sError) {
return WsjcppValidators::isValidTimeH24(sValue, sError);
}
// ----------------------------------------------------------------------
// WsjcppValidatorDateTime
WsjcppValidatorDateTime::WsjcppValidatorDateTime()
: WsjcppValidatorStringBase("datetime") {
TAG = "WsjcppValidatorDateTime";
}
// ----------------------------------------------------------------------
bool WsjcppValidatorDateTime::isValid(const std::string &sValue, std::string &sError) {
int nSize = sValue.size();
// '2020-01-01T00:00:00'
if (nSize != 19) {
sError = "Invalid size format expected length 19";
return false;
}
std::string sDate = sValue.substr(0,10);
if (!WsjcppValidators::isValidDate(sDate, sError)) {
return false;
}
if (sValue[10] != 'T') {
sError = "Expected 'T' in 10 position, but got '";
sError += sValue[10];
sError += "'";
return false;
}
std::string sTime = sValue.substr(11,8);
if (!WsjcppValidators::isValidTimeH24(sTime, sError)) {
return false;
}
return true;
}
// ----------------------------------------------------------------------
// WsjcppValidatorURL
WsjcppValidatorURL::WsjcppValidatorURL()
: WsjcppValidatorStringBase("url") {
TAG = "WsjcppValidatorURL";
m_rxLikeIPv4Format = std::regex("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$");
}
// ----------------------------------------------------------------------
bool WsjcppValidatorURL::isValid(const std::string &sValue, std::string &sError) {
if (sValue.size() == 0) {
sError = "Value is empty";
return false;
}
if (sValue.find(".") == std::string::npos) {
sError = "Must contain at least one dot";
return false;
}
std::string sProtocol = WsjcppCore::extractURLProtocol(sValue);
if (!WsjcppValidators::isValidURLProtocol(sProtocol, sError)) {
return false;
}
int nStartPos = sProtocol.length() + 3;
std::string sAuthorityAddressPath = "";
for (int i = nStartPos; i < sValue.size(); i++) {
char c = sValue[i];
if (c == '?') {
break;
}
sAuthorityAddressPath += c;
}
std::string sQuery = sValue.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 != "") {
// sError = "TODO check username and password sAuthority= [" + sAuthority + "]";
// -.~_!$&'()*+,;=:%40:80%2f::::::
// WsjcppLog::warn(TAG, sError);
// 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) {
sError = "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 (!WsjcppValidators::isValidPort(sPort, sError)) {
return false;
}
}
if (std::regex_match(sAddress, m_rxLikeIPv4Format)) {
if (!WsjcppValidators::isValidIPv4(sAddress, sError)) {
return false;
}
if (sPort != "") {
int nPort = std::atoi(sPort.c_str());
if (nPort < 1 || nPort > 65535) {
sError = "Port '" + std::to_string(nPort) + "' must be more then 0 and less then 65536";
return false;
}
}
} else {
if (!WsjcppValidators::isValidDomainName(sAddress, sError)) {
return false;
}
}
if ((sPath == "" || sPath == "/") && sQuery == "") {
return true;
}
if (sPath != "") {
for (int i = 0; i < sPath.length(); i++) {
char c = sPath[i];
if (c == ' ') {
sError = "Path could not contains whitespace ' '";
return false;
}
}
}
if (sQuery != "") {
for (int i = 0; i < sQuery.length(); i++) {
char c = sQuery[i];
if (c == ' ') {
sError = "Query could not contains whitespace ' ' (must be encoded)";
return false;
}
}
}
// sError = "sAddressAndPath=[" + sAddressAndPath + "], , sAddress=[" + sAddress + "]";
return true;
}
// ----------------------------------------------------------------------
// WsjcppValidatorBase64
WsjcppValidatorBase64::WsjcppValidatorBase64()
: WsjcppValidatorStringBase("base64") {
TAG = "WsjcppValidatorBase64";
}
// ----------------------------------------------------------------------
bool WsjcppValidatorBase64::isValid(const std::string &sValue, std::string &sError) {
return WsjcppValidators::isValidBase64(sValue, sError);
}
// ----------------------------------------------------------------------
// WsjcppValidatorNumber
WsjcppValidatorNumber::WsjcppValidatorNumber()
: WsjcppValidatorStringBase("number") {
TAG = "WsjcppValidatorNumber";
}
// ----------------------------------------------------------------------
bool WsjcppValidatorNumber::isValid(const std::string &sValue, std::string &sError) {
int nSize = sValue.size();
bool bHasOneAndMoreNumbers = false;
for (int i = 0; i < nSize; i++) {
char c = sValue[i];
if (c == '-' && i == 0) {
continue;
}
if (c < '0' || c > '9') {
sError = "Unexpected char '";
sError += c;
sError += "' in " + std::to_string(i) + " position";
return false;
}
bHasOneAndMoreNumbers = true;
}
return bHasOneAndMoreNumbers;
}
// ----------------------------------------------------------------------
// WsjcppValidatorHex
WsjcppValidatorHex::WsjcppValidatorHex()
: WsjcppValidatorStringBase("hex") {
TAG = "WsjcppValidatorHex";
}
// ----------------------------------------------------------------------
bool WsjcppValidatorHex::isValid(const std::string &sValue, std::string &sError) {
int nSize = sValue.size();
if (nSize == 0) {
sError = "Empty string";
return false;
}
for (int i = 0; i < nSize; i++) {
char c = sValue[i];
if (c == '-' && i == 0) {
continue;
}
if ((c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F')) {
sError = "Unexpected char '";
sError += c;
sError += "' in " + std::to_string(i) + " position";
return false;
}
}
return true;
}
// ----------------------------------------------------------------------
// WsjcppValidatorIntegerBase
WsjcppValidatorIntegerBase::WsjcppValidatorIntegerBase(const std::string &sTypeName) {
TAG = "WsjcppValidatorIntegerBase";
m_sTypeName = sTypeName;
}
// ----------------------------------------------------------------------
WsjcppValidatorType WsjcppValidatorIntegerBase::getBaseType() {
return WsjcppValidatorType::WSJCPP_VALIDATOR_INTEGER;
}
// ----------------------------------------------------------------------
std::string WsjcppValidatorIntegerBase::getTypeName() {
return m_sTypeName;
}
// ----------------------------------------------------------------------
// WsjcppValidatorIntegerMinValue
WsjcppValidatorIntegerMinValue::WsjcppValidatorIntegerMinValue(int nMinValue)
: WsjcppValidatorIntegerBase("integer_min_value") {
TAG = "WsjcppValidatorIntegerMinValue";
m_nMinValue = nMinValue;
}
// ----------------------------------------------------------------------
bool WsjcppValidatorIntegerMinValue::isValid(int nValue, std::string &sError) {
if (nValue < m_nMinValue) {
sError = "Value must be more or equal then " + std::to_string(m_nMinValue);
return false;
}
return true;
}
// ----------------------------------------------------------------------
// WsjcppValidatorIntegerMaxValue
WsjcppValidatorIntegerMaxValue::WsjcppValidatorIntegerMaxValue(int nMaxValue)
: WsjcppValidatorIntegerBase("integer_max_value") {
TAG = "WsjcppValidatorIntegerMaxValue";
m_nMaxValue = nMaxValue;
}
// ----------------------------------------------------------------------
bool WsjcppValidatorIntegerMaxValue::isValid(int nValue, std::string &sError) {
if (nValue > m_nMaxValue) {
sError = "Value must be less or equal then " + std::to_string(m_nMaxValue);
return false;
}
return true;
}
// ----------------------------------------------------------------------
// WsjcppValidatorJsonBase
WsjcppValidatorJsonBase::WsjcppValidatorJsonBase(const std::string &sTypeName) {
TAG = "WsjcppValidatorJsonBase";
m_sTypeName = sTypeName;
}
// ----------------------------------------------------------------------
WsjcppValidatorType WsjcppValidatorJsonBase::getBaseType() {
return WsjcppValidatorType::WSJCPP_VALIDATOR_JSON;
}
// ----------------------------------------------------------------------
std::string WsjcppValidatorJsonBase::getTypeName() {
return m_sTypeName;
}
// ----------------------------------------------------------------------