mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 08:46:19 +08:00
Added regex pattern validators
This commit is contained in:
+3
-1
@@ -4,12 +4,13 @@
|
||||
#include "rfl/AllOf.hpp"
|
||||
#include "rfl/AnyOf.hpp"
|
||||
#include "rfl/Box.hpp"
|
||||
#include "rfl/ConStr.hpp"
|
||||
#include "rfl/Field.hpp"
|
||||
#include "rfl/Flatten.hpp"
|
||||
#include "rfl/Literal.hpp"
|
||||
#include "rfl/NamedTuple.hpp"
|
||||
#include "rfl/OneOf.hpp"
|
||||
#include "rfl/Pattern.hpp"
|
||||
#include "rfl/PatternValidator.hpp"
|
||||
#include "rfl/Ref.hpp"
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "rfl/Timestamp.hpp"
|
||||
@@ -30,6 +31,7 @@
|
||||
#include "rfl/name_t.hpp"
|
||||
#include "rfl/named_tuple_t.hpp"
|
||||
#include "rfl/parsing/CustomParser.hpp"
|
||||
#include "rfl/patterns.hpp"
|
||||
#include "rfl/remove_fields.hpp"
|
||||
#include "rfl/replace.hpp"
|
||||
#include "rfl/to_named_tuple.hpp"
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef RFL_PATTERN_HPP_
|
||||
#define RFL_PATTERN_HPP_
|
||||
|
||||
#include "rfl/PatternValidator.hpp"
|
||||
#include "rfl/Validator.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <internal::StringLiteral _regex, internal::StringLiteral _name>
|
||||
using Pattern = Validator<std::string, PatternValidator<_regex, _name>>;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,52 @@
|
||||
#ifndef RFL_PATTERNVALIDATOR_HPP_
|
||||
#define RFL_PATTERNVALIDATOR_HPP_
|
||||
|
||||
#include <ctime>
|
||||
#include <iterator>
|
||||
#include <optional>
|
||||
#include <regex>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include "internal/StringLiteral.hpp"
|
||||
#include "rfl/Literal.hpp"
|
||||
#include "rfl/Result.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <internal::StringLiteral _regex, internal::StringLiteral _name>
|
||||
struct PatternValidator {
|
||||
static Result<std::string> validate(const std::string& _str) noexcept {
|
||||
const auto check_pattern =
|
||||
[&](const auto& _regex_pattern) -> rfl::Result<std::string> {
|
||||
if (!std::regex_match(_str, _regex_pattern)) {
|
||||
return rfl::Error("String '" + _str + "' did not match format '" +
|
||||
_name.str() + "': '" + _regex.str() + "'.");
|
||||
}
|
||||
return _str;
|
||||
};
|
||||
|
||||
return regex_pattern().and_then(check_pattern);
|
||||
}
|
||||
|
||||
private:
|
||||
/// Uses a memoization pattern to init the pattern.
|
||||
static rfl::Result<std::regex> regex_pattern() noexcept {
|
||||
if (!regex_pattern_) {
|
||||
try {
|
||||
regex_pattern_ = std::regex(_regex.str());
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
}
|
||||
return *regex_pattern_;
|
||||
}
|
||||
|
||||
private:
|
||||
/// The underlying regex pattern.
|
||||
static inline std::optional<std::regex> regex_pattern_;
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -21,6 +21,15 @@ struct Validator {
|
||||
|
||||
Validator(const T& _value) : value_(V::validate(_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()) {}
|
||||
|
||||
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()) {}
|
||||
|
||||
~Validator() = default;
|
||||
|
||||
/// Exposes the underlying value.
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
#ifndef RFL_PATTERNS_HPP_
|
||||
#define RFL_PATTERNS_HPP_
|
||||
|
||||
#include "rfl/Pattern.hpp"
|
||||
#include "rfl/Validator.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
// This pattern will match valid Base64 encoded strings with or without padding.
|
||||
using Base64Encoded = Pattern<R"(^[A-Za-z0-9+/]*(={0,2})?$)", "Base64Encoded">;
|
||||
|
||||
/// email
|
||||
using Email =
|
||||
Pattern<R"(^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$)", "Email">;
|
||||
|
||||
using UUIDv1 = Pattern<
|
||||
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}$)",
|
||||
"UUIDv1">;
|
||||
|
||||
using UUIDv2 = Pattern<
|
||||
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}$)",
|
||||
"UUIDv2">;
|
||||
|
||||
using UUIDv3 = Pattern<
|
||||
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}$)",
|
||||
"UUIDv3">;
|
||||
|
||||
using UUIDv4 = Pattern<
|
||||
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}$)",
|
||||
"UUIDv4">;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_email() {
|
||||
std::cout << "test_email" << std::endl;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"email", rfl::Email> email;
|
||||
};
|
||||
|
||||
const auto homer = Person{.first_name = "Homer",
|
||||
.last_name = "Simpson",
|
||||
.email = "homer@simpson.com"};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","email":"homer@simpson.com"})");
|
||||
}
|
||||
+2
-1
@@ -34,6 +34,7 @@
|
||||
#include "test_unique_ptr.hpp"
|
||||
#include "test_unique_ptr2.hpp"
|
||||
//#include "test_constr.hpp"
|
||||
#include "test_email.hpp"
|
||||
#include "test_one_of.hpp"
|
||||
#include "test_unordered_map.hpp"
|
||||
#include "test_unordered_multimap.hpp"
|
||||
@@ -87,7 +88,7 @@ int main() {
|
||||
test_all_of();
|
||||
test_any_of();
|
||||
test_one_of();
|
||||
// test_constr();
|
||||
test_email();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user