22 explicit operator bool()
const {
return is_valid; }
26template <
typename V,
typename T>
27concept ValidatorFor =
requires(
const V& validator,
const T& value) {
28 { validator(value) } -> std::same_as<ValidationResult>;
56 auto other_fn = other.fn_;
58 auto result = this_fn(value);
62 return other_fn(value);
70 auto other_fn = other.fn_;
72 auto result = this_fn(value);
76 return other_fn(value);
86 requires std::is_arithmetic_v<T>
90 if (value < min_value) {
91 return ValidationResult::Error(
"Value " + std::to_string(value) +
" is less than minimum " + std::to_string(min_value));
99 requires std::is_arithmetic_v<T>
103 if (value > max_value) {
104 return ValidationResult::Error(
"Value " + std::to_string(value) +
" exceeds maximum " + std::to_string(max_value));
112 requires std::is_arithmetic_v<T>
115 return Min(min_value).
And(
Max(max_value));
120 requires std::is_arithmetic_v<T>
124 if (value <= T { 0 }) {
133 requires std::is_arithmetic_v<T>
137 if (value < T { 0 }) {
149 return ValidationResult::Error(
"Value must not be empty");
159 if (value.size() > max_len) {
160 return ValidationResult::Error(
"String length " + std::to_string(value.size()) +
" exceeds maximum " + std::to_string(max_len));
170 if (value.size() < min_len) {
171 return ValidationResult::Error(
"String length " + std::to_string(value.size()) +
" is less than minimum " + std::to_string(min_len));
182 for (
const auto& allowed_value : allowed) {
183 if (value == allowed_value) {
192template <
typename T,
typename Pred>
193 requires std::predicate<Pred, const T&>
Type-erased validator that can hold any validation function.
Definition validator.h:33
Validator()
Creates an always-valid validator.
Definition validator.h:38
auto And(Validator< T > other) const -> Validator< T >
Combines this validator with another (both must pass).
Definition validator.h:53
auto Or(Validator< T > other) const -> Validator< T >
Combines this validator with another (either must pass).
Definition validator.h:67
auto operator()(const T &value) const -> ValidationResult
Validates a value.
Definition validator.h:50
std::function< ValidationResult(const T &)> validator_fn
Definition validator.h:35
Validator(validator_fn fn)
Creates a validator from a function.
Definition validator.h:44
Concept for validator types.
Definition validator.h:27
C++20 compile-time type-safe configuration library.
Definition conf.h:13
auto Positive() -> Validator< T >
Creates a validator that checks if a numeric value is positive.
Definition validator.h:121
auto Predicate(Pred pred, std::string error_message) -> Validator< T >
Creates a validator from a predicate function.
Definition validator.h:194
auto Max(T max_value) -> Validator< T >
Creates a validator that checks if a numeric value is at most max.
Definition validator.h:100
auto MaxLength(std::size_t max_len) -> Validator< std::string >
Creates a validator that checks if a string length is at most max.
Definition validator.h:156
auto NonNegative() -> Validator< T >
Creates a validator that checks if a numeric value is non-negative.
Definition validator.h:134
auto MinLength(std::size_t min_len) -> Validator< std::string >
Creates a validator that checks if a string length is at least min.
Definition validator.h:167
auto Range(T min_value, T max_value) -> Validator< T >
Creates a validator that checks if a numeric value is within [min, max].
Definition validator.h:113
auto AlwaysValid() -> Validator< T >
Creates an always-valid validator.
Definition validator.h:206
auto Min(T min_value) -> Validator< T >
Creates a validator that checks if a numeric value is at least min.
Definition validator.h:87
auto OneOf(std::vector< T > allowed_values) -> Validator< T >
Creates a validator that checks if a value is one of the allowed values.
Definition validator.h:179
auto NotEmpty() -> Validator< std::string >
Creates a validator that checks if a string is not empty.
Definition validator.h:145
Result of a validation operation.
Definition validator.h:12
static auto Ok() -> ValidationResult
Creates a successful validation result.
Definition validator.h:17
static auto Error(std::string message) -> ValidationResult
Creates a failed validation result with an error message.
Definition validator.h:20
std::string error_message
Definition validator.h:14
bool is_valid
Definition validator.h:13