userver: userver/components/static_config_validator.hpp Source File
Loading...
Searching...
No Matches
static_config_validator.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/components/static_config_validator.hpp
4/// @brief Static component configuration validation helpers
5
6#include <userver/components/component_config.hpp>
7#include <userver/components/raw_component_base.hpp>
8#include <userver/yaml_config/impl/validate_static_config.hpp>
9#include <userver/yaml_config/schema.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace components {
14
15enum class ValidationMode {
16 kOnlyTurnedOn,
17 kAll,
18};
19
20ValidationMode Parse(const yaml_config::YamlConfig& value, formats::parse::To<ValidationMode>);
21
22namespace impl {
23
24[[noreturn]] void WrapInvalidStaticConfigSchemaException(const std::exception&);
25
26template <typename Component>
27void TryValidateStaticConfig(
28 std::string_view component_name,
29 const components::ComponentConfig& static_config,
30 ValidationMode validation_condition
31) {
32 if (components::kForceNoValidation<Component>) {
33 return;
34 }
35
36 if (components::kHasValidate<Component> || validation_condition == ValidationMode::kAll) {
37 yaml_config::Schema schema;
38 try {
39 schema = Component::GetStaticConfigSchema();
40 } catch (const std::exception& ex) {
41 WrapInvalidStaticConfigSchemaException(ex);
42 }
43 schema.path = component_name;
44
45 yaml_config::impl::Validate(static_config, schema);
46 }
47}
48
49template <typename Component>
50yaml_config::Schema GetStaticConfigSchema() {
51 // TODO: implement for kOnlyTurnedOn
52 return Component::GetStaticConfigSchema();
53}
54
55} // namespace impl
56} // namespace components
57
58USERVER_NAMESPACE_END