feat: Validate unexpected config values (#2457)

This commit is contained in:
Ayaz Salikhov
2025-08-20 12:38:51 +01:00
committed by GitHub
parent e9ab081ab7
commit a172d0b7ea
12 changed files with 101 additions and 59 deletions

View File

@@ -237,6 +237,12 @@ ClioConfigDefinition::parse(ConfigFileInterface const& config)
});
}
for (auto const& key : config.getAllKeys()) {
if (!map_.contains(key) && !arrayPrefixesToKeysMap.contains(key)) {
listOfErrors.emplace_back("Unknown key: " + key);
}
}
if (!listOfErrors.empty())
return listOfErrors;

View File

@@ -19,7 +19,6 @@
#pragma once
#include "rpc/common/APIVersion.hpp"
#include "util/Assert.hpp"
#include "util/config/Array.hpp"
#include "util/config/ConfigConstraints.hpp"
@@ -27,18 +26,15 @@
#include "util/config/ConfigValue.hpp"
#include "util/config/Error.hpp"
#include "util/config/ObjectView.hpp"
#include "util/config/Types.hpp"
#include "util/config/ValueView.hpp"
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <optional>
#include <string>
#include <string_view>
#include <thread>
#include <unordered_map>
#include <utility>
#include <variant>

View File

@@ -22,6 +22,7 @@
#include "util/config/Types.hpp"
#include <optional>
#include <string>
#include <string_view>
#include <vector>
@@ -63,6 +64,14 @@ public:
*/
virtual bool
containsKey(std::string_view key) const = 0;
/**
* @brief Retrieves all keys in the configuration file.
*
* @return A vector of all keys in the configuration file.
*/
virtual std::vector<std::string>
getAllKeys() const = 0;
};
} // namespace util::config

View File

@@ -142,6 +142,16 @@ ConfigFileJson::containsKey(std::string_view key) const
return jsonObject_.contains(key);
}
std::vector<std::string>
ConfigFileJson::getAllKeys() const
{
std::vector<std::string> keys;
for (auto const& [key, value] : jsonObject_) {
keys.push_back(key);
}
return keys;
}
boost::json::object const&
ConfigFileJson::inner() const
{

View File

@@ -28,6 +28,7 @@
#include <expected>
#include <filesystem>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
@@ -73,6 +74,14 @@ public:
[[nodiscard]] bool
containsKey(std::string_view key) const override;
/**
* @brief Retrieves all keys in the configuration file.
*
* @return A vector of all keys in the configuration file.
*/
[[nodiscard]] std::vector<std::string>
getAllKeys() const override;
/**
* @brief Creates a new ConfigFileJson by parsing the provided JSON file and
* stores the values in the object.

View File

@@ -1,49 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2024, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#pragma once
#include "util/config/ConfigFileInterface.hpp"
#include "util/config/Types.hpp"
#include <boost/filesystem/path.hpp>
#include <string_view>
#include <vector>
// TODO: implement when we support yaml
namespace util::config {
/** @brief Yaml representation of config */
class ConfigFileYaml final : public ConfigFileInterface {
public:
ConfigFileYaml() = default;
Value
getValue(std::string_view key) const override;
std::vector<Value>
getArray(std::string_view key) const override;
bool
containsKey(std::string_view key) const override;
};
} // namespace util::config