|
| 1 | +/** |
| 2 | + * Validation examples for Vix.cpp |
| 3 | + * |
| 4 | + * This file demonstrates how to use the validation module: |
| 5 | + * - simple field validation |
| 6 | + * - numeric validation |
| 7 | + * - parsed validation (string -> int) |
| 8 | + * - schema / form validation |
| 9 | + * |
| 10 | + * These examples are intended for: |
| 11 | + * - HTTP controllers |
| 12 | + * - JSON / form validation |
| 13 | + * - CLI argument validation |
| 14 | + */ |
| 15 | + |
| 16 | +#include <iostream> |
| 17 | +#include <optional> |
| 18 | +#include <string> |
| 19 | + |
| 20 | +#include <vix/validation/Schema.hpp> |
| 21 | +#include <vix/validation/Validate.hpp> |
| 22 | +#include <vix/validation/Pipe.hpp> |
| 23 | + |
| 24 | +using namespace vix::validation; |
| 25 | + |
| 26 | +// ------------------------------------------------------------ |
| 27 | +// Example 1: simple string validation |
| 28 | +// ------------------------------------------------------------ |
| 29 | +void example_simple_string() |
| 30 | +{ |
| 31 | + std::string email = "john@example.com"; |
| 32 | + |
| 33 | + auto res = validate("email", email) |
| 34 | + .required() |
| 35 | + .email() |
| 36 | + .length_max(120) |
| 37 | + .result(); |
| 38 | + |
| 39 | + std::cout << "[example_simple_string] ok=" << res.ok() << "\n"; |
| 40 | +} |
| 41 | + |
| 42 | +// ------------------------------------------------------------ |
| 43 | +// Example 2: numeric validation (already typed) |
| 44 | +// ------------------------------------------------------------ |
| 45 | +void example_numeric() |
| 46 | +{ |
| 47 | + int age = 17; |
| 48 | + |
| 49 | + auto res = validate("age", age) |
| 50 | + .min(18, "must be adult") |
| 51 | + .max(120) |
| 52 | + .result(); |
| 53 | + |
| 54 | + std::cout << "[example_numeric] ok=" << res.ok() << "\n"; |
| 55 | +} |
| 56 | + |
| 57 | +// ------------------------------------------------------------ |
| 58 | +// Example 3: parsed validation (string -> int) |
| 59 | +// ------------------------------------------------------------ |
| 60 | +void example_parsed() |
| 61 | +{ |
| 62 | + std::string age_input = "25"; // try "abc" or "10" |
| 63 | + |
| 64 | + auto res = validate_parsed<int>("age", age_input) |
| 65 | + .between(18, 120) |
| 66 | + .result("age must be a number"); |
| 67 | + |
| 68 | + std::cout << "[example_parsed] ok=" << res.ok() << "\n"; |
| 69 | +} |
| 70 | + |
| 71 | +// ------------------------------------------------------------ |
| 72 | +// Example 4: optional field |
| 73 | +// ------------------------------------------------------------ |
| 74 | +void example_optional() |
| 75 | +{ |
| 76 | + std::optional<int> score = std::nullopt; |
| 77 | + |
| 78 | + auto res = validate("score", score) |
| 79 | + .required("score is required") |
| 80 | + .result(); |
| 81 | + |
| 82 | + std::cout << "[example_optional] ok=" << res.ok() << "\n"; |
| 83 | +} |
| 84 | + |
| 85 | +// ------------------------------------------------------------ |
| 86 | +// Example 5: in_set validation |
| 87 | +// ------------------------------------------------------------ |
| 88 | +void example_in_set() |
| 89 | +{ |
| 90 | + std::string role = "admin"; |
| 91 | + |
| 92 | + auto res = validate("role", role) |
| 93 | + .required() |
| 94 | + .in_set({"admin", "user", "guest"}) |
| 95 | + .result(); |
| 96 | + |
| 97 | + std::cout << "[example_in_set] ok=" << res.ok() << "\n"; |
| 98 | +} |
| 99 | + |
| 100 | +// ------------------------------------------------------------ |
| 101 | +// Example 6: schema validation (form / entity) |
| 102 | +// ------------------------------------------------------------ |
| 103 | +struct RegisterForm |
| 104 | +{ |
| 105 | + std::string email; |
| 106 | + std::string password; |
| 107 | + std::string age; // raw input |
| 108 | +}; |
| 109 | + |
| 110 | +void example_schema() |
| 111 | +{ |
| 112 | + auto schema = vix::validation::schema<RegisterForm>() |
| 113 | + .field("email", &RegisterForm::email, |
| 114 | + [](auto f, const std::string &v) |
| 115 | + { |
| 116 | + return validate(f, v) |
| 117 | + .required() |
| 118 | + .email() |
| 119 | + .length_max(120) |
| 120 | + .result(); |
| 121 | + }) |
| 122 | + .field("password", &RegisterForm::password, |
| 123 | + [](auto f, const std::string &v) |
| 124 | + { |
| 125 | + return validate(f, v) |
| 126 | + .required() |
| 127 | + .length_min(8) |
| 128 | + .length_max(64) |
| 129 | + .result(); |
| 130 | + }) |
| 131 | + .parsed<int>("age", &RegisterForm::age, |
| 132 | + [](auto f, std::string_view sv) |
| 133 | + { |
| 134 | + return validate_parsed<int>(f, sv) |
| 135 | + .between(18, 120) |
| 136 | + .result("age must be a number"); |
| 137 | + }); |
| 138 | + |
| 139 | + RegisterForm form{ |
| 140 | + "bad-email", |
| 141 | + "123", |
| 142 | + "abc"}; |
| 143 | + |
| 144 | + auto res = schema.validate(form); |
| 145 | + |
| 146 | + std::cout << "[example_schema] ok=" << res.ok() << "\n"; |
| 147 | + std::cout << "errors=" << res.errors.size() << "\n"; |
| 148 | + |
| 149 | + for (const auto &e : res.errors.all()) |
| 150 | + { |
| 151 | + std::cout |
| 152 | + << " - field=" << e.field |
| 153 | + << " code=" << to_string(e.code) |
| 154 | + << " message=" << e.message |
| 155 | + << "\n"; |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +// ------------------------------------------------------------ |
| 160 | +// Main |
| 161 | +// ------------------------------------------------------------ |
| 162 | +int main() |
| 163 | +{ |
| 164 | + example_simple_string(); |
| 165 | + example_numeric(); |
| 166 | + example_parsed(); |
| 167 | + example_optional(); |
| 168 | + example_in_set(); |
| 169 | + example_schema(); |
| 170 | + |
| 171 | + return 0; |
| 172 | +} |
0 commit comments