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/parse/common_containers.hpp>
15#include <userver/formats/yaml_fwd.hpp>
16
17USERVER_NAMESPACE_BEGIN
18
19namespace yaml_config {
20
21struct Schema;
22
23enum class FieldType {
24 kBool,
25 kInteger,
26 kNumber,
27 kString,
28 kArray,
29 kObject,
30};
31
32std::string ToString(FieldType type);
33
34class SchemaPtr final {
35 public:
36 explicit SchemaPtr(Schema&& schema);
37
38 const Schema& operator*() const { return *schema_; }
39 Schema& operator*() { return *schema_; }
40
41 const Schema* operator->() const { return schema_.get(); }
42
43 private:
44 std::unique_ptr<Schema> schema_;
45};
46
47formats::yaml::Value Serialize(const SchemaPtr& schema,
48 formats::serialize::To<formats::yaml::Value>);
49
50/// @ingroup userver_universal
51///
52/// @brief JSON Schema-like type definition
53///
54/// @see @ref static-configs-validation "Static configs validation"
55struct Schema final {
56 void UpdateDescription(std::string new_description);
57
58 static Schema EmptyObject();
59
60 std::string path;
61
62 FieldType type{};
63 std::string description;
64 std::optional<std::string> default_description;
65 std::optional<std::variant<bool, SchemaPtr>> additional_properties;
66 std::optional<std::unordered_map<std::string, SchemaPtr>> properties;
67 std::optional<SchemaPtr> items;
68 std::optional<std::unordered_set<std::string>> enum_values;
69 std::optional<double> minimum;
70 std::optional<double> maximum;
71};
72
73Schema Parse(const formats::yaml::Value& schema, formats::parse::To<Schema>);
74
75formats::yaml::Value Serialize(const Schema& schema,
76 formats::serialize::To<formats::yaml::Value>);
77
78namespace impl {
79
80Schema SchemaFromString(const std::string& yaml_string);
81
82} // namespace impl
83
84} // namespace yaml_config
85
86USERVER_NAMESPACE_END