@@ -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
0 commit comments