diff --git a/include/rfl/Validator.hpp b/include/rfl/Validator.hpp index e7813276..50a96b07 100644 --- a/include/rfl/Validator.hpp +++ b/include/rfl/Validator.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "internal/HasValidation.hpp" #include "internal/StringLiteral.hpp" @@ -34,8 +35,15 @@ struct Validator { Validator(const Validator& _other) = default; + Validator(T&& _value) : value_(V::validate(_value).value()) {} + Validator(const T& _value) : value_(V::validate(_value).value()) {} + template , + bool>::type = true> + Validator(U&& _value) + : value_(V::validate(T(std::forward(_value))).value()) {} + template , bool>::type = true> Validator(const U& _value) : value_(V::validate(T(_value)).value()) {} @@ -50,7 +58,7 @@ struct Validator { /// Assigns the underlying object. auto& operator=(T&& _value) { - value_ = V::validate(_value).value(); + value_ = V::validate(std::forward(_value)).value(); return *this; } @@ -58,13 +66,13 @@ struct Validator { Validator& operator=(const Validator& _other) = default; /// Assigns the underlying object. - Validator& operator=(Validator&& _other) = default; + Validator& operator=(Validator&& _other) noexcept = default; /// Assigns the underlying object. template , bool>::type = true> - auto& operator=(U&& _value) { - value_ = V::validate(T(std::forward(_value))).value(); + auto& operator=(U&& _value) noexcept { + value_ = V::validate(T(std::forward(_value))).value(); return *this; } diff --git a/tests/test_anonymous_fields.hpp b/tests/test_anonymous_fields.hpp index e331c620..bb480e53 100644 --- a/tests/test_anonymous_fields.hpp +++ b/tests/test_anonymous_fields.hpp @@ -16,8 +16,8 @@ void test_anonymous_fields() { std::string first_name; std::string last_name; rfl::Timestamp<"%Y-%m-%d"> birthday; - unsigned int age; - std::string email; + Age age; + rfl::Email email; std::vector children; }; @@ -25,8 +25,7 @@ void test_anonymous_fields() { .last_name = "Simpson", .birthday = "1987-04-19", .age = 10, - .email = "bart@simpson.com", - .children = std::vector()}; + .email = "bart@simpson.com"}; const auto lisa = Person{.first_name = "Lisa", .last_name = "Simpson",