Skip to content

Commit

Permalink
Renamed method ValidateJson ro ValidateJson to make it general
Browse files Browse the repository at this point in the history
  • Loading branch information
nawaz1991 committed Jun 15, 2024
1 parent 0b7642f commit f58ab8f
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 48 deletions.
6 changes: 3 additions & 3 deletions docs/classdiagram.puml.in
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ abstract class BaseValidator {
+BaseValidator(ValidationError err_code)
+BaseValidator(const vector<string>& ref_keys, ValidationError err_code)
+~BaseValidator()
+{abstract} ValidateJson(const string& content, string& err_msg) : ValidationError
+{abstract} Validate(const string& content, string& err_msg) : ValidationError
#code_on_error_ : ValidationError
-{static} kErrHeaders : static const unordered_map<ValidationError, string>
+GetErrHeader() : string {query}
Expand All @@ -69,7 +69,7 @@ class HeaderParamValidator {
class JsonValidator {
+JsonValidator(const Value& schema_val, const vector<string>& ref_keys, ValidationError err_code)
+~JsonValidator()
+ValidateJson(const string& json_str, string& error_msg) : ValidationError
+Validate(const string& json_str, string& error_msg) : ValidationError
-schema_ : SchemaDocument*
-validator_ : SchemaValidator*
-mutex_ : mutex
Expand All @@ -79,7 +79,7 @@ class JsonValidator {
}
class MethodValidator {
+MethodValidator()
+ValidateJson(const string& method, string& err_msg) : ValidationError
+Validate(const string& method, string& err_msg) : ValidationError
-{static} kValidMethods : static const unordered_set<string>
}
class OASValidator {
Expand Down
2 changes: 1 addition & 1 deletion include/validators/base_validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BaseValidator
explicit BaseValidator(ValidationError err_code);
explicit BaseValidator(const std::vector<std::string>& ref_keys, ValidationError err_code);

virtual ValidationError ValidateJson(const std::string& content, std::string& err_msg) = 0;
virtual ValidationError Validate(const std::string& content, std::string& err_msg) = 0;
std::string GetErrHeader() const;
virtual ~BaseValidator() = default;

Expand Down
2 changes: 1 addition & 1 deletion include/validators/json_validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class JsonValidator: public BaseValidator
public:
JsonValidator(const rapidjson::Value& schema_val, const std::vector<std::string>& ref_keys,
ValidationError err_code);
ValidationError ValidateJson(const std::string& json_str, std::string& error_msg) override;
ValidationError Validate(const std::string& json_str, std::string& error_msg) override;
~JsonValidator() override;
};

Expand Down
2 changes: 1 addition & 1 deletion include/validators/method_validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MethodValidator: public BaseValidator
{
public:
MethodValidator();
ValidationError ValidateJson(const std::string& method, std::string& err_msg) override;
ValidationError Validate(const std::string& method, std::string& err_msg) override;

private:
static const std::unordered_set<std::string> kValidMethods;
Expand Down
2 changes: 1 addition & 1 deletion src/oas_validator_imp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ ValidationError OASValidatorImp::GetValidators(const std::string& method, const
ValidatorsStore*& validators, std::string& error_msg,
std::unordered_map<size_t, ParamRange>* param_idxs, std::string* query)
{
auto err_code = method_validator_.ValidateJson(method, error_msg);
auto err_code = method_validator_.Validate(method, error_msg);
CHECK_ERROR(err_code)

auto enum_method = kStringToMethod.at(method);
Expand Down
2 changes: 1 addition & 1 deletion src/validators/json_validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ JsonValidator::JsonValidator(const rapidjson::Value& schema_val, const std::vect
}
}

ValidationError JsonValidator::ValidateJson(const std::string& json_str, std::string& error_msg)
ValidationError JsonValidator::Validate(const std::string& json_str, std::string& error_msg)
{
rapidjson::Document doc;
doc.Parse(json_str.c_str());
Expand Down
2 changes: 1 addition & 1 deletion src/validators/method_validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ MethodValidator::MethodValidator()
{
}

ValidationError MethodValidator::ValidateJson(const std::string& method, std::string& err_msg)
ValidationError MethodValidator::Validate(const std::string& method, std::string& err_msg)
{
if (kValidMethods.find(method) == kValidMethods.end()) {
err_msg += err_header_ + R"("description": "Invalid HTTP method ')" + method + "'" + R"("}})";
Expand Down
2 changes: 1 addition & 1 deletion src/validators/param_validators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ ValidationError ParamValidator::ValidateParam(const char* beg, const char* end,
{
try {
auto ret = deserializer_->Deserialize(beg, end);
return JsonValidator::ValidateJson(ret, error_msg);
return JsonValidator::Validate(ret, error_msg);
} catch (const DeserializationException& exc) {
error_msg = err_header_ + exc.what() + "}}";
return code_on_error_;
Expand Down
2 changes: 1 addition & 1 deletion src/validators/validators_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void ValidatorsStore::AddParamValidators(const std::string& path, const rapidjso
ValidationError ValidatorsStore::ValidateBody(const std::string& json_body, std::string& error_msg)
{
if (body_validator_) {
return body_validator_->ValidateJson(json_body, error_msg);
return body_validator_->Validate(json_body, error_msg);
}
return ValidationError::NONE; // No validator, no error
}
Expand Down
20 changes: 10 additions & 10 deletions test/unittest/src/validators/body_validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ TEST_F(TestBodyValidator, ValidJson)
"S_example": "This is a pattern property"
})";
std::string error_msg;
EXPECT_EQ(validator_->ValidateJson(json_str, error_msg), ValidationError::NONE);
EXPECT_EQ(validator_->Validate(json_str, error_msg), ValidationError::NONE);
EXPECT_TRUE(error_msg.empty());
}

Expand All @@ -175,7 +175,7 @@ TEST_F(TestBodyValidator, InvalidParsing)
"S_example": "This is a pattern property"
})";
std::string error_msg;
EXPECT_EQ(validator_->ValidateJson(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_EQ(validator_->Validate(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_FALSE(error_msg.empty());
rapidjson::Document doc;
doc.Parse(error_msg.c_str());
Expand Down Expand Up @@ -206,7 +206,7 @@ TEST_F(TestBodyValidator, InvalidIntegerType)
"S_example": "This is a pattern property"
})";
std::string error_msg;
EXPECT_EQ(validator_->ValidateJson(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_EQ(validator_->Validate(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_FALSE(error_msg.empty());
rapidjson::Document doc;
doc.Parse(error_msg.c_str());
Expand Down Expand Up @@ -239,7 +239,7 @@ TEST_F(TestBodyValidator, InvalidStringType)
"S_example": "This is a pattern property"
})";
std::string error_msg;
EXPECT_EQ(validator_->ValidateJson(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_EQ(validator_->Validate(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_FALSE(error_msg.empty());
rapidjson::Document doc;
doc.Parse(error_msg.c_str());
Expand Down Expand Up @@ -272,7 +272,7 @@ TEST_F(TestBodyValidator, InvalidArrayUniqueItems)
"S_example": "This is a pattern property"
})";
std::string error_msg;
EXPECT_EQ(validator_->ValidateJson(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_EQ(validator_->Validate(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_FALSE(error_msg.empty());
rapidjson::Document doc;
doc.Parse(error_msg.c_str());
Expand Down Expand Up @@ -305,7 +305,7 @@ TEST_F(TestBodyValidator, InvalidIntegerMinimum)
"S_example": "This is a pattern property"
})";
std::string error_msg;
EXPECT_EQ(validator_->ValidateJson(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_EQ(validator_->Validate(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_FALSE(error_msg.empty());
rapidjson::Document doc;
doc.Parse(error_msg.c_str());
Expand Down Expand Up @@ -338,7 +338,7 @@ TEST_F(TestBodyValidator, InvalidIntegerMaximim)
"S_example": "This is a pattern property"
})";
std::string error_msg;
EXPECT_EQ(validator_->ValidateJson(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_EQ(validator_->Validate(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_FALSE(error_msg.empty());
rapidjson::Document doc;
doc.Parse(error_msg.c_str());
Expand Down Expand Up @@ -369,7 +369,7 @@ TEST_F(TestBodyValidator, InvalidRequired)
"S_example": "This is a pattern property"
})";
std::string error_msg;
EXPECT_EQ(validator_->ValidateJson(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_EQ(validator_->Validate(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_FALSE(error_msg.empty());
rapidjson::Document doc;
doc.Parse(error_msg.c_str());
Expand Down Expand Up @@ -404,7 +404,7 @@ TEST_F(TestBodyValidator, InvalidPatternProp)
"T_example": "Another invalid property"
})";
std::string error_msg;
EXPECT_EQ(validator_->ValidateJson(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_EQ(validator_->Validate(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_FALSE(error_msg.empty());
rapidjson::Document doc;
doc.Parse(error_msg.c_str());
Expand Down Expand Up @@ -439,7 +439,7 @@ TEST_F(TestBodyValidator, InvalidEnum)
"T_example": "Another invalid property"
})";
std::string error_msg;
EXPECT_EQ(validator_->ValidateJson(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_EQ(validator_->Validate(json_str, error_msg), ValidationError::INVALID_BODY);
EXPECT_FALSE(error_msg.empty());
rapidjson::Document doc;
doc.Parse(error_msg.c_str());
Expand Down
54 changes: 27 additions & 27 deletions test/unittest/src/validators/method_validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ TEST(MethodValidatorTest, Positive)
MethodValidator validator;
std::string err_msg;

EXPECT_EQ(validator.ValidateJson("GET", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("POST", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("PUT", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("DELETE", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("HEAD", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("OPTIONS", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("TRACE", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("CONNECT", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("PATCH", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("GET", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("POST", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("PUT", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("DELETE", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("HEAD", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("OPTIONS", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("TRACE", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("CONNECT", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("PATCH", err_msg), ValidationError::NONE);

EXPECT_EQ(validator.ValidateJson("get", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("post", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("put", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("delete", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("head", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("options", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("trace", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("connect", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.ValidateJson("patch", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("get", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("post", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("put", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("delete", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("head", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("options", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("trace", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("connect", err_msg), ValidationError::NONE);
EXPECT_EQ(validator.Validate("patch", err_msg), ValidationError::NONE);
}

TEST(MethodValidatorTest, Negative)
Expand All @@ -39,15 +39,15 @@ TEST(MethodValidatorTest, Negative)
std::string err_msg;
rapidjson::Document doc;

EXPECT_EQ(validator.ValidateJson("GETT", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.Validate("GETT", err_msg), ValidationError::INVALID_METHOD);
doc.Parse(err_msg.c_str());
EXPECT_FALSE(doc.HasParseError());
EXPECT_EQ(validator.ValidateJson("POSTT", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.ValidateJson("PUTT", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.ValidateJson("DELETET", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.ValidateJson("HEADT", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.ValidateJson("OPTIONST", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.ValidateJson("TRACET", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.ValidateJson("CONNECTT", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.ValidateJson("PATCHT", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.Validate("POSTT", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.Validate("PUTT", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.Validate("DELETET", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.Validate("HEADT", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.Validate("OPTIONST", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.Validate("TRACET", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.Validate("CONNECTT", err_msg), ValidationError::INVALID_METHOD);
EXPECT_EQ(validator.Validate("PATCHT", err_msg), ValidationError::INVALID_METHOD);
}

0 comments on commit f58ab8f

Please sign in to comment.