uid_t uid, rpc::ActionModel& action) {
auto db = std::make_unique<action::SqliteDb>();
std::string schema_json = db->GetAction(action.Getaction_id());
- std::string error;
-
- if (!ActionValidator().CheckModelValid(schema_json, action.Getjson(),
- error)) {
- LOG(ERROR) << "Invalid model. error: " << error;
+ if (!ActionValidator().CheckModelValid(schema_json, action.Getjson()))
return;
- }
common::ActionSchema schema(schema_json);
LOG(DEBUG) << "OnExecute action : " << schema.GetActionId();
#include <rapidjson/error/en.h>
#include <rapidjson/schema.h>
+#include "common/utils/logging.hh"
+#include "common/utils/safe_json.hpp"
+
namespace action {
+
bool ActionValidator::CheckSchemaValid(const std::string& schemajson) const {
// TODO: implement this.
return false;
}
bool ActionValidator::CheckModelValid(const std::string& schemajson,
- const std::string& modeljson,
- std::string& errMsg) const {
- // TODO: implement this.
+ const std::string& modeljson) const {
+ try {
+ common::SafeJson schema(schemajson);
+ common::SafeJson model(modeljson);
+
+ for (auto& [key, item] : schema.members("params")) {
+ std::string type = common::SafeJson::get<std::string>(*item, "type");
+ bool isRequired = false;
+ try {
+ isRequired = common::SafeJson::get<bool>(*item, "isRequired");
+ } catch (const std::runtime_error& e) {
+ isRequired = false;
+ }
+
+ try {
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-variable"
+ if (type == "int") {
+ int value = model.get<int>({"params", key});
+ } else if (type == "string") {
+ std::string value = model.get<std::string>({"params", key});
+ } else if (type == "bool") { // TODO: change to boolean
+ bool value = model.get<bool>({"params", key});
+ } else if (type == "array") {
+ auto value = model.array({"params", key});
+ } else if (type == "object") {
+ auto value = model.members({"params", key});
+ } else if (type == "double") { // TODO: change to number
+ double value = model.get<double>({"params", key});
+ } else {
+ LOG(ERROR) << "Unknown type: " << type;
+ return false;
+ }
+#pragma GCC diagnostic pop
+ } catch (const std::runtime_error& e) {
+ if (isRequired) {
+ LOG(ERROR) << e.what();
+ return false;
+ }
+ }
+ }
+ } catch (const std::runtime_error& e) {
+ LOG(ERROR) << e.what();
+ return false;
+ }
+
return true;
}
ActionSchema::ActionSchema() {}
-#include <iostream>
ActionSchema::ActionSchema(const std::string& json_str) {
try {
SafeJson root(json_str);
action_id_ = root.get<std::string>("name");
appid_ = root.get<std::string>("appId"); // TODO: Change to details::appId
- category_ = root.get<std::string>("category");
+ try {
+ category_ = root.get<std::string>("category");
+ } catch (...) {
+ }
description_ = root.get<std::string>("desc");
type_ = GetActionTypeFromString(root.get<std::string>("type"));
// params
for (auto const& it : root.array("required-privileges"))
privileges_.push_back(it->GetString());
- } catch (...) {
+ } catch (const std::runtime_error& e) {
// TODO: how to handle invalid json case?
- LOG(ERROR) << "Failed to parse json string";
+ LOG(ERROR) << "Failed to parse json string: " << e.what();
+ LOG(ERROR) << json_str;
}
}