userver: userver/yaml_config/schema.hpp Source File
Loading...
Searching...
No Matches
schema.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/yaml_config/schema.hpp
4/// @brief @copybrief yaml_config::Schema
5
6#include <memory>
7#include <optional>
8#include <string>
9#include <unordered_map>
10#include <unordered_set>
11#include <variant>
12#include <vector>
13
14#include <userver/formats/yaml_fwd.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace yaml_config {
19
20struct Schema;
21
22enum class FieldType {
23 kBool,
24 kInteger,
25 kNumber,
26 kString,
27 kArray,
28 kObject,
29};
30
31std::string ToString(FieldType type);
32
33class SchemaPtr final {
34public:
35 explicit SchemaPtr(Schema&& schema);
36
37 SchemaPtr(SchemaPtr&&) noexcept = default;
38 SchemaPtr& operator=(SchemaPtr&&) noexcept = default;
39 SchemaPtr(const SchemaPtr&);
40 SchemaPtr& operator=(const SchemaPtr&);
41
42 const Schema& operator*() const;
43 Schema& operator*();
44 const Schema* operator->() const { return &**this; }
45 Schema* operator->() { return &**this; }
46
47 bool operator==(const SchemaPtr&) const;
48
49private:
50 std::unique_ptr<Schema> schema_;
51};
52
53formats::yaml::Value Serialize(const SchemaPtr& schema, formats::serialize::To<formats::yaml::Value>);
54
55/// @ingroup userver_universal
56///
57/// @brief JSON Schema-like type definition
58///
59/// @see @ref static-configs-validation "Static configs validation"
60struct Schema final {
61 void UpdateDescription(std::string new_description);
62
63 static Schema EmptyObject();
64
65 bool operator==(const Schema&) const;
66
67 std::string path;
68
69 FieldType type{};
70 std::string description;
71 std::optional<std::string> default_description;
72 std::optional<std::variant<bool, SchemaPtr>> additional_properties;
73 std::optional<std::unordered_map<std::string, SchemaPtr>> properties;
74 std::optional<SchemaPtr> items;
75 std::optional<std::unordered_set<std::string>> enum_values;
76 std::optional<double> minimum;
77 std::optional<double> maximum;
78 std::optional<std::size_t> min_items;
79 std::optional<std::size_t> max_items;
80};
81
82Schema Parse(const formats::yaml::Value& schema, formats::parse::To<Schema>);
83
84formats::yaml::Value Serialize(const Schema& schema, formats::serialize::To<formats::yaml::Value>);
85
86namespace impl {
87
88Schema SchemaFromString(const std::string& yaml_string);
89
90} // namespace impl
91
92} // namespace yaml_config
93
94USERVER_NAMESPACE_END