diff --git a/README.md b/README.md index 83520fbc..e5bb7b53 100644 --- a/README.md +++ b/README.md @@ -47,8 +47,7 @@ For a more in-depth theoretical discussions of these topics, the following books // Age must be a plausible number, between 0 and 130. This will // be validated automatically. -using Age = rfl::Validator, rfl::Maximum<130>>>; +using Age = rfl::Validator, rfl::Maximum<130>>; // "firstName", "lastName" and "children" are the field names // as they will appear in the JSON. The C++ standard is diff --git a/include/rfl/Validator.hpp b/include/rfl/Validator.hpp index f0044088..64fc69c9 100644 --- a/include/rfl/Validator.hpp +++ b/include/rfl/Validator.hpp @@ -11,69 +11,75 @@ #include "internal/HasValidation.hpp" #include "internal/StringLiteral.hpp" +#include "rfl/AllOf.hpp" #include "rfl/Result.hpp" namespace rfl { -template -requires internal::HasValidation +template +requires internal::HasValidation, T> struct Validator { + private: + using AllOfVs = std::conditional_t>; + public: using ReflectionType = T; /// Exception-free validation. - static Result> from_value(const T& _value) noexcept { + static Result> from_value(const T& _value) noexcept { try { - return Validator(_value); + return Validator(_value); } catch (std::exception& e) { return Error(e.what()); } } - Validator() : value_(V::validate(T()).value()) {} + Validator() : value_(AllOfVs::validate(T()).value()) {} - Validator(Validator&& _other) noexcept = default; + Validator(Validator&& _other) noexcept = default; - Validator(const Validator& _other) = default; + Validator(const Validator& _other) = default; - Validator(T&& _value) : value_(V::validate(_value).value()) {} + Validator(T&& _value) : value_(AllOfVs::validate(_value).value()) {} - Validator(const T& _value) : value_(V::validate(_value).value()) {} + Validator(const T& _value) : value_(AllOfVs::validate(_value).value()) {} template , bool>::type = true> Validator(U&& _value) - : value_(V::validate(T(std::forward(_value))).value()) {} + : value_(AllOfVs::validate(T(std::forward(_value))).value()) {} template , bool>::type = true> - Validator(const U& _value) : value_(V::validate(T(_value)).value()) {} + Validator(const U& _value) : value_(AllOfVs::validate(T(_value)).value()) {} ~Validator() = default; /// Assigns the underlying object. auto& operator=(const T& _value) { - value_ = V::validate(_value).value(); + value_ = AllOfVs::validate(_value).value(); return *this; } /// Assigns the underlying object. auto& operator=(T&& _value) { - value_ = V::validate(std::forward(_value)).value(); + value_ = AllOfVs::validate(std::forward(_value)).value(); return *this; } /// Assigns the underlying object. - Validator& operator=(const Validator& _other) = default; + Validator& operator=(const Validator& _other) = + default; /// Assigns the underlying object. - Validator& operator=(Validator&& _other) noexcept = default; + Validator& operator=(Validator&& _other) noexcept = + default; /// Assigns the underlying object. template , bool>::type = true> auto& operator=(U&& _value) noexcept { - value_ = V::validate(T(std::forward(_value))).value(); + value_ = AllOfVs::validate(T(std::forward(_value))).value(); return *this; } @@ -81,12 +87,12 @@ struct Validator { template , bool>::type = true> auto& operator=(const U& _value) { - value_ = V::validate(T(_value)).value(); + value_ = AllOfVs::validate(T(_value)).value(); return *this; } /// Equality operator other Validators. - bool operator==(const Validator& _other) const { + bool operator==(const Validator& _other) const { return value() == _other.value(); } @@ -104,14 +110,14 @@ struct Validator { T value_; }; -template -inline auto operator<=>(const Validator& _v1, - const Validator& _v2) { +template +inline auto operator<=>(const Validator& _v1, + const Validator& _v2) { return _v1.value() <=> _v2.value(); } -template -inline auto operator<=>(const Validator& _v, const T& _t) { +template +inline auto operator<=>(const Validator& _v, const T& _t) { return _v.value() <=> _t; } @@ -119,9 +125,9 @@ inline auto operator<=>(const Validator& _v, const T& _t) { namespace std { -template -struct hash> { - size_t operator()(const rfl::Validator& _v) const { +template +struct hash> { + size_t operator()(const rfl::Validator& _v) const { return hash()(_v.value()); } }; diff --git a/tests/json/test_all_of.hpp b/tests/json/test_all_of.hpp index a29dded7..c9c23f2d 100644 --- a/tests/json/test_all_of.hpp +++ b/tests/json/test_all_of.hpp @@ -9,8 +9,7 @@ void test_all_of() { std::cout << "test_all_of" << std::endl; - using Age = rfl::Validator, rfl::Maximum<130>>>; + using Age = rfl::Validator, rfl::Maximum<130>>; struct Person { rfl::Field<"firstName", std::string> first_name; diff --git a/tests/json/test_readme_example.hpp b/tests/json/test_readme_example.hpp index 381b1c60..72228800 100644 --- a/tests/json/test_readme_example.hpp +++ b/tests/json/test_readme_example.hpp @@ -13,8 +13,7 @@ void test_readme_example() { // Age must be a plausible number, between 0 and 130. This will // be validated automatically. - using Age = rfl::Validator, rfl::Maximum<130>>>; + using Age = rfl::Validator, rfl::Maximum<130>>; // "firstName", "lastName" and "children" are the field names // as they will appear in the JSON. The C++ standard is