Added size validation

This commit is contained in:
Patrick Urbanke
2023-11-02 22:53:58 +01:00
parent a2a6609466
commit fcaf2e8e23
7 changed files with 83 additions and 146 deletions
+1
View File
@@ -12,6 +12,7 @@
#include "rfl/Pattern.hpp"
#include "rfl/PatternValidator.hpp"
#include "rfl/Ref.hpp"
#include "rfl/Size.hpp"
#include "rfl/TaggedUnion.hpp"
#include "rfl/Timestamp.hpp"
#include "rfl/Validator.hpp"
-92
View File
@@ -1,92 +0,0 @@
#ifndef RFL_UUID_HPP_
#define RFL_UUID_HPP_
#include <ctime>
#include <iterator>
#include <regex>
#include <stdexcept>
#include <string>
#include "internal/StringLiteral.hpp"
#include "rfl/Literal.hpp"
#include "rfl/Result.hpp"
namespace rfl {
template <internal::StringLiteral _pattern = "", int8_t _min = -1,
int8_t _max = -1>
class ConStr {
constexpr static const internal::StringLiteral pattern_ = _pattern;
public:
using ReflectionType = std::string;
ConStr(const char *_str) {
auto unvalidated_str = std::string(_str);
if constexpr (pattern_ != internal::StringLiteral{""}) {
std::regex regex_pattern(pattern_.str());
if (!std::regex_match(unvalidated_str, regex_pattern)) {
throw std::runtime_error("String '" + unvalidated_str +
"' did not match format " + pattern_.str() +
" format");
}
}
if constexpr (_min >= 0) {
if (unvalidated_str.length() < _min) {
throw std::runtime_error("String '" + unvalidated_str +
"' is shorter than min length" +
std::to_string(_min));
}
}
if constexpr (_max >= 0) {
if (unvalidated_str.length() > _max) {
throw std::runtime_error("String '" + unvalidated_str +
"' is longer than max length" +
std::to_string(_max));
}
}
value_ = _str;
}
ConStr(const std::string &_str) : ConStr(_str.c_str()){};
~ConStr() = default;
std::string value_;
ReflectionType reflection() const { return value_; }
/// Returns a result containing the ConStr when successful or an Error
/// otherwise.
static Result<ConStr> from_string(const char *_str) noexcept {
try {
return ConStr(_str);
} catch (std::exception &e) {
return Error(e.what());
}
}
/// Returns a result containing the ConStr when successful or an Error
/// otherwise.
static Result<ConStr> from_string(const std::string &_str) {
return from_string(_str.c_str());
}
};
/// UUID
using UUIDv1 = ConStr<
R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-1[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)">;
using UUIDv2 = ConStr<
R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-2[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)">;
using UUIDv3 = ConStr<
R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-3[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)">;
using UUIDv4 = ConStr<
R"(^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$)">;
// This pattern will match valid Base64 encoded strings with or without padding.
using Base64Encoded = ConStr<R"(^[A-Za-z0-9+/]*(={0,2})?$)">;
/// email
using Email = ConStr<R"(^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$)">;
} // namespace rfl
#endif
+22
View File
@@ -0,0 +1,22 @@
#ifndef RFL_SIZE_HPP_
#define RFL_SIZE_HPP_
#include "rfl/Result.hpp"
namespace rfl {
template <class V>
struct Size {
template <class T>
static rfl::Result<T> validate(const T& _t) {
const auto to_t = [&](const auto& _v) { return _t; };
const auto embellish_error = [](const auto& _err) {
return Error("Size validation failed: " + _err.what());
};
return V::validate(_t.size()).transform(to_t).or_else(embellish_error);
}
};
} // namespace rfl
#endif
+2 -49
View File
@@ -19,6 +19,8 @@ struct Validator {
public:
using ReflectionType = T;
Validator() : value_(V::validate(T()).value()) {}
Validator(const T& _value) : value_(V::validate(_value).value()) {}
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
@@ -45,55 +47,6 @@ struct Validator {
/// The underlying value.
T value_;
};
/*
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
+3 -2
View File
@@ -6,10 +6,11 @@
namespace rfl {
// This pattern will match valid Base64 encoded strings with or without padding.
/// This pattern will match valid Base64 encoded strings with or without
/// padding.
using Base64Encoded = Pattern<R"(^[A-Za-z0-9+/]*(={0,2})?$)", "Base64Encoded">;
/// email
/// This pattern will match email addresses.
using Email =
Pattern<R"(^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$)", "Email">;