feat : add structural validation rules to ConfigValidator
This commit is contained in:
parent
4f89a2918c
commit
dbc318cdfa
@ -1,5 +1,4 @@
|
|||||||
#include <webserv/config/validation/ConfigValidator.hpp>
|
#include <webserv/config/validation/ConfigValidator.hpp>
|
||||||
|
|
||||||
#include <webserv/config/validation/ValidationEngine.hpp> // for ValidationEngine
|
#include <webserv/config/validation/ValidationEngine.hpp> // for ValidationEngine
|
||||||
#include <webserv/config/validation/directive_rules/AValidationRule.hpp> // for AValidationRule
|
#include <webserv/config/validation/directive_rules/AValidationRule.hpp> // for AValidationRule
|
||||||
#include <webserv/config/validation/directive_rules/AllowedValuesRule.hpp> // for AllowedValuesRule
|
#include <webserv/config/validation/directive_rules/AllowedValuesRule.hpp> // for AllowedValuesRule
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
#include <webserv/config/validation/ValidationEngine.hpp>
|
|
||||||
|
|
||||||
#include <webserv/config/AConfig.hpp> // for AConfig
|
#include <webserv/config/AConfig.hpp> // for AConfig
|
||||||
#include <webserv/config/GlobalConfig.hpp> // for GlobalConfig
|
#include <webserv/config/GlobalConfig.hpp> // for GlobalConfig
|
||||||
#include <webserv/config/LocationConfig.hpp> // for LocationConfig
|
#include <webserv/config/LocationConfig.hpp> // for LocationConfig
|
||||||
#include <webserv/config/ServerConfig.hpp> // for ServerConfig
|
#include <webserv/config/ServerConfig.hpp> // for ServerConfig
|
||||||
|
#include <webserv/config/validation/ValidationEngine.hpp>
|
||||||
#include <webserv/config/validation/ValidationResult.hpp> // for ValidationResult
|
#include <webserv/config/validation/ValidationResult.hpp> // for ValidationResult
|
||||||
#include <webserv/config/validation/directive_rules/AValidationRule.hpp> // for AValidationRule
|
#include <webserv/config/validation/directive_rules/AValidationRule.hpp> // for AValidationRule
|
||||||
#include <webserv/log/Log.hpp> // for Log, LOCATION
|
#include <webserv/log/Log.hpp> // for Log, LOCATION
|
||||||
@ -31,7 +30,8 @@ void ValidationEngine::addLocationRule(const std::string &directiveName, std::un
|
|||||||
void ValidationEngine::addStructuralRule(std::unique_ptr<AStructuralValidationRule> rule)
|
void ValidationEngine::addStructuralRule(std::unique_ptr<AStructuralValidationRule> rule)
|
||||||
{
|
{
|
||||||
Log::trace(LOCATION);
|
Log::trace(LOCATION);
|
||||||
if (rule != nullptr) {
|
if (rule != nullptr)
|
||||||
|
{
|
||||||
structuralRules_.push_back(std::move(rule));
|
structuralRules_.push_back(std::move(rule));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -124,15 +124,20 @@ void ValidationEngine::validateLocationConfig(const std::string &path, const Loc
|
|||||||
Log::trace(LOCATION);
|
Log::trace(LOCATION);
|
||||||
|
|
||||||
// Run location structural validation rules
|
// Run location structural validation rules
|
||||||
for (const auto &rule : structuralRules_) {
|
for (const auto &rule : structuralRules_)
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
ValidationResult result = rule->validateLocation(config);
|
ValidationResult result = rule->validateLocation(config);
|
||||||
if (!result.isValidResult()) {
|
if (!result.isValidResult())
|
||||||
|
{
|
||||||
results_.push_back(result);
|
results_.push_back(result);
|
||||||
}
|
}
|
||||||
} catch (const std::exception &e) {
|
}
|
||||||
results_.push_back(ValidationResult::error(
|
catch (const std::exception &e)
|
||||||
"Structural rule '" + rule->getRuleName() + "' threw exception for location '" + path + "': " + e.what()));
|
{
|
||||||
|
results_.push_back(ValidationResult::error("Structural rule '" + rule->getRuleName() +
|
||||||
|
"' threw exception for location '" + path + "': " + e.what()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,15 +150,20 @@ void ValidationEngine::validateServerConfig(const ServerConfig *config)
|
|||||||
Log::trace(LOCATION);
|
Log::trace(LOCATION);
|
||||||
|
|
||||||
// Run server structural validation rules
|
// Run server structural validation rules
|
||||||
for (const auto &rule : structuralRules_) {
|
for (const auto &rule : structuralRules_)
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
ValidationResult result = rule->validateServer(config);
|
ValidationResult result = rule->validateServer(config);
|
||||||
if (!result.isValidResult()) {
|
if (!result.isValidResult())
|
||||||
|
{
|
||||||
results_.push_back(result);
|
results_.push_back(result);
|
||||||
}
|
}
|
||||||
} catch (const std::exception &e) {
|
}
|
||||||
results_.push_back(ValidationResult::error(
|
catch (const std::exception &e)
|
||||||
"Structural rule '" + rule->getRuleName() + "' threw exception: " + e.what()));
|
{
|
||||||
|
results_.push_back(
|
||||||
|
ValidationResult::error("Structural rule '" + rule->getRuleName() + "' threw exception: " + e.what()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,15 +185,20 @@ void ValidationEngine::validateGlobalConfig(const GlobalConfig *config)
|
|||||||
Log::trace(LOCATION);
|
Log::trace(LOCATION);
|
||||||
|
|
||||||
// Run global structural validation rules
|
// Run global structural validation rules
|
||||||
for (const auto &rule : structuralRules_) {
|
for (const auto &rule : structuralRules_)
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
ValidationResult result = rule->validateGlobal(config);
|
ValidationResult result = rule->validateGlobal(config);
|
||||||
if (!result.isValidResult()) {
|
if (!result.isValidResult())
|
||||||
|
{
|
||||||
results_.push_back(result);
|
results_.push_back(result);
|
||||||
}
|
}
|
||||||
} catch (const std::exception &e) {
|
}
|
||||||
results_.push_back(ValidationResult::error(
|
catch (const std::exception &e)
|
||||||
"Structural rule '" + rule->getRuleName() + "' threw exception: " + e.what()));
|
{
|
||||||
|
results_.push_back(
|
||||||
|
ValidationResult::error("Structural rule '" + rule->getRuleName() + "' threw exception: " + e.what()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,7 +16,9 @@ private:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
AStructuralValidationRule(const std::string &ruleName, const std::string &description)
|
AStructuralValidationRule(const std::string &ruleName, const std::string &description)
|
||||||
: ruleName_(ruleName), description_(description) {}
|
: ruleName_(ruleName), description_(description)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual ~AStructuralValidationRule() = default;
|
virtual ~AStructuralValidationRule() = default;
|
||||||
@ -46,13 +48,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Non-virtual getters - set in constructor
|
// Non-virtual getters - set in constructor
|
||||||
[[nodiscard]] std::string getRuleName() const
|
[[nodiscard]] std::string getRuleName() const { return ruleName_; }
|
||||||
{
|
|
||||||
return ruleName_;
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] std::string getDescription() const
|
[[nodiscard]] std::string getDescription() const { return description_; }
|
||||||
{
|
|
||||||
return description_;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
@ -6,8 +6,8 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
MinimumServerBlocksRule::MinimumServerBlocksRule(size_t minimumServers)
|
MinimumServerBlocksRule::MinimumServerBlocksRule(size_t minimumServers)
|
||||||
: AStructuralValidationRule("MinimumServerBlocksRule",
|
: AStructuralValidationRule("MinimumServerBlocksRule", "Ensures global config has at least " +
|
||||||
"Ensures global config has at least " + std::to_string(minimumServers) + " server block(s)"),
|
std::to_string(minimumServers) + " server block(s)"),
|
||||||
minimumServers_(minimumServers)
|
minimumServers_(minimumServers)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -16,15 +16,16 @@ ValidationResult MinimumServerBlocksRule::validateGlobal(const GlobalConfig *con
|
|||||||
{
|
{
|
||||||
Log::trace(LOCATION);
|
Log::trace(LOCATION);
|
||||||
|
|
||||||
if (config == nullptr) {
|
if (config == nullptr)
|
||||||
|
{
|
||||||
return ValidationResult::error("Global config is null");
|
return ValidationResult::error("Global config is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t serverCount = config->getServerConfigs().size();
|
size_t serverCount = config->getServerConfigs().size();
|
||||||
|
|
||||||
if (serverCount < minimumServers_) {
|
if (serverCount < minimumServers_)
|
||||||
return ValidationResult::error(
|
{
|
||||||
"Global configuration must have at least " + std::to_string(minimumServers_) +
|
return ValidationResult::error("Global configuration must have at least " + std::to_string(minimumServers_) +
|
||||||
" server block(s), but found " + std::to_string(serverCount));
|
" server block(s), but found " + std::to_string(serverCount));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user