mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 18:56:19 +08:00
Allow several conditions in the Validator, which will then be connected by AllOf
This commit is contained in:
@@ -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<int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
using Age = rfl::Validator<int, rfl::Minimum<0>, rfl::Maximum<130>>;
|
||||
|
||||
// "firstName", "lastName" and "children" are the field names
|
||||
// as they will appear in the JSON. The C++ standard is
|
||||
|
||||
+32
-26
@@ -11,69 +11,75 @@
|
||||
|
||||
#include "internal/HasValidation.hpp"
|
||||
#include "internal/StringLiteral.hpp"
|
||||
#include "rfl/AllOf.hpp"
|
||||
#include "rfl/Result.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class T, class V>
|
||||
requires internal::HasValidation<V, T>
|
||||
template <class T, class V, class... Vs>
|
||||
requires internal::HasValidation<AllOf<V, Vs...>, T>
|
||||
struct Validator {
|
||||
private:
|
||||
using AllOfVs = std::conditional_t<sizeof...(Vs) == 0, V, AllOf<V, Vs...>>;
|
||||
|
||||
public:
|
||||
using ReflectionType = T;
|
||||
|
||||
/// Exception-free validation.
|
||||
static Result<Validator<T, V>> from_value(const T& _value) noexcept {
|
||||
static Result<Validator<T, V, Vs...>> from_value(const T& _value) noexcept {
|
||||
try {
|
||||
return Validator<T, V>(_value);
|
||||
return Validator<T, V, Vs...>(_value);
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
Validator() : value_(V::validate(T()).value()) {}
|
||||
Validator() : value_(AllOfVs::validate(T()).value()) {}
|
||||
|
||||
Validator(Validator<T, V>&& _other) noexcept = default;
|
||||
Validator(Validator<T, V, Vs...>&& _other) noexcept = default;
|
||||
|
||||
Validator(const Validator<T, V>& _other) = default;
|
||||
Validator(const Validator<T, V, Vs...>& _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 <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
Validator(U&& _value)
|
||||
: value_(V::validate(T(std::forward<U>(_value))).value()) {}
|
||||
: value_(AllOfVs::validate(T(std::forward<U>(_value))).value()) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
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<T>(_value)).value();
|
||||
value_ = AllOfVs::validate(std::forward<T>(_value)).value();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
Validator<T, V>& operator=(const Validator<T, V>& _other) = default;
|
||||
Validator<T, V, Vs...>& operator=(const Validator<T, V, Vs...>& _other) =
|
||||
default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
Validator<T, V>& operator=(Validator<T, V>&& _other) noexcept = default;
|
||||
Validator<T, V, Vs...>& operator=(Validator<T, V, Vs...>&& _other) noexcept =
|
||||
default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
auto& operator=(U&& _value) noexcept {
|
||||
value_ = V::validate(T(std::forward<U>(_value))).value();
|
||||
value_ = AllOfVs::validate(T(std::forward<U>(_value))).value();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -81,12 +87,12 @@ struct Validator {
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
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<T, V>& _other) const {
|
||||
bool operator==(const Validator<T, V, Vs...>& _other) const {
|
||||
return value() == _other.value();
|
||||
}
|
||||
|
||||
@@ -104,14 +110,14 @@ struct Validator {
|
||||
T value_;
|
||||
};
|
||||
|
||||
template <class T, class V>
|
||||
inline auto operator<=>(const Validator<T, V>& _v1,
|
||||
const Validator<T, V>& _v2) {
|
||||
template <class T, class V, class... Vs>
|
||||
inline auto operator<=>(const Validator<T, V, Vs...>& _v1,
|
||||
const Validator<T, V, Vs...>& _v2) {
|
||||
return _v1.value() <=> _v2.value();
|
||||
}
|
||||
|
||||
template <class T, class V>
|
||||
inline auto operator<=>(const Validator<T, V>& _v, const T& _t) {
|
||||
template <class T, class V, class... Vs>
|
||||
inline auto operator<=>(const Validator<T, V, Vs...>& _v, const T& _t) {
|
||||
return _v.value() <=> _t;
|
||||
}
|
||||
|
||||
@@ -119,9 +125,9 @@ inline auto operator<=>(const Validator<T, V>& _v, const T& _t) {
|
||||
|
||||
namespace std {
|
||||
|
||||
template <class T, class V>
|
||||
struct hash<rfl::Validator<T, V>> {
|
||||
size_t operator()(const rfl::Validator<T, V>& _v) const {
|
||||
template <class T, class V, class... Vs>
|
||||
struct hash<rfl::Validator<T, V, Vs...>> {
|
||||
size_t operator()(const rfl::Validator<T, V, Vs...>& _v) const {
|
||||
return hash<T>()(_v.value());
|
||||
}
|
||||
};
|
||||
|
||||
@@ -9,8 +9,7 @@
|
||||
void test_all_of() {
|
||||
std::cout << "test_all_of" << std::endl;
|
||||
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
|
||||
@@ -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<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;
|
||||
|
||||
// "firstName", "lastName" and "children" are the field names
|
||||
// as they will appear in the JSON. The C++ standard is
|
||||
|
||||
Reference in New Issue
Block a user