mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 00:26:19 +08:00
adapted test.
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
#ifndef RFL_VALIDATOR_HPP_
|
||||
#define RFL_VALIDATOR_HPP_
|
||||
|
||||
#include <concepts>
|
||||
#include <optional>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "internal/StringLiteral.hpp"
|
||||
#include "rfl/Result.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class Class, typename Type>
|
||||
concept HasValidation = requires(Class obj, Type value) {
|
||||
{ Class::validate(value) } -> std::same_as<std::optional<rfl::Error>>;
|
||||
};
|
||||
|
||||
template <typename _Type>
|
||||
concept HasDescription = requires(_Type obj) {
|
||||
{ obj.description } -> std::same_as<std::optional<rfl::Error>>;
|
||||
};
|
||||
|
||||
|
||||
template <typename _Type, class Class>
|
||||
requires HasValidation<Class, _Type>
|
||||
struct Annotate {
|
||||
public:
|
||||
using ReflectionType = _Type;
|
||||
ReflectionType value;
|
||||
|
||||
template <class T,
|
||||
std::enable_if_t<std::is_convertible_v<T, _Type>, bool> = true >
|
||||
Annotate(const T& _value) : value(_value) {
|
||||
std::optional<rfl::Error> validate_check = Class::validate(value);
|
||||
if (validate_check.has_value()) {
|
||||
throw std::runtime_error(validate_check.value().what());
|
||||
}
|
||||
};
|
||||
|
||||
template <class T,
|
||||
std::enable_if_t<std::is_convertible_v<T, _Type>, bool> = true >
|
||||
Annotate(T&& _value) : value(std::forward<T>(_value)) {
|
||||
std::optional<rfl::Error> 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<rfl::Error> validate_check = Class::validate(value);
|
||||
if (validate_check.has_value()) {
|
||||
throw std::runtime_error(validate_check.value().what());
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
std::string to_string(const T& _value) {
|
||||
std::ostringstream oss;
|
||||
oss << _value;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
concept HasSize = requires(T obj) {
|
||||
{ obj.size() } -> std::same_as<size_t>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
concept HasIntLength = HasSize<T> || std::is_integral_v<T>;
|
||||
|
||||
template <size_t _min_length, size_t _max_length>
|
||||
struct Length {
|
||||
static constexpr const auto min_length = _min_length;
|
||||
static constexpr const auto max_length = _max_length;
|
||||
|
||||
template <HasIntLength T>
|
||||
static std::optional<rfl::Error> validate(T _value) {
|
||||
uint16_t size;
|
||||
if constexpr (HasSize<T>) {
|
||||
size = _value.size();
|
||||
} else if (std::is_integral_v<T>) {
|
||||
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 <internal::StringLiteral _pattern>
|
||||
struct Pattern {
|
||||
static constexpr const auto pattern = _pattern;
|
||||
|
||||
static std::optional<rfl::Error> 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
|
||||
@@ -0,0 +1,58 @@
|
||||
#include <cassert>
|
||||
#include <rfl.hpp>
|
||||
|
||||
#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<std::string, rfl::Pattern<R"(XXXX-XXXX-XXXX-\d{4})">>;
|
||||
//// Nick name between 5 and 10 characters long without regex use
|
||||
using NickNameType = rfl::Annotate<std::string, rfl::Length<5, 10>>;
|
||||
|
||||
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<std::string, "R(XXXX-XXXX-XXXX-\d{4})">>;
|
||||
//};
|
||||
//
|
||||
}
|
||||
Reference in New Issue
Block a user