Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Add validation for collection name.

kishorenc4b2beba0d940
diff --git a/include/collection_manager.h b/include/collection_manager.h
index 23c74f06..58261887 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -128,9 +128,7 @@ public:
bool auth_key_matches(const std::string& auth_key_sent, const std::string& action,
const std::vector<std::string>& collections, std::map<std::string, std::string>& params) const;
- static Option<Collection*> create_collection(nlohmann::json& req_json,
- const size_t num_memory_shards,
- const std::string & default_sorting_field);
+ static Option<Collection*> create_collection(nlohmann::json& req_json);
Option<Collection*> create_collection(const std::string& name, const size_t num_memory_shards,
const std::vector<field> & fields,
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 02bedb2b..abeef898 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -757,9 +757,55 @@ nlohmann::json CollectionManager::get_collection_summaries() const {
return json_summaries;
}
-Option<Collection*> CollectionManager::create_collection(nlohmann::json& req_json,
- const size_t num_memory_shards,
- const std::string& default_sorting_field) {
+Option<Collection*> CollectionManager::create_collection(nlohmann::json& req_json) {
+ const char* NUM_MEMORY_SHARDS = "num_memory_shards";
+ const char* DEFAULT_SORTING_FIELD = "default_sorting_field";
+
+ // validate presence of mandatory fields
+
+ if(req_json.count("name") == 0) {
+ return Option<Collection*>(400, "Parameter `name` is required.");
+ }
+
+ if(!req_json["name"].is_string() || req_json["name"].get<std::string>().empty()) {
+ return Option<Collection*>(400, "Parameter `name` must be a non-empty string.");
+ }
+
+ if(req_json.count(NUM_MEMORY_SHARDS) == 0) {
+ req_json[NUM_MEMORY_SHARDS] = CollectionManager::DEFAULT_NUM_MEMORY_SHARDS;
+ }
+
+ if(req_json.count("fields") == 0) {
+ return Option<Collection*>(400, "Parameter `fields` is required.");
+ }
+
+ if(req_json.count(DEFAULT_SORTING_FIELD) == 0) {
+ req_json[DEFAULT_SORTING_FIELD] = "";
+ }
+
+ if(!req_json[DEFAULT_SORTING_FIELD].is_string()) {
+ return Option<Collection*>(400, std::string("`") + DEFAULT_SORTING_FIELD +
+ "` should be a string. It should be the name of an int32/float field.");
+ }
+
+ if(!req_json[NUM_MEMORY_SHARDS].is_number_unsigned()) {
+ return Option<Collection*>(400, std::string("`") + NUM_MEMORY_SHARDS + "` should be a positive integer.");
+ }
+
+ size_t num_memory_shards = req_json[NUM_MEMORY_SHARDS].get<size_t>();
+ if(num_memory_shards == 0) {
+ return Option<Collection*>(400, std::string("`") + NUM_MEMORY_SHARDS + "` should be a positive integer.");
+ }
+
+ // field specific validation
+
+ if(!req_json["fields"].is_array() || req_json["fields"].empty()) {
+ return Option<Collection *>(400, "The `fields` value should be an array of objects containing "
+ "`name`, `type` and optionally, `facet` properties.");
+ }
+
+ const std::string& default_sorting_field = req_json[DEFAULT_SORTING_FIELD].get<std::string>();
+
std::string fallback_field_type;
std::vector<field> fields;
auto parse_op = field::json_fields_to_fields(req_json["fields"], fallback_field_type, fields);
diff --git a/src/core_api.cpp b/src/core_api.cpp
index adb8f1d3..9b7c32c9 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -74,8 +74,6 @@ bool get_collections(const std::shared_ptr<http_req>& req, const std::shared_ptr
}
bool post_create_collection(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
- const char* NUM_MEMORY_SHARDS = "num_memory_shards";
-
nlohmann::json req_json;
try {
@@ -87,62 +85,7 @@ bool post_create_collection(const std::shared_ptr<http_req>& req, const std::sha
}
CollectionManager & collectionManager = CollectionManager::get_instance();
-
- // validate presence of mandatory fields
-
- if(req_json.count("name") == 0) {
- res->set_400("Parameter `name` is required.");
- return false;
- }
-
- if(req_json.count(NUM_MEMORY_SHARDS) == 0) {
- req_json[NUM_MEMORY_SHARDS] = CollectionManager::DEFAULT_NUM_MEMORY_SHARDS;
- }
-
- if(req_json.count("fields") == 0) {
- res->set_400("Parameter `fields` is required.");
- return false;
- }
-
- const char* DEFAULT_SORTING_FIELD = "default_sorting_field";
-
- if(req_json.count(DEFAULT_SORTING_FIELD) == 0) {
- req_json[DEFAULT_SORTING_FIELD] = "";
- }
-
- if(!req_json[DEFAULT_SORTING_FIELD].is_string()) {
- res->set_400(std::string("`") + DEFAULT_SORTING_FIELD +
- "` should be a string. It should be the name of an int32/float field.");
- return false;
- }
-
- if(!req_json[NUM_MEMORY_SHARDS].is_number_unsigned()) {
- res->set_400(std::string("`") + NUM_MEMORY_SHARDS + "` should be a positive integer.");
- return false;
- }
-
- size_t num_memory_shards = req_json[NUM_MEMORY_SHARDS].get<size_t>();
- if(num_memory_shards == 0) {
- res->set_400(std::string("`") + NUM_MEMORY_SHARDS + "` should be a positive integer.");
- return false;
- }
-
- if(collectionManager.get_collection(req_json["name"]) != nullptr) {
- res->set_409("Collection with name `" + req_json["name"].get<std::string>() + "` already exists.");
- return false;
- }
-
- // field specific validation
-
- if(!req_json["fields"].is_array() || req_json["fields"].empty()) {
- res->set_400("The `fields` value should be an array of objects containing "
- "`name`, `type` and optionally, `facet` properties.");
- return false;
- }
-
- const std::string & default_sorting_field = req_json[DEFAULT_SORTING_FIELD].get<std::string>();
- const Option<Collection*> & collection_op =
- collectionManager.create_collection(req_json, num_memory_shards, default_sorting_field);
+ const Option<Collection*> & collection_op = collectionManager.create_collection(req_json);
if(collection_op.ok()) {
nlohmann::json json_response = collection_op.get()->get_summary_json();