diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..90cfebd6 --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +BasedOnStyle : Google diff --git a/include/rfl.hpp b/include/rfl.hpp index 5b7ae588..a3ee0210 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -30,5 +30,7 @@ #include "rfl/replace.hpp" #include "rfl/to_named_tuple.hpp" #include "rfl/visit.hpp" +#include "rfl/ConStr.hpp" +#include "rfl/Validator.hpp" #endif diff --git a/include/rfl/ConStr.hpp b/include/rfl/ConStr.hpp new file mode 100644 index 00000000..030fd824 --- /dev/null +++ b/include/rfl/ConStr.hpp @@ -0,0 +1,92 @@ +#ifndef RFL_UUID_HPP_ +#define RFL_UUID_HPP_ + +#include +#include +#include +#include +#include + +#include "internal/StringLiteral.hpp" +#include "rfl/Literal.hpp" +#include "rfl/Result.hpp" + +namespace rfl { + +template +class ConStr { + + constexpr static const internal::StringLiteral pattern_ = _pattern; + +public: + using ReflectionType = std::string; + ConStr(const char *_str) { + auto unvalidated_str = std::string(_str); + if constexpr (pattern_ != internal::StringLiteral{""}) { + std::regex regex_pattern(pattern_.str()); + if (!std::regex_match(unvalidated_str, regex_pattern)) { + throw std::runtime_error("String '" + unvalidated_str + + "' did not match format " + pattern_.str() + + " format"); + } + } + if constexpr (_min >= 0) { + if (unvalidated_str.length() < _min) { + throw std::runtime_error("String '" + unvalidated_str + + "' is shorter than min length" + + std::to_string(_min)); + } + } + if constexpr (_max >= 0) { + if (unvalidated_str.length() > _max) { + throw std::runtime_error("String '" + unvalidated_str + + "' is longer than max length" + + std::to_string(_max)); + } + } + value_ = _str; + } + + ConStr(const std::string &_str) : ConStr(_str.c_str()){}; + ~ConStr() = default; + + std::string value_; + + ReflectionType reflection() const { return value_; } + + /// Returns a result containing the ConStr when successful or an Error + /// otherwise. + static Result from_string(const char *_str) noexcept { + try { + return ConStr(_str); + } catch (std::exception &e) { + return Error(e.what()); + } + } + + /// Returns a result containing the ConStr when successful or an Error + /// otherwise. + static Result from_string(const std::string &_str) { + return from_string(_str.c_str()); + } +}; +/// UUID +using UUIDv1 = ConStr< + R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-1[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)">; +using UUIDv2 = ConStr< + R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-2[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)">; +using UUIDv3 = ConStr< + R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-3[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)">; +using UUIDv4 = ConStr< + R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)">; + +// This pattern will match valid Base64 encoded strings with or without padding. +using Base64Encoded = ConStr; + +/// email +using Email = ConStr; + +} // namespace rfl + +#endif diff --git a/include/rfl/Validator.hpp b/include/rfl/Validator.hpp new file mode 100644 index 00000000..0a656b0e --- /dev/null +++ b/include/rfl/Validator.hpp @@ -0,0 +1,118 @@ +#ifndef RFL_VALIDATOR_HPP_ +#define RFL_VALIDATOR_HPP_ + +#include +#include +#include +#include +#include + +#include "internal/StringLiteral.hpp" +#include "rfl/Result.hpp" + +namespace rfl { + +template +concept HasValidation = requires(Class obj, Type value) { + { Class::validate(value) } -> std::same_as>; +}; + +template +concept HasDescription = requires(_Type obj) { + { obj.description } -> std::same_as>; +}; + + +template +requires HasValidation +struct Annotate { + public: + using ReflectionType = _Type; + ReflectionType value; + + template , bool> = true > + Annotate(const T& _value) : value(_value) { + std::optional validate_check = Class::validate(value); + if (validate_check.has_value()) { + throw std::runtime_error(validate_check.value().what()); + } + }; + + template , bool> = true > + Annotate(T&& _value) : value(std::forward(_value)) { + std::optional validate_check = Class::validate(value); + if (validate_check.has_value()) { + throw std::runtime_error(validate_check.value().what()); + } + } + + Annotate(ReflectionType _value) : value(_value) { + std::optional validate_check = Class::validate(value); + if (validate_check.has_value()) { + throw std::runtime_error(validate_check.value().what()); + } + } + +}; + +template +std::string to_string(const T& _value) { + std::ostringstream oss; + oss << _value; + return oss.str(); +} + +template +concept HasSize = requires(T obj) { + { obj.size() } -> std::same_as; +}; + +template +concept HasIntLength = HasSize || std::is_integral_v; + +template +struct Length { + static constexpr const auto min_length = _min_length; + static constexpr const auto max_length = _max_length; + + template + static std::optional validate(T _value) { + uint16_t size; + if constexpr (HasSize) { + size = _value.size(); + } else if (std::is_integral_v) { + size = _value; + } + if (size < _min_length) { + return rfl::Error("'" + to_string(_value) + + "' is less than the min length requirement of '" + + std::to_string(_min_length) + "'"); + } + if (size > _max_length) { + return rfl::Error("'" + to_string(_value) + + "' exceeds max length requirement of '" + + std::to_string(_max_length) + "'"); + } + return std::nullopt; + } +}; + +template +struct Pattern { + static constexpr const auto pattern = _pattern; + + static std::optional validate(std::string _value) { + std::regex regex_pattern(_pattern.str()); + if (!std::regex_match(_value, regex_pattern)) { + return rfl::Error("String '" + _value + "' did not match format '" + + _pattern.str() + "'"); + } + return std::nullopt; + } +}; + +} // namespace rfl +// +#endif diff --git a/tests/test_constr.hpp b/tests/test_constr.hpp new file mode 100644 index 00000000..0fd41da4 --- /dev/null +++ b/tests/test_constr.hpp @@ -0,0 +1,58 @@ +#include +#include + +#include "write_and_read.hpp" +void test_constr() { + std::cout << "test_constr" << std::endl; + + // Custom validation for string + // Masked credit cards + using MaskedCreditCardType = rfl::Annotate>; + //// Nick name between 5 and 10 characters long without regex use + using NickNameType = rfl::Annotate>; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + //rfl::Field<"id", rfl::UUIDv4> id; + //rfl::Field<"payload", rfl::Base64Encoded> payload; + rfl::Field<"maskedCreditCard", MaskedCreditCardType> masked_credit_card; + rfl::Field<"nickName", NickNameType> nick_name; + }; + + try { + const auto wrong_bart = Person{ + .first_name = "Bart", + //.id = "de76b905-5b32-4127-96d1-48e5321e11a6", + //.payload = "aGVsbG93b3JsZAo=", + .masked_credit_card = "XXXX-XXXX-XXXX-444A", + .nick_name = "Cosmo", + }; + } catch (const std::exception &e) { + std::cout << e.what() << std::endl; + //assert( + // e.what() == + // std::string( + // R"(String 'XXXX-XXXX-XXXX-444A' did not match format 'XXXX-XXXX-XXXX-\d{4}')")); + } + const auto correct_bart = Person{ + .first_name = "Bart", + //.id = "de76b905-5b32-4127-96d1-48e5321e11a6", + //.payload = "aGVsbG93b3JsZAo=", + .masked_credit_card = "XXXX-XXXX-XXXX-4440", + .nick_name = "Cosmo", + }; + + write_and_read(correct_bart, + "{\"firstName\":\"Bart\",\"lastName\":\"Simpson\"," + "\"id\":\"de76b905-5b32-4127-96d1-48e5321e11a6\"," + "\"payload\":\"aGVsbG93b3JsZAo=\"," + "\"maskedCreditCard\":\"XXXX-XXXX-XXXX-4440\"," + "\"nickName\":\"Cosmo\"}"); + + //struct Blub { + // rfl::Field<"firstName", + // rfl::Validate>; + //}; + // +} diff --git a/tests/tests.cpp b/tests/tests.cpp index 00b68559..904eda9c 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -32,10 +32,11 @@ #include "test_timestamp.hpp" #include "test_unique_ptr.hpp" #include "test_unique_ptr2.hpp" +#include "test_constr.hpp" #include "test_unordered_map.hpp" +#include "test_unordered_set.hpp" #include "test_unordered_multimap.hpp" #include "test_unordered_multiset.hpp" -#include "test_unordered_set.hpp" #include "test_variant.hpp" int main() { @@ -79,6 +80,7 @@ int main() { test_replace_with_other_struct(); test_as(); test_as_flatten(); + test_constr(); test_custom_constructor();