13#include "PropertyMacros.h"
36 return {
false, msg, prop};
49 const std::string& propName,
68 RangeValidator(T min, T max,
bool minInclusive =
true,
bool maxInclusive =
true)
72 const std::string& propName,
78 if (
auto* i = std::get_if<int64_t>(&value)) {
79 v =
static_cast<T
>(*i);
80 }
else if (
auto* d = std::get_if<double>(&value)) {
81 v =
static_cast<T
>(*d);
84 "Expected numeric value for range validation");
91 if (!minOk || !maxOk) {
92 std::ostringstream oss;
93 oss <<
"Value " << v <<
" out of range "
104 std::ostringstream oss;
126 const std::string& description =
"")
129 regex_ = std::regex(pattern);
130 }
catch (
const std::regex_error& e) {
131 throw std::runtime_error(
"Invalid regex pattern: " + pattern +
" - " + e.what());
136 const std::string& propName,
139 auto* str = std::get_if<std::string>(&value);
142 "Expected string value for regex validation");
145 if (!std::regex_match(*str,
regex_)) {
146 std::string msg =
"String '" + *str +
"' does not match pattern";
169 inline const char*
IPv4 = R
"(^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)";
172 inline const char*
Email = R
"(^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$)";
175 inline const char*
UnixPath = R
"(^(/[^/\0]+)+/?$|^/$)";
178 inline const char*
URL = R
"(^https?://[^\s/$.?#].[^\s]*$)";
181 inline const char*
Identifier = R
"(^[a-zA-Z_][a-zA-Z0-9_]*$)";
192 EnumValidator(std::initializer_list<std::string> values,
bool caseSensitive =
true)
199 const std::string& propName,
202 auto* str = std::get_if<std::string>(&value);
205 "Expected string value for enum validation");
208 std::string input = *str;
210 std::transform(input.begin(), input.end(), input.begin(), ::tolower);
213 for (
const auto& allowed :
allowed_) {
214 std::string cmp = allowed;
216 std::transform(cmp.begin(), cmp.end(), cmp.begin(), ::tolower);
223 std::ostringstream oss;
224 oss <<
"Value '" << *str <<
"' not in allowed values: {";
225 for (
size_t i = 0; i <
allowed_.size(); ++i) {
226 if (i > 0) oss <<
", ";
234 std::ostringstream oss;
236 for (
size_t i = 0; i <
allowed_.size(); ++i) {
237 if (i > 0) oss <<
",";
254 void add(std::shared_ptr<PropertyValidator> validator) {
259 const std::string& propName,
263 auto result = v->validate(propName, value);
272 std::ostringstream oss;
274 if (i > 0) oss <<
" AND ";
285 std::map<std::string, std::shared_ptr<PropertyValidator>>
validators_;
289 std::shared_ptr<PropertyValidator> validator) {
299 return it->second->validate(propName, value);
307 const std::map<std::string, ScalarPropertyValue>& props
309 std::vector<ValidationResult> results;
310 for (
const auto& [name, value] : props) {
311 auto result =
validate(name, value);
313 results.push_back(result);
326 return std::make_shared<RangeValidator<T>>(min, max);
330 const std::string& pattern,
331 const std::string& description =
""
333 return std::make_shared<RegexValidator>(pattern, description);
337 std::initializer_list<std::string> values,
338 bool caseSensitive =
true
340 return std::make_shared<EnumValidator>(values, caseSensitive);
Definition PropertyValidators.h:250
void add(std::shared_ptr< PropertyValidator > validator)
Definition PropertyValidators.h:254
std::vector< std::shared_ptr< PropertyValidator > > validators_
Definition PropertyValidators.h:251
std::string describe() const override
Definition PropertyValidators.h:271
ValidationResult validate(const std::string &propName, const ScalarPropertyValue &value) const override
Definition PropertyValidators.h:258
Definition PropertyValidators.h:187
bool caseSensitive_
Definition PropertyValidators.h:189
std::string describe() const override
Definition PropertyValidators.h:233
EnumValidator(std::initializer_list< std::string > values, bool caseSensitive=true)
Definition PropertyValidators.h:192
EnumValidator(std::vector< std::string > values, bool caseSensitive=true)
Definition PropertyValidators.h:195
const std::vector< std::string > & allowedValues() const
Definition PropertyValidators.h:244
std::vector< std::string > allowed_
Definition PropertyValidators.h:188
ValidationResult validate(const std::string &propName, const ScalarPropertyValue &value) const override
Definition PropertyValidators.h:198
Definition PropertyValidators.h:284
std::vector< ValidationResult > validateAll(const std::map< std::string, ScalarPropertyValue > &props) const
Definition PropertyValidators.h:306
void registerValidator(const std::string &propName, std::shared_ptr< PropertyValidator > validator)
Definition PropertyValidators.h:288
std::map< std::string, std::shared_ptr< PropertyValidator > > validators_
Definition PropertyValidators.h:285
bool hasValidator(const std::string &propName) const
Definition PropertyValidators.h:302
ValidationResult validate(const std::string &propName, const ScalarPropertyValue &value) const
Definition PropertyValidators.h:293
Definition PropertyValidators.h:43
virtual std::string describe() const =0
virtual ValidationResult validate(const std::string &propName, const ScalarPropertyValue &value) const =0
virtual ~PropertyValidator()=default
Definition PropertyValidators.h:61
bool maxInclusive_
Definition PropertyValidators.h:65
T min_
Definition PropertyValidators.h:62
bool minInclusive_
Definition PropertyValidators.h:64
std::string describe() const override
Definition PropertyValidators.h:103
RangeValidator(T min, T max, bool minInclusive=true, bool maxInclusive=true)
Definition PropertyValidators.h:68
T max_
Definition PropertyValidators.h:63
ValidationResult validate(const std::string &propName, const ScalarPropertyValue &value) const override
Definition PropertyValidators.h:71
Definition PropertyValidators.h:119
std::string description_
Definition PropertyValidators.h:122
std::string describe() const override
Definition PropertyValidators.h:158
RegexValidator(const std::string &pattern, const std::string &description="")
Definition PropertyValidators.h:125
std::string pattern_
Definition PropertyValidators.h:120
ValidationResult validate(const std::string &propName, const ScalarPropertyValue &value) const override
Definition PropertyValidators.h:135
std::regex regex_
Definition PropertyValidators.h:121
const char * UnixPath
Definition PropertyValidators.h:175
const char * Email
Definition PropertyValidators.h:172
const char * URL
Definition PropertyValidators.h:178
const char * IPv4
Definition PropertyValidators.h:169
const char * Identifier
Definition PropertyValidators.h:181
Definition FrameTypeRegistrations.h:10
std::shared_ptr< RangeValidator< T > > makeRangeValidator(T min, T max)
Definition PropertyValidators.h:325
std::shared_ptr< EnumValidator > makeEnumValidator(std::initializer_list< std::string > values, bool caseSensitive=true)
Definition PropertyValidators.h:336
std::shared_ptr< RegexValidator > makeRegexValidator(const std::string &pattern, const std::string &description="")
Definition PropertyValidators.h:329
std::variant< int64_t, double, bool, std::string > ScalarPropertyValue
Definition ModuleRegistry.h:30
Definition PropertyValidators.h:29
std::string propertyName
Definition PropertyValidators.h:32
std::string error
Definition PropertyValidators.h:31
bool valid
Definition PropertyValidators.h:30
static ValidationResult ok()
Definition PropertyValidators.h:34
static ValidationResult fail(const std::string &prop, const std::string &msg)
Definition PropertyValidators.h:35