Skip to content
This repository was archived by the owner on Aug 16, 2026. It is now read-only.

Commit 3358a1e

Browse files
committed
Moved part of methods to wsjcpp-core. Start renaming names
1 parent dfb5915 commit 3358a1e

23 files changed

Lines changed: 310 additions & 355 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

22
# wsjcpp-validators Changelog
33

4+
## [v0.2.0]
5+
6+
- Moved part of methods to wsjcpp-core
7+
- Refactored names
48

59
## [v0.1.4]
610

src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ distribution:
2121
- source-file: src/wsjcpp_core/wsjcpp_core.cpp
2222
target-file: wsjcpp_core.cpp
2323
type: "source-code"
24-
sha1: "25926b911a674cf969b94f29e4480f242b31094a"
24+
sha1: "601ce90d3d2acbf187a036ac44f02e79710fb032"
2525
- source-file: src/wsjcpp_core/wsjcpp_core.h
2626
target-file: wsjcpp_core.h
2727
type: "source-code" # todo must be header-file
28-
sha1: "01aa9c5363db7523efb2dad5b32f90bdba1c16e1"
28+
sha1: "3d5d70a608dea3965e6daf8c7cdaccd6c139e5a3"
2929
- source-file: "src/wsjcpp_core/wsjcpp_unit_tests.cpp"
3030
target-file: "wsjcpp_unit_tests.cpp"
3131
type: "unit-tests"

src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,4 +1402,249 @@ bool is_valid_ip6(const std::string &value, std::string &error) {
14021402
return isValid;
14031403
}
14041404

