ValidationBuilder
mach::ValidationBuilder
Description
Section titled “Description”Configures validation rules for a type. Used to associate validation rules with fields of a type before validation is performed.
Headers
Section titled “Headers”Primary header:
#include <mach/ValidationBuilder.hpp>Convenience header:
#include <mach/mach.hpp>Syntax
Section titled “Syntax”template <typename T>class ValidationBuilder {
public: template <typename Field> [[nodiscard]] validation::FieldValidationBuilder<T, Field> field(Field T::* field);};Template Parameters
Section titled “Template Parameters”| Type | Description |
|---|---|
T |
The type being configured for validation. |
Member Functions
Section titled “Member Functions”| Name | Description |
|---|---|
field |
Begins configuring validation rules for a field. |
Thread Safety
Section titled “Thread Safety”Not thread-safe. Validation configuration should be performed from one thread.
Ownership
Section titled “Ownership”Remarks
Section titled “Remarks”ValidationBuilderis intended to be used only from a type’svalidate()method.- Validation rules are configured by selecting a field with
field()and applying one or more validation rules using the returnedFieldValidationBuilder. - Validation configuration is performed once and reused for every validation of the type.
Example
Section titled “Example”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)See Also
Section titled “See Also”| Name | Description |
|---|---|
FieldValidationBuilder |
Configures validation rules for a field. |