diff --git a/docs/classdiagram.puml.in b/docs/classdiagram.puml.in index 077d3c3..2d92312 100644 --- a/docs/classdiagram.puml.in +++ b/docs/classdiagram.puml.in @@ -42,7 +42,7 @@ abstract class BaseValidator { +BaseValidator(ValidationError err_code) +BaseValidator(const vector& 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 +GetErrHeader() : string {query} @@ -69,7 +69,7 @@ class HeaderParamValidator { class JsonValidator { +JsonValidator(const Value& schema_val, const vector& 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 @@ -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 } class OASValidator { diff --git a/include/validators/base_validator.hpp b/include/validators/base_validator.hpp index b0d56fd..4b71639 100644 --- a/include/validators/base_validator.hpp +++ b/include/validators/base_validator.hpp @@ -18,7 +18,7 @@ class BaseValidator explicit BaseValidator(ValidationError err_code); explicit BaseValidator(const std::vector& 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; diff --git a/include/validators/json_validator.hpp b/include/validators/json_validator.hpp index aa7d468..05bb473 100644 --- a/include/validators/json_validator.hpp +++ b/include/validators/json_validator.hpp @@ -29,7 +29,7 @@ class JsonValidator: public BaseValidator public: JsonValidator(const rapidjson::Value& schema_val, const std::vector& 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; }; diff --git a/include/validators/method_validator.hpp b/include/validators/method_validator.hpp index 9000720..a103df1 100644 --- a/include/validators/method_validator.hpp +++ b/include/validators/method_validator.hpp @@ -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 kValidMethods; diff --git a/src/oas_validator_imp.cpp b/src/oas_validator_imp.cpp index 4dad5a8..4f37069 100644 --- a/src/oas_validator_imp.cpp +++ b/src/oas_validator_imp.cpp @@ -222,7 +222,7 @@ ValidationError OASValidatorImp::GetValidators(const std::string& method, const ValidatorsStore*& validators, std::string& error_msg, std::unordered_map* 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); diff --git a/src/validators/json_validator.cpp b/src/validators/json_validator.cpp index fd06825..f1b3cef 100644 --- a/src/validators/json_validator.cpp +++ b/src/validators/json_validator.cpp @@ -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()); diff --git a/src/validators/method_validator.cpp b/src/validators/method_validator.cpp index 158f6aa..4bb370e 100644 --- a/src/validators/method_validator.cpp +++ b/src/validators/method_validator.cpp @@ -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"("}})"; diff --git a/src/validators/param_validators.cpp b/src/validators/param_validators.cpp index 7501019..1849efb 100644 --- a/src/validators/param_validators.cpp +++ b/src/validators/param_validators.cpp @@ -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_; diff --git a/src/validators/validators_store.cpp b/src/validators/validators_store.cpp index 770dd7b..df544bc 100644 --- a/src/validators/validators_store.cpp +++ b/src/validators/validators_store.cpp @@ -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 } diff --git a/test/unittest/src/validators/body_validator.cpp b/test/unittest/src/validators/body_validator.cpp index 35f42c0..b9ffa48 100644 --- a/test/unittest/src/validators/body_validator.cpp +++ b/test/unittest/src/validators/body_validator.cpp @@ -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()); } @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); diff --git a/test/unittest/src/validators/method_validator.cpp b/test/unittest/src/validators/method_validator.cpp index 1f2038e..f87033d 100644 --- a/test/unittest/src/validators/method_validator.cpp +++ b/test/unittest/src/validators/method_validator.cpp @@ -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) @@ -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); } \ No newline at end of file