diff --git a/services/lib/src/GlobalTools.cpp b/services/lib/src/GlobalTools.cpp index 6c7cbd2e2..0a01a14c4 100644 --- a/services/lib/src/GlobalTools.cpp +++ b/services/lib/src/GlobalTools.cpp @@ -1,48 +1,62 @@ #include "GlobalTools.h" +#include #include #include #include #include #include #include +#include #include namespace comm { namespace network { namespace tools { uint64_t getCurrentTimestamp() { using namespace std::chrono; return duration_cast(system_clock::now().time_since_epoch()) .count(); } bool hasEnvFlag(const std::string &flag) { if (std::getenv(flag.c_str()) == nullptr) { return false; } return std::string(std::getenv(flag.c_str())) == "1"; } std::string decorateTableName(const std::string &baseName) { std::string suffix = ""; if (hasEnvFlag("COMM_TEST_SERVICES")) { suffix = "-test"; } return baseName + suffix; } bool isSandbox() { return hasEnvFlag("COMM_SERVICES_SANDBOX"); } std::string generateUUID() { thread_local boost::uuids::random_generator random_generator; return boost::uuids::to_string(random_generator()); } +bool validateUUIDv4(const std::string &uuid) { + const std::regex uuidV4RegexFormat( + "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"); + try { + return std::regex_match(uuid, uuidV4RegexFormat); + } catch (const std::exception &e) { + LOG(ERROR) << "Tools: " + << "Got an exception at `validateUUID`: " << e.what(); + return false; + } +} + } // namespace tools } // namespace network } // namespace comm diff --git a/services/lib/src/GlobalTools.h b/services/lib/src/GlobalTools.h index ecf6b8263..ddb4f7ac8 100644 --- a/services/lib/src/GlobalTools.h +++ b/services/lib/src/GlobalTools.h @@ -1,24 +1,26 @@ #pragma once #include #include namespace comm { namespace network { namespace tools { const std::string ID_SEPARATOR = ":"; uint64_t getCurrentTimestamp(); bool hasEnvFlag(const std::string &flag); std::string decorateTableName(const std::string &baseName); bool isSandbox(); std::string generateUUID(); +bool validateUUIDv4(const std::string &uuid); + } // namespace tools } // namespace network } // namespace comm