userver: userver/formats/json/schema.hpp Source File
Loading...
Searching...
No Matches
schema.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <string>
7#include <string_view>
8
12
13USERVER_NAMESPACE_BEGIN
14
15namespace formats::json {
16
18class SchemaValidationException final : public ExceptionWithPath {
19public:
20 SchemaValidationException(std::string_view msg, std::string_view path, std::string_view schema_path);
21
22 std::string_view GetSchemaPath() const noexcept;
23
24private:
25 std::string schema_path_;
26};
27
32class Schema final {
33public:
34 class ValidationResult;
35 class ValidationError;
36
37 explicit Schema(const Value& doc);
38 ~Schema() noexcept;
39
42 ValidationResult Validate(const Value& doc) const;
43
44private:
45 struct Impl;
46 static constexpr std::size_t kSize = 288;
47 static constexpr std::size_t kAlignment = 8;
49};
50
53public:
55 ValidationResult() noexcept;
56
58 ValidationResult& operator=(ValidationResult&&) noexcept;
59 ~ValidationResult() noexcept;
60
62 explicit operator bool() const noexcept;
63
65 bool IsValid() const noexcept;
66
68 bool IsError() const noexcept;
69
73 void ThrowIfError() &&;
74
76 ValidationError GetError() &&;
77
78private:
79 friend class Schema;
80 friend class ValidationError;
81
82 struct Impl;
83 static constexpr std::size_t kSize = 24;
84 static constexpr std::size_t kAlignment = 8;
85 utils::FastPimpl<Impl, kSize, kAlignment> impl_;
86};
87
88class Schema::ValidationError final {
89public:
92 [[noreturn]] void Throw() const;
93
97 std::string_view GetValuePath() const noexcept;
98
101 std::string_view GetSchemaPath() const noexcept;
102
105 std::string_view GetDetailsString() const noexcept;
106
107private:
108 friend class ValidationResult;
109
110 ValidationError();
111
112 std::string value_path_;
113 std::string schema_path_;
114 std::string details_string_;
115};
116
117} // namespace formats::json
118
119USERVER_NAMESPACE_END