From eed4bfac6791e2218c1534e21b689912897fa59d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Sverre=20Lien=20Sell=C3=A6g?= Date: Sun, 23 May 2021 16:45:12 +0200 Subject: [PATCH] add error messages to missing fields --- include/data.hpp | 2 +- include/http.hpp | 5 +++++ src/data.cpp | 15 +++++++++------ src/http.cpp | 7 ++++++- src/store.cpp | 4 ++-- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/include/data.hpp b/include/data.hpp index 1219826..77e9ff4 100644 --- a/include/data.hpp +++ b/include/data.hpp @@ -8,7 +8,7 @@ class data : public json { fs::path &get_data_path(); void create_data_directory(); fs::path get_store_path(); - bool validate(const json &); + json validate(const json &); public: data(); diff --git a/include/http.hpp b/include/http.hpp index 6dd5632..c8f1b5b 100644 --- a/include/http.hpp +++ b/include/http.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include const auto header_application_data = @@ -10,6 +11,10 @@ class response { public: static void bad_request(std::shared_ptr response); static void bad_request(std::shared_ptr response); + static void bad_request(std::shared_ptr response, + const json &); + static void bad_request(std::shared_ptr response, + const json &); static void not_found(std::shared_ptr response); static void not_found(std::shared_ptr response); }; \ No newline at end of file diff --git a/src/data.cpp b/src/data.cpp index caa6e68..eb139fa 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -43,14 +43,15 @@ void data::create_data_directory() { fs::path data::get_store_path() { return get_data_path() / "store.json"; } json data::process(const json &input) { - if (!validate(input)) { - return nullptr; + const auto errors = validate(input); + if (errors.size() != 0) { + return errors; } return input; } -bool data::validate(const json &input) { +json data::validate(const json &input) { const std::string name = input.value("name", ""); const std::string address = input.value("address", ""); const std::string phone = input.value("phone", ""); @@ -59,14 +60,16 @@ bool data::validate(const json &input) { const std::unordered_map strings{ {"name", name}, {"address", address}, {"phone", phone}, {"email", email}}; + json errors = json::array(); + for (const auto &f : strings) { if (f.second.size() == 0) { - return false; + errors.push_back({{f.first, "Missing name."}}); } if (f.second.size() >= 255) { - return false; + errors.push_back({{f.first, "Name is to long."}}); } } - return true; + return errors; } diff --git a/src/http.cpp b/src/http.cpp index cef5c55..2b92be8 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -1,5 +1,4 @@ #include -#include void response::bad_request(std::shared_ptr response) { return response->write(Status::client_error_bad_request); @@ -9,6 +8,12 @@ void response::bad_request(std::shared_ptr response) { return response->write(Status::client_error_bad_request); } +void response::bad_request(std::shared_ptr response, + const json &json) { + return response->write(Status::client_error_bad_request, json.dump(), + {header_access_control, header_application_data}); +} + void response::not_found(std::shared_ptr response) { return response->write(Status::client_error_not_found); } diff --git a/src/store.cpp b/src/store.cpp index 378d387..f5a8ebc 100644 --- a/src/store.cpp +++ b/src/store.cpp @@ -80,8 +80,8 @@ int Application::run() { const auto json_response = dat.process(data); - if (json_response.is_null()) { - return response::bad_request(response); + if (json_response.is_array()) { + return response::bad_request(response, json_response); } response->write(Status::success_created, json_response.dump(),