1405+
1406+
bool is_valid_date(const std::string &value, std::string &error) {
1407+
int nSize = value.size();
1408+
if (nSize != 10) {
1409+
error = "Invalid size format expected length 10";
1410+
return false;
1411+
}
1412+
1413+
for (int i = 0; i < 10; i++) {
1414+
char c = value[i];
1415+
if (i == 4 || i == 7) {
1416+
if (c != '-') {
1417+
error = "Expected '-' in " + std::to_string(i) + " position, but got '";
1418+
error += c;
1419+
error += "'";
1420+
return false;
1421+
}
1422+
continue;
1423+
}
1424+
if (c < '0' || c > '9') {
1425+
error = "Unexpected char '";
1426+
error += c;
1427+
error += "' in " + std::to_string(i) + " position";
1428+
return false;
1429+
}
1430+
}
1431+
// 2020-01-01
1432+
std::string sYear = value.substr(0, 4);
1433+
int nYear = std::atoi(sYear.c_str());
1434+
1435+
std::string sMonth = value.substr(5, 2);
1436+
int nMonth = std::atoi(sMonth.c_str());
1437+
if (nMonth < 1 || nMonth > 12) {
1438+
error = "Invalid value number of month '" + std::to_string(nMonth) + "' expected 01..12";
1439+
return false;
1440+
}
1441+
1442+
int nMaxDay = 0;
1443+
if (nMonth == 1 || nMonth == 3 || nMonth == 5 || nMonth == 7 || nMonth == 8 || nMonth == 10 || nMonth == 12) {
1444+
nMaxDay = 31;
1445+
} else if (nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11) {
1446+
nMaxDay = 30;
1447+
} else if (nMonth == 2 && nYear % 4 == 0) {
1448+
nMaxDay = 29;
1449+
} else if (nMonth == 2 && nYear % 4 != 0) {
1450+
nMaxDay = 28;
1451+
}
1452+
1453+
std::string sDay = value.substr(8, 2);
1454+
int nDay = std::atoi(sDay.c_str());
1455+
if (nDay < 1 || nDay > nMaxDay) {
1456+
error = "Invalid value number of day '" + std::to_string(nDay) + "' expected 01.." + std::to_string(nMaxDay);
1457+
return false;
1458+
}
1459+
return true;
1460+
}
1461+
1462+
bool is_valid_time24(const std::string &value, std::string &error) {
1463+
int nSize = value.size();
1464+
if (nSize != 8) {
1465+
error = "Invalid size format expected length 8";
1466+
return false;
1467+
}
1468+
1469+
for (int i = 0; i < 8; i++) {
1470+
char c = value[i];
1471+
if (i == 2 || i == 5) {
1472+
if (c != ':') {
1473+
error = "Expected ':' in " + std::to_string(i) + " position, but got '";
1474+
error += c;
1475+
error += "'";
1476+
return false;
1477+
}
1478+
continue;
1479+
}
1480+
if (c < '0' || c > '9') {
1481+
error = "Unexpected char '";
1482+
error += c;
1483+
error += "' in " + std::to_string(i) + " position";
1484+
return false;
1485+
}
1486+
}
1487+
1488+
std::string sHours = value.substr(0, 2);
1489+
int nHours = std::atoi(sHours.c_str());
1490+
if (nHours > 23) {
1491+
error = "Invalid value of hours '" + std::to_string(nHours) + "' expected 00..23";
1492+
return false;
1493+
}
1494+
std::string sMinutes = value.substr(3, 2);
1495+
int nMinutes = std::atoi(sMinutes.c_str());
1496+
if (nMinutes > 59) {
1497+
error = "Invalid value of minutes '" + std::to_string(nMinutes) + "' expected 00..59";
1498+
return false;
1499+
}
1500+
1501+
std::string sSeconds = value.substr(6, 2);
1502+
int nSeconds = std::atoi(sSeconds.c_str());
1503+
if (nSeconds > 59) {
1504+
error = "Invalid value of seconds '" + std::to_string(nSeconds) + "' expected 00..59";
1505+
return false;
1506+
}
1507+
return true;
1508+
}
1509+
1510+
bool is_valid_domain_name(const std::string &value, std::string &error) {
1511+
std::vector<std::string> vSubDomains;
1512+
std::string sTmpDomain = "";
1513+
int nAddressLen = value.size();
1514+
char cPrev = 0;
1515+
for (int i = 0; i < nAddressLen; i++) {
1516+
char c = value[i];
1517+
if (i == 0 && c == '.') {
1518+
error = "Domain Name '" + value + "' could not be start on '.'";
1519+
return false;
1520+
}
1521+
if (c == '.') {
1522+
if (sTmpDomain != "") {
1523+
vSubDomains.push_back(sTmpDomain);
1524+
sTmpDomain = "";
1525+
continue;
1526+
} else {
1527+
error = "Domain Name '" + value + "' could not contains '..'";
1528+
return false;
1529+
}
1530+
}
1531+
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-') {
1532+
sTmpDomain += c;
1533+
if (cPrev == '-' && c == cPrev) {
1534+
error = "Domain Name '" + value + "' could not two times in a row '";
1535+
error += c;
1536+
error += c;
1537+
error += "'";
1538+
return false;
1539+
}
1540+
cPrev = c;
1541+
} else {
1542+
error = "Domain Name '" + value + "' contains unexpected '";
1543+
error += c;
1544+
error += "'";
1545+
return false;
1546+
}
1547+
}
1548+
1549+
if (sTmpDomain != "") {
1550+
vSubDomains.push_back(sTmpDomain);
1551+
sTmpDomain = "";
1552+
} else {
1553+
error = "Domain Name '" + value + "' could not contains '.' on end";
1554+
return false;
1555+
}
1556+
1557+
if (vSubDomains.size() < 2) {
1558+
error = "Domain Name '" + value + "' must contains least one dot";
1559+
return false;
1560+
}
1561+
std::string sRootDomain = vSubDomains[vSubDomains.size() - 1];
1562+
if (sRootDomain.size() < 2) {
1563+
error = "Domain Name '" + value + "' has wrong root domain '" + sRootDomain + "' length must be more then 1";
1564+
return false;
1565+
}
1566+
for (int i = 0; i < sRootDomain.size(); i++) {
1567+
char c = sRootDomain[i];
1568+
if ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z')) {
1569+
error = "Domain Name '" + value + "' has wrong root domain '" + sRootDomain + "' must have only chars";
1570+
return false;
1571+
}
1572+
}
1573+
1574+
for (int i = 0; i < vSubDomains.size(); i++) {
1575+
std::string sDomain = vSubDomains[i];
1576+
char c = sDomain[0];
1577+
if ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9')) {
1578+
error = "Subdomain '" + sDomain + "' could not start on '";
1579+
error += c;
1580+
error += "'";
1581+
return false;
1582+
}
1583+
c = sDomain[sDomain.size() - 1];
1584+
if ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9')) {
1585+
error = "Subdomain '" + sDomain + "' could not end on '";
1586+
error += c;
1587+
error += "'";
1588+
return false;
1589+
}
1590+
}
1591+
return true;
1592+
}
1593+
1594+
bool is_valid_port(const std::string &value, std::string &error) {
1595+
int nPort = std::atoi(value.c_str());
1596+
return wsjcpp::is_valid_port(nPort, error);
1597+
}
1598+
1599+
bool is_valid_port(int nValue, std::string &error) {
1600+
if (nValue < 1 || nValue > 65535) {
1601+
error = "Port '" + std::to_string(nValue) + "' must be more then 0 and less then 65536";
1602+
return false;
1603+
}
1604+
return true;
1605+
}
1606+
1607+
bool is_valid_url_protocol(const std::string &value, std::string &error) {
1608+
if (value != "http" && value != "https" && value != "ws" && value != "wss" && value != "ftp" &&
1609+
value != "ssl") {
1610+
error = "Unexpected protocol '" + value + "'";
1611+
return false;
1612+
}
1613+
return true;
1614+
}
1615+
1616+
bool is_valid_base64(const std::string &value, std::string &error) {
1617+
int nSize = value.size();
1618+
if (nSize % 4 != 0) {
1619+
error = "Value size must be a multiple of 4";
1620+
return false;
1621+
}
1622+
bool bLastChar = false;
1623+
for (int i = 0; i < nSize; i++) {
1624+
char c = value[i];
1625+
if (!bLastChar && c == '=') {
1626+
bLastChar = true;
1627+
continue;
1628+
}
1629+
if (bLastChar && c == '=') {
1630+
continue;
1631+
}
1632+
1633+
if (bLastChar && c != '=') {
1634+
error = "Unexpected char '";
1635+
error += c;
1636+
error += "' after '=' in " + std::to_string(i) + " position";
1637+
return false;
1638+
}
1639+
1640+
if ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '+' && c != '/') {
1641+
error = "Unexpected char '";
1642+
error += c;
1643+
error += "' in " + std::to_string(i) + " position";
1644+
return false;
1645+
}
1646+
}
1647+
return true;
1648+
}
1649+
14051650
} // namespace wsjcpp

