userver
C++ Async Framework
Documentation
API Groups
Namespaces
Reference
Class List
Class Index
File List
Macros
All
e
i
l
r
t
u
Functions
Macros
e
i
l
r
t
u
Examples
Toggle main menu visibility
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Modules
Pages
Concepts
Loading...
Searching...
No Matches
All results
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
13
USERVER_NAMESPACE_BEGIN
14
15
namespace
formats
::
json
{
16
17
/// @see formats::json::Schema::ValidationError::Throw
18
class
SchemaValidationException
final
:
public
ExceptionWithPath
{
19
public
:
20
SchemaValidationException(std::string_view msg, std::string_view path, std::string_view schema_path);
21
22
std::string_view GetSchemaPath()
const
noexcept
;
23
24
private
:
25
std::string schema_path_;
26
};
18
class
SchemaValidationException
final
:
public
ExceptionWithPath
{
…
};
27
28
/// @brief Contains a prepared JSON schema.
29
///
30
/// Usage example:
31
/// @snippet formats/json/schema_test.cpp sample
32
class
Schema
final
{
33
public
:
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
44
private
:
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
};
32
class
Schema
final
{
…
};
50
51
/// Contains error details (if any) from Schema::Validate.
52
class
Schema::ValidationResult
final
{
53
public
:
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
78
private
:
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
};
52
class
Schema::ValidationResult
final
{
…
};
87
88
class
Schema::ValidationError
final
{
89
public
:
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.
97
std::string_view
GetValuePath
()
const
;
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.
100
std::string_view
GetSchemaPath
()
const
;
101
/// Describes the specifics of what condition was violated. The exact
102
/// format of details is unstable and should not be relied upon.
103
std::string_view
GetDetailsString
()
const
;
104
105
private
:
106
friend
class
ValidationResult;
107
108
ValidationError();
109
110
std::string value_path_;
111
std::string schema_path_;
112
std::string details_string_;
113
};
88
class
Schema::ValidationError
final
{
…
};
114
115
}
// namespace formats::json
116
117
USERVER_NAMESPACE_END
Docs version:
v1.0
,
v2.0
,
trunk/develop
userver
formats
json
schema.hpp
Generated on Wed Apr 30 2025 15:59:17 for userver by
Doxygen
1.13.2