-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation_demo.cpp
More file actions
98 lines (89 loc) · 2.74 KB
/
Copy pathvalidation_demo.cpp
File metadata and controls
98 lines (89 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
*
* @file validation_demo.hpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
* @brief Example of using `Vix::utils::validate_map()` with `Logger`.
*
* Demonstrates schema-based input validation using the `Vix::utils::Validation`
* module, along with structured logging via `Vix::Logger`.
*
* ### Modules demonstrated
* - **Validation.hpp** — Declarative field validation with `Schema`
* - **Logger.hpp** — Thread-safe logging with configurable patterns and levels
*
* ### Example output
* ```
* [2025-10-10 19:04:12.512] [info] Validation OK
* ```
*
* Or, if validation fails:
* ```
* [2025-10-10 19:04:12.512] [error] Validation FAILED:
* [2025-10-10 19:04:12.512] [error] - email -> Email has invalid format
* ```
*
* ### How it works
* 1. Define input data as a `std::unordered_map<std::string, std::string>`
* 2. Define a validation `Schema` with field rules
* 3. Call `validate_map(data, schema)`
* 4. Inspect the returned `Result<void, FieldErrors>`
* 5. Log results with `Logger`
*
* @see Vix::utils::validate_map
* @see Vix::utils::Schema
* @see Vix::utils::Rule
* @see Vix::Logger
*/
#include <vix/utils/Validation.hpp>
#include <vix/utils/Logger.hpp>
#include <iostream>
#include <unordered_map>
using namespace vix::utils;
using vix::utils::Logger;
/**
* @brief Demonstrates form-style validation using the `Validation` utility.
*
* This program validates a set of string fields (`name`, `age`, `email`)
* against a schema that defines type, format, and range constraints.
*
* The results are logged via `Vix::Logger` with formatted, colored output.
*
* @return int Returns `0` on success, `1` on validation error.
*/
int main()
{
// Input data (simulating form submission)
std::unordered_map<std::string, std::string> data{
{"name", "Gaspard"},
{"age", "18"},
{"email", "softadastra@example.com"}};
// Schema definition — declarative validation rules
Schema sch{
{"name", required("Name")},
{"age", num_range(1, 150, "Age")},
{"email", match(R"(^[^@\s]+@[^@\s]+\.[^@\s]+$)", "Email")}};
// Logger setup (pattern & level)
auto &log = Logger::getInstance();
log.setPattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %v");
log.setLevel(Logger::Level::Info);
// Validation process
const auto r = validate_map(data, sch);
if (r.is_ok())
{
log.log(Logger::Level::Info, "Validation OK");
return 0;
}
log.log(Logger::Level::Error, "Validation FAILED:");
for (const auto &kv : r.error())
{
log.log(Logger::Level::Error, " - {} -> {}", kv.first, kv.second);
}
return 1;
}