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
3/// @file userver/formats/json/schema.hpp
4/// @brief JSON schema validator
5
6#include <string>
7#include <string_view>
8
9#include <userver/formats/json/exception.hpp>
10#include <userver/formats/json/value.hpp>
11#include <userver/utils/fast_pimpl.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace formats::json {
16
17/// @see formats::json::Schema::ValidationError::Throw
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
28/// @brief Contains a prepared JSON schema.
29///
30/// Usage example:
31/// @snippet formats/json/schema_test.cpp sample
32class Schema final {
33public:
34 class ValidationResult;
35 class ValidationError;
36
37 explicit Schema(const Value& doc);
38 ~Schema() noexcept;
39
40 /// @brief Validates JSON against the Schema.
41 /// @returns ValidationResult describing the validation error, if any.
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;
48 utils::FastPimpl<Impl, kSize, kAlignment> impl_;
49};
50
51/// Contains error details (if any) from Schema::Validate.
52class Schema::ValidationResult final {
53public:
54 /// Creates an `IsValid` result.
55 ValidationResult() noexcept;
56
57 ValidationResult(ValidationResult&&) noexcept;
58 ValidationResult& operator=(ValidationResult&&) noexcept;
59 ~ValidationResult() noexcept;
60
61 /// @returns `true` on validation success.
62 explicit operator bool() const noexcept;
63
64 /// @returns `true` on validation success.
65 bool IsValid() const noexcept;
66
67 /// @returns `true` on validation error.
68 bool IsError() const noexcept;
69
70 /// @throws SchemaValidationException on validation error.
71 /// @see Schema::ValidationResult::GetError
72 /// @see Schema::ValidationError::Throw
73 void ThrowIfError() &&;
74
75 /// @returns Validation error, `IsError` must be true.
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:
90 /// @throws SchemaValidationException with error details.
91 /// @see Schema::ValidationResult::ThrowIfError
92 [[noreturn]] void Throw() const;
93
94 /// Describes the location within the validated JSON which violates
95 /// schema. The exact format of value path is unstable and should not
96 /// be relied upon.
98 /// Describes the location within the schema which was violated. The exact
99 /// format of schema path is unstable and should not be relied upon.
101 /// Describes the specifics of what condition was violated. The exact
102 /// format of details is unstable and should not be relied upon.
104
105private:
106 friend class ValidationResult;
107
108 ValidationError();
109
110 std::string value_path_;
111 std::string schema_path_;
112 std::string details_string_;
113};
114
115} // namespace formats::json
116
117USERVER_NAMESPACE_END