Browse Source

add error messages to missing fields

master
Jørgen Sverre Lien Sellæg 5 years ago
parent
commit
eed4bfac67
  1. 2
      include/data.hpp
  2. 5
      include/http.hpp
  3. 15
      src/data.cpp
  4. 7
      src/http.cpp
  5. 4
      src/store.cpp

2
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();

5
include/http.hpp

@ -1,4 +1,5 @@
#pragma once
#include <json.hpp>
#include <web_server.hpp>
const auto header_application_data =
@ -10,6 +11,10 @@ class response {
public:
static void bad_request(std::shared_ptr<HttpServer::Response> response);
static void bad_request(std::shared_ptr<HttpsServer::Response> response);
static void bad_request(std::shared_ptr<HttpServer::Response> response,
const json &);
static void bad_request(std::shared_ptr<HttpsServer::Response> response,
const json &);
static void not_found(std::shared_ptr<HttpServer::Response> response);
static void not_found(std::shared_ptr<HttpsServer::Response> response);
};

15
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<std::string, std::string> 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;
}

7
src/http.cpp

@ -1,5 +1,4 @@
#include <http.hpp>
#include <json.hpp>
void response::bad_request(std::shared_ptr<HttpServer::Response> response) {
return response->write(Status::client_error_bad_request);
@ -9,6 +8,12 @@ void response::bad_request(std::shared_ptr<HttpsServer::Response> response) {
return response->write(Status::client_error_bad_request);
}
void response::bad_request(std::shared_ptr<HttpServer::Response> 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<HttpsServer::Response> response) {
return response->write(Status::client_error_not_found);
}

4
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(),

Loading…
Cancel
Save