src.wsjcpp/wsjcpp_core/wsjcpp_core.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,15 @@ bool is_linux();
268268
bool is_windows();
269269
std::string padding_right(const std::string &source, char pad_char, size_t length);
270270
std::string padding_left(const std::string &source, char pad_char, size_t length);
271-
272271
bool is_valid_ip4(const std::string &value, std::string &error);
273272
bool is_valid_ip6(const std::string &value, std::string &error);
273+
bool is_valid_date(const std::string &value, std::string &error);
274+
bool is_valid_time24(const std::string &value, std::string &error);
275+
bool is_valid_url_protocol(const std::string &value, std::string &error);
276+
bool is_valid_domain_name(const std::string &value, std::string &error);
277+
bool is_valid_port(const std::string &value, std::string &error);
278+
bool is_valid_port(int nValue, std::string &error);
279+
bool is_valid_base64(const std::string &value, std::string &error);
274280

275281
} // namespace wsjcpp
276282

src/main.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,25 @@ int main(int argc, char *argv[]) {
4949
}
5050
std::string sArg1(argv[1]);
5151

52-
std::vector<WsjcppValidatorStringBase *> vValidators;
52+
std::vector<wsjcpp::WsjcppValidatorStringBase *> vValidators;
5353
// vValidators.push_back(new WsjcppValidatorStringLength(5, 100));
5454
// vValidators.push_back(new WsjcppValidatorStringLength(1, 5));
55-
vValidators.push_back(new WsjcppValidatorEmail());
56-
vValidators.push_back(new WsjcppValidatorUUID());
57-
vValidators.push_back(new WsjcppValidatorStringListBase("lang", {"en", "de", "ru"}));
58-
vValidators.push_back(new WsjcppValidatorStringRegexpBase("testre", "^[a-zA-Z]+$"));
59-
vValidators.push_back(new WsjcppValidatorJWT());
60-
vValidators.push_back(new WsjcppValidatorDate());
61-
vValidators.push_back(new WsjcppValidatorTimeH24());
62-
vValidators.push_back(new WsjcppValidatorDateTime());
55+
vValidators.push_back(new wsjcpp::WsjcppValidatorEmail());
56+
vValidators.push_back(new wsjcpp::WsjcppValidatorUUID());
57+
vValidators.push_back(new wsjcpp::WsjcppValidatorStringListBase("lang", {"en", "de", "ru"}));
58+
vValidators.push_back(new wsjcpp::WsjcppValidatorStringRegexpBase("testre", "^[a-zA-Z]+$"));
59+
vValidators.push_back(new wsjcpp::WsjcppValidatorJWT());
60+
vValidators.push_back(new wsjcpp::WsjcppValidatorDate());
61+
vValidators.push_back(new wsjcpp::WsjcppValidatorTimeH24());
62+
vValidators.push_back(new wsjcpp::WsjcppValidatorDateTime());
6363
// vValidators.push_back(new WsjcppValidatorURL());
64-
vValidators.push_back(new WsjcppValidatorBase64());
65-
vValidators.push_back(new WsjcppValidatorNumber());
66-
vValidators.push_back(new WsjcppValidatorHex());
64+
vValidators.push_back(new wsjcpp::WsjcppValidatorBase64());
65+
vValidators.push_back(new wsjcpp::WsjcppValidatorNumber());
66+
vValidators.push_back(new wsjcpp::WsjcppValidatorHex());
6767

6868
std::string sResult = "";
6969
for (int i = 0; i < vValidators.size(); i++) {
70-
WsjcppValidatorStringBase *pValidator = vValidators[i];
70+
wsjcpp::WsjcppValidatorStringBase *pValidator = vValidators[i];
7171
std::string sError;
7272
if (sResult.size() > 0) {
7373
sResult += ",";

src/tests/test_is_validator_base64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int main() {
2424
tests.push_back(local("+/11", true));
2525
tests.push_back(local("%$", false));
2626

27-
WsjcppValidatorBase64 *pValidator = new WsjcppValidatorBase64();
27+
wsjcpp::WsjcppValidatorBase64 *pValidator = new wsjcpp::WsjcppValidatorBase64();
2828

2929
for (int i = 0; i < tests.size(); i++) {
3030
local t = tests[i];

src/tests/test_is_validator_date.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int main() {
2929
tests.push_back(local("1898-15-01", false));
3030
tests.push_back(local("1898-01-32", false));
3131

32-
WsjcppValidatorDate *pValidator = new WsjcppValidatorDate();
32+
wsjcpp::WsjcppValidatorDate *pValidator = new wsjcpp::WsjcppValidatorDate();
3333

3434
for (int i = 0; i < tests.size(); i++) {
3535
local t = tests[i];

src/tests/test_is_validator_datetime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int main() {
2727
tests.push_back(local("18a8-01-01T00:00:00", false));
2828
tests.push_back(local("1838-01-01T25:00:00", false));
2929

30-
WsjcppValidatorDateTime *pValidator = new WsjcppValidatorDateTime();
30+
wsjcpp::WsjcppValidatorDateTime *pValidator = new wsjcpp::WsjcppValidatorDateTime();
3131

3232
for (int i = 0; i < tests.size(); i++) {
3333
local t = tests[i];

src/tests/test_is_validator_email.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int main() {
2424
tests.push_back(local("s_s-some@test.com", true));
2525
tests.push_back(local("s_s-some@test.c", false));
2626

27-
WsjcppValidatorEmail *pValidator = new WsjcppValidatorEmail();
27+
wsjcpp::WsjcppValidatorEmail *pValidator = new wsjcpp::WsjcppValidatorEmail();
2828

2929
for (int i = 0; i < tests.size(); i++) {
3030
local t = tests[i];

src/tests/test_is_validator_hex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ int main() {
2222
tests.push_back(local("abcdef0123456789ABCDEF", true));
2323
tests.push_back(local("0123J", false));
2424

25-
WsjcppValidatorHex *pValidator = new WsjcppValidatorHex();
25+
wsjcpp::WsjcppValidatorHex *pValidator = new wsjcpp::WsjcppValidatorHex();
2626

2727
for (int i = 0; i < tests.size(); i++) {
2828
local t = tests[i];

0 commit comments

Comments
 (0)