userver: userver/components/static_config_validator.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
static_config_validator.hpp
1#pragma once
2
3#include <userver/components/component_config.hpp>
4#include <userver/components/raw_component_base.hpp>
5#include <userver/yaml_config/impl/validate_static_config.hpp>
6#include <userver/yaml_config/schema.hpp>
7
8USERVER_NAMESPACE_BEGIN
9
10namespace components {
11
12enum class ValidationMode {
13 kOnlyTurnedOn,
14 kAll,
15};
16
17ValidationMode Parse(const yaml_config::YamlConfig& value, formats::parse::To<ValidationMode>);
18
19namespace impl {
20
21[[noreturn]] void WrapInvalidStaticConfigSchemaException(const std::exception&);
22
23template <typename Component>
24void TryValidateStaticConfig(
25 std::string_view component_name,
26 const components::ComponentConfig& static_config,
27 ValidationMode validation_condition
28) {
29 if (components::kForceNoValidation<Component>) return;
30
31 if (components::kHasValidate<Component> || validation_condition == ValidationMode::kAll) {
32 yaml_config::Schema schema;
33 try {
34 schema = Component::GetStaticConfigSchema();
35 } catch (const std::exception& ex) {
36 WrapInvalidStaticConfigSchemaException(ex);
37 }
38 schema.path = component_name;
39
40 yaml_config::impl::Validate(static_config, schema);
41 }
42}
43
44template <typename Component>
45yaml_config::Schema GetStaticConfigSchema() {
46 // TODO: implement for kOnlyTurnedOn
47 return Component::GetStaticConfigSchema();
48}
49
50} // namespace impl
51} // namespace components
52
53USERVER_NAMESPACE_END