Skip to content
Download

ValidationBuilder

mach::ValidationBuilder

Configures validation rules for a type. Used to associate validation rules with fields of a type before validation is performed.

Primary header:

#include <mach/ValidationBuilder.hpp>

Convenience header:

#include <mach/mach.hpp>
template <typename T>
class ValidationBuilder {
public:
template <typename Field>
[[nodiscard]] validation::FieldValidationBuilder<T, Field> field(Field T::* field);
};
Type Description
T The type being configured for validation.
Name Description
field Begins configuring validation rules for a field.

Not thread-safe. Validation configuration should be performed from one thread.

  • ValidationBuilder is intended to be used only from a type’s validate() method.
  • Validation rules are configured by selecting a field with field() and applying one or more validation rules using the returned FieldValidationBuilder.
  • Validation configuration is performed once and reused for every validation of the type.
struct RegisterRequest {
std::string username;
std::string email;
int age;
void validate(mach::ValidationBuilder<RegisterRequest>& validation) const {
validation.field(&RegisterRequest::username)
.length(3, 20)
.regex(R"(^[A-Za-z0-9_]+$)");
validation.field(&RegisterRequest::email)
.email();
validation.field(&RegisterRequest::age)
.range(18, 120);
}
};
MACH_DEFINE_JSON(RegisterRequest, username, email, age)
Name Description
FieldValidationBuilder Configures validation rules for a field.