23 explicit operator bool()
const {
return is_valid; }
27template <
typename V,
typename T>
28concept ValidatorFor =
requires(
const V& validator,
const T& value) {
29 { validator(value) } -> std::same_as<ValidationResult>;
57 auto other_fn = other.fn_;
59 auto result = this_fn(value);
63 return other_fn(value);
71 auto other_fn = other.fn_;
73 auto result = this_fn(value);
77 return other_fn(value);
87 requires std::is_arithmetic_v<T>
91 if (value < min_value) {
92 return ValidationResult::Error(
"Value " + std::to_string(value) +
" is less than minimum " + std::to_string(min_value));
100 requires std::is_arithmetic_v<T>
104 if (value > max_value) {
105 return ValidationResult::Error(
"Value " + std::to_string(value) +
" exceeds maximum " + std::to_string(max_value));
113 requires std::is_arithmetic_v<T>
116 return Min(min_value).
And(
Max(max_value));
121 requires std::is_arithmetic_v<T>
125 if (value <= T { 0 }) {
134 requires std::is_arithmetic_v<T>
138 if (value < T { 0 }) {
150 return ValidationResult::Error(
"Value must not be empty");
160 if (value.size() > max_len) {
161 return ValidationResult::Error(
"String length " + std::to_string(value.size()) +
" exceeds maximum " + std::to_string(max_len));
171 if (value.size() < min_len) {
172 return ValidationResult::Error(
"String length " + std::to_string(value.size()) +
" is less than minimum " + std::to_string(min_len));
183 for (
const auto& allowed_value : allowed) {
184 if (value == allowed_value) {
193template <
typename T,
typename Pred>
194 requires std::predicate<Pred, const T&>
Type-erased validator that can hold any validation function.
Definition validator.h:34
Validator()
Creates an always-valid validator.
Definition validator.h:39
auto And(Validator< T > other) const -> Validator< T >
Combines this validator with another (both must pass).
Definition validator.h:54
auto Or(Validator< T > other) const -> Validator< T >
Combines this validator with another (either must pass).
Definition validator.h:68
auto operator()(const T &value) const -> ValidationResult
Validates a value.
Definition validator.h:51
std::function< ValidationResult(const T &)> validator_fn
Definition validator.h:36
Validator(validator_fn fn)
Creates a validator from a function.
Definition validator.h:45
Concept for validator types.
Definition validator.h:28
C++20 compile-time type-safe configuration library.
Definition conf.h:17
auto Positive() -> Validator< T >
Creates a validator that checks if a numeric value is positive.
Definition validator.h:122
auto Predicate(Pred pred, std::string error_message) -> Validator< T >
Creates a validator from a predicate function.
Definition validator.h:195
auto Max(T max_value) -> Validator< T >
Creates a validator that checks if a numeric value is at most max.
Definition validator.h:101
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:157
auto NonNegative() -> Validator< T >
Creates a validator that checks if a numeric value is non-negative.
Definition validator.h:135
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:168
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:114
auto AlwaysValid() -> Validator< T >
Creates an always-valid validator.
Definition validator.h:207
auto Min(T min_value) -> Validator< T >
Creates a validator that checks if a numeric value is at least min.
Definition validator.h:88
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:180
auto NotEmpty() -> Validator< std::string >
Creates a validator that checks if a string is not empty.
Definition validator.h:146
Result of a validation operation.
Definition validator.h:13
static auto Ok() -> ValidationResult
Creates a successful validation result.
Definition validator.h:18
static auto Error(std::string message) -> ValidationResult
Creates a failed validation result with an error message.
Definition validator.h:21
std::string error_message
Definition validator.h:15
bool is_valid
Definition validator.h:14