userver: userver/components/static_config_validator.hpp Source File
Loading...
Searching...
No Matches
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>) {
30 return;
31 }
32
33 if (components::kHasValidate<Component> || validation_condition == ValidationMode::kAll) {
34 yaml_config::Schema schema;
35 try {
36 schema = Component::GetStaticConfigSchema();
37 } catch (const std::exception& ex) {
38 WrapInvalidStaticConfigSchemaException(ex);
39 }
40 schema.path = component_name;
41
42 yaml_config::impl::Validate(static_config, schema);
43 }
44}
45
46template <typename Component>
47yaml_config::Schema GetStaticConfigSchema() {
48 // TODO: implement for kOnlyTurnedOn
49 return Component::GetStaticConfigSchema();
50}
51
52} // namespace impl
53} // namespace components
54
55USERVER_NAMESPACE_END