Webserv
Loading...
Searching...
No Matches
ValidationUtils.hpp
Go to the documentation of this file.
1#pragma once
2#include "common/string.hpp"
3#include "config/Block.hpp"
7#include <string>
8
9namespace config {
10
16public:
21 static void checkArgs(ParsedDirectiveArgs const &args, size_t min, size_t max,
22 std::string const &name) {
23 if (args.size() < min || args.size() > max) {
24 std::string msg = "'" + name + "' directive requires ";
25 if (min == max) {
26 msg += "exactly " + utils::toString(min);
27 } else {
28 msg += "between " + utils::toString(min) + " and " + utils::toString(max);
29 }
30 msg += " argument(s).";
31 throw ConfigError(msg);
32 }
33 }
34
39 static void checkType(Token const &arg, TokenType expectedType, std::string const &name) {
40 if (arg.type != expectedType) {
41 throw ConfigError("'" + name + "' expected " + getTokenTypeName(expectedType) + ".");
42 }
43 }
44
49 static void checkContext(Block const &block, std::string const &allowedContexts,
50 std::string const &name) {
51 if (allowedContexts.find(block.name()) == std::string::npos) {
52 throw ConfigError("'" + name + "' directive is not allowed in: " + block.name());
53 }
54 }
55
56private:
57 static std::string getTokenTypeName(TokenType type) {
58 switch (type) {
59 case NUMBER:
60 return "NUMBER";
61 case IDENTIFIER:
62 return "IDENTIFIER";
63 case STRING:
64 return "STRING";
65 default:
66 return "value";
67 }
68 }
69};
70
71} // namespace config
Base class for configuration blocks like 'server' and 'location'.
Definition Block.hpp:15
std::string const & name() const
Gets the name of the block.
Definition Block.cpp:149
Definition ConfigException.hpp:26
Static utility class for common configuration validation tasks.
Definition ValidationUtils.hpp:15
static void checkType(Token const &arg, TokenType expectedType, std::string const &name)
Verifies that a specific argument matches the expected token type.
Definition ValidationUtils.hpp:39
static void checkArgs(ParsedDirectiveArgs const &args, size_t min, size_t max, std::string const &name)
Verifies that the number of arguments is within the inclusive range [min, max].
Definition ValidationUtils.hpp:21
static void checkContext(Block const &block, std::string const &allowedContexts, std::string const &name)
Verifies if the directive is being processed within an allowed context.
Definition ValidationUtils.hpp:49
static std::string getTokenTypeName(TokenType type)
Definition ValidationUtils.hpp:57
Definition ArgumentFactory.hpp:5
std::vector< Token > ParsedDirectiveArgs
Definition types.hpp:13
TokenType
Represents the type of a lexical token.
Definition Token.hpp:12
@ STRING
A quoted string literal, e.g., "hello world".
Definition Token.hpp:14
@ IDENTIFIER
A keyword or variable name, e.g., "server_name".
Definition Token.hpp:13
@ NUMBER
A numeric literal, e.g., 8080.
Definition Token.hpp:15
std::string toString(T const &v)
Definition string.hpp:7
Represents a single lexical token with a type and a literal value.
Definition Token.hpp:28
TokenType type
Definition Token.hpp:30