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 671884bf..6066a156 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -29,5 +29,6 @@ #include "rfl/to_named_tuple.hpp" #include "rfl/visit.hpp" #include "rfl/ConStr.hpp" +#include "rfl/Validator.hpp" #endif diff --git a/include/rfl/Validator.hpp b/include/rfl/Validator.hpp new file mode 100644 index 00000000..cf1df9e4 --- /dev/null +++ b/include/rfl/Validator.hpp @@ -0,0 +1,113 @@ +#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 + // Annotate(T _value) : value(static_cast<_Type>(_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; + } +}; + +Annotate> a("hello"); +Annotate> b("hello"); +Annotate> c(2); +Annotate> d(2); +Annotate, Length<1,2>> e({1,2}); + +} // namespace rfl +// +#endif