|
|
|
|
@ -42,14 +42,7 @@ void data::create_data_directory() {
|
|
|
|
|
|
|
|
|
|
fs::path data::get_store_path() { return get_data_path() / "store.json"; } |
|
|
|
|
|
|
|
|
|
json data::process(const json &input) { |
|
|
|
|
const auto errors = validate(input); |
|
|
|
|
if (errors.size() != 0) { |
|
|
|
|
return errors; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return input; |
|
|
|
|
} |
|
|
|
|
json data::process(const json &input) { return input; } |
|
|
|
|
|
|
|
|
|
json data::validate(const json &input) { |
|
|
|
|
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{ |
|
|
|
|
{"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) { |
|
|
|
|
auto &field = errors[f.first]; |
|
|
|
|
|
|
|
|
|
if (f.second.size() == 0) { |
|
|
|
|
errors.push_back({{f.first, "Missing name."}}); |
|
|
|
|
add_error(field, "is missing from form."); |
|
|
|
|
} |
|
|
|
|
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}}; |
|
|
|
|
} |
|
|
|
|
|