Aria 2.0.0
C++23 MVVM framework (C++20 minimum) — reactive, coroutine-first, ABI-layered
Loading...
Searching...
No Matches
validation_key.hpp
Go to the documentation of this file.
1#pragma once
2
3// ============================================================================
4// aria/validation_key.hpp
5// ----------------------------------------------------------------------------
6// `ValidationKey` and `Severity` -- the locator + severity primitives
7// used by the unified `aria::Error` model (see <aria/error.hpp>).
8//
9// Per docs/error-model.md every validator-produced error is just an
10// `aria::Error` with `kind == ErrorKind::Validation` and a populated
11// `key`. There is **no** independent `ValidationError` struct -- the
12// unification is the whole point of the unified error taxonomy.
13//
14// Why split this out from `error.hpp`?
15// * `ValidationKey` is also handy in test code and pure validation
16// helpers that do not want to drag in the full Error type.
17// * Keeps `error.hpp` free of any forward-declaration tricks.
18// * The two together form one "locator" toolbox; users that need
19// both should `#include <aria/error.hpp>`, which transitively
20// pulls this header in.
21//
22// Per docs/api-style.md S-1 these names live in `aria::`.
23// ============================================================================
24
25#include <cstddef>
26#include <cstdint>
27#include <functional>
28#include <ostream>
29#include <string>
30#include <utility>
31
32namespace aria {
33
34// ---------------------------------------------------------------------------
35// Severity
36// ---------------------------------------------------------------------------
37
42enum class Severity : std::uint8_t {
43 Error = 0,
45};
46
47// ---------------------------------------------------------------------------
48// ValidationKey
49// ---------------------------------------------------------------------------
50
53 std::string field_path;
54 std::string rule_id;
55
56 [[nodiscard]] bool empty() const noexcept {
57 return field_path.empty() && rule_id.empty();
58 }
59
61 [[nodiscard]] std::string to_string() const {
62 std::string out;
63 out.reserve(field_path.size() + rule_id.size() + 1);
64 out.append(field_path);
65 out.push_back('#');
66 out.append(rule_id);
67 return out;
68 }
69};
70
71inline bool operator==(const ValidationKey& a, const ValidationKey& b) noexcept {
72 return a.field_path == b.field_path && a.rule_id == b.rule_id;
73}
74inline bool operator!=(const ValidationKey& a, const ValidationKey& b) noexcept {
75 return !(a == b);
76}
77
78inline std::ostream& operator<<(std::ostream& os, const ValidationKey& k) {
79 return os << k.to_string();
80}
81
82// ---------------------------------------------------------------------------
83// Sugar: `field("...") / rule_id("...")` builder
84// ---------------------------------------------------------------------------
85
86namespace validation_dsl {
87
88struct FieldPart { std::string path; };
89struct RulePart { std::string id; };
90
91} // namespace validation_dsl
92
93[[nodiscard]] inline validation_dsl::FieldPart field(std::string path) noexcept {
94 return validation_dsl::FieldPart{std::move(path)};
95}
96[[nodiscard]] inline validation_dsl::RulePart rule_id(std::string id) noexcept {
97 return validation_dsl::RulePart{std::move(id)};
98}
99
100[[nodiscard]] inline ValidationKey
102 return ValidationKey{std::move(f.path), std::move(r.id)};
103}
104
105} // namespace aria
106
107// ---------------------------------------------------------------------------
108// std::hash specialisation -- so ValidationKey can be a map key.
109// ---------------------------------------------------------------------------
110namespace std {
111
112template<>
113struct hash<::aria::ValidationKey> {
114 [[nodiscard]] std::size_t operator()(const ::aria::ValidationKey& k) const noexcept {
115 std::size_t h = std::hash<std::string>{}(k.field_path);
116 h ^= std::hash<std::string>{}(k.rule_id) + 0x9e3779b97f4a7c15ULL
117 + (h << 6) + (h >> 2);
118 return h;
119 }
120};
121
122} // namespace std
Definition validation_key.hpp:86
Definition signal.hpp:12
std::ostream & operator<<(std::ostream &os, const Error &e)
Definition error.hpp:275
validation_dsl::RulePart rule_id(std::string id) noexcept
Definition validation_key.hpp:96
validation_dsl::FieldPart field(std::string path) noexcept
Definition validation_key.hpp:93
ValidationKey operator/(validation_dsl::FieldPart f, validation_dsl::RulePart r)
Definition validation_key.hpp:101
bool operator!=(const Error &a, const Error &b) noexcept
Definition error.hpp:271
bool operator==(const Error &a, const Error &b) noexcept
Definition error.hpp:264
Severity
Severity of a single error / warning.
Definition validation_key.hpp:42
@ Warning
Definition validation_key.hpp:44
Definition validation_key.hpp:110
One uniform error record.
Definition error.hpp:129
(field_path, rule_id) locator for a validation message.
Definition validation_key.hpp:52
std::string rule_id
e.g. "required"; empty = anonymous
Definition validation_key.hpp:54
bool empty() const noexcept
Definition validation_key.hpp:56
std::string field_path
e.g. "user.email"; empty = form-level
Definition validation_key.hpp:53
std::string to_string() const
Stable string view of the form "<field_path>#<rule_id>".
Definition validation_key.hpp:61
Definition validation_key.hpp:88
std::string path
Definition validation_key.hpp:88
Definition validation_key.hpp:89
std::string id
Definition validation_key.hpp:89
std::size_t operator()(const ::aria::ValidationKey &k) const noexcept
Definition validation_key.hpp:114