Browse Source

send back 403 with validation errors

master
Jørgen Sverre Lien Sellæg 5 years ago
parent
commit
8b930c7480
  1. 2
      include/data.hpp
  2. 32
      src/data.cpp
  3. 10
      src/store.cpp

2
include/data.hpp

@ -8,9 +8,9 @@ class data : public json {
fs::path &get_data_path(); fs::path &get_data_path();
void create_data_directory(); void create_data_directory();
fs::path get_store_path(); fs::path get_store_path();
json validate(const json &);
public: public:
data(); data();
json process(const json &); json process(const json &);
json validate(const json &);
}; };

32
src/data.cpp

@ -42,14 +42,7 @@ void data::create_data_directory() {
fs::path data::get_store_path() { return get_data_path() / "store.json"; } fs::path data::get_store_path() { return get_data_path() / "store.json"; }
json data::process(const json &input) { json data::process(const json &input) { return input; }
const auto errors = validate(input);
if (errors.size() != 0) {
return errors;
}
return input;
}
json data::validate(const json &input) { json data::validate(const json &input) {
const std::string name = input.value("name", ""); const std::string name = input.value("name", "");
@ -60,16 +53,31 @@ json data::validate(const json &input) {
const std::unordered_map<std::string, std::string> strings{ const std::unordered_map<std::string, std::string> strings{
{"name", name}, {"address", address}, {"phone", phone}, {"email", email}}; {"name", name}, {"address", address}, {"phone", phone}, {"email", email}};
json errors = json::array(); json errors = json::object({
{"name", json::array()},
{"address", json::array()},
{"phone", json::array()},
{"email", json::array()},
});
int count = 0;
const auto add_error = [&](json &field, const std::string &str) {
count++;
field.push_back(str);
};
for (const auto &f : strings) { for (const auto &f : strings) {
auto &field = errors[f.first];
if (f.second.size() == 0) { if (f.second.size() == 0) {
errors.push_back({{f.first, "Missing name."}}); add_error(field, "is missing from form.");
} }
if (f.second.size() >= 255) { if (f.second.size() >= 255) {
errors.push_back({{f.first, "Name is to long."}}); add_error(field, "to long to process. 254+");
} }
} }
return errors; return json{{"errors", errors}, {"count", count}};
} }

10
src/store.cpp

@ -78,12 +78,14 @@ int Application::run() {
return response::not_found(response); return response::not_found(response);
} }
const auto json_response = dat.process(data); const auto errors = dat.validate(data);
int error_count = errors["count"];
if (json_response.is_array()) { if (error_count != 0) {
return response::bad_request(response, json_response); return response::bad_request(response, errors["errors"]);
} }
const auto json_response = dat.process(data);
response->write(Status::success_created, json_response.dump(), response->write(Status::success_created, json_response.dump(),
{header_access_control, header_application_data}); {header_access_control, header_application_data});
}; };

Loading…
Cancel
Save