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

Validate field data types during collection creation.

kishorenc7d6a266934b8
diff --git a/include/field.h b/include/field.h
index b9263a24..279d9e1f 100644
--- a/include/field.h
+++ b/include/field.h
@@ -88,6 +88,10 @@ struct field {
type == field_types::INT64_ARRAY || type == field_types::BOOL_ARRAY);
}
+ bool has_valid_type() const {
+ return is_string() || is_integer() || is_float() || is_bool();
+ }
+
std::string faceted_name() const {
return (facet && !is_string()) ? "_fstr_" + name : name;
}
diff --git a/src/collection.cpp b/src/collection.cpp
index 55aff582..651d04e2 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1804,7 +1804,8 @@ Option<bool> Collection::parse_filter_query(const std::string& simple_filter_que
f = {field_name, {raw_value.substr(filter_value_index)}, str_comparator};
}
} else {
- return Option<bool>(400, "Error with filter field `" + _field.name + "`: Unidentified field type.");
+ return Option<bool>(400, "Error with filter field `" + _field.name +
+ "`: Unidentified field data type, see docs for supported data types.");
}
filters.push_back(f);
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 59ee29dc..dde7859b 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -283,6 +283,12 @@ Option<Collection*> CollectionManager::create_collection(const std::string& name
field_val[fields::optional] = field.optional;
fields_json.push_back(field_val);
+ if(!field.has_valid_type()) {
+ return Option<Collection*>(400, "Field `" + field.name +
+ "` has an invalid data type `" + field.type +
+ "`, see docs for supported data types.");
+ }
+
if(field.name == default_sorting_field && !(field.type == field_types::INT32 ||
field.type == field_types::INT64 ||
field.type == field_types::FLOAT)) {
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 9ad0cf8c..b61e2d12 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -2503,3 +2503,20 @@ TEST_F(CollectionTest, RemoveIfFound) {
collectionManager.drop_collection("coll1");
}
+
+TEST_F(CollectionTest, CreateCollectionInvalidFieldType) {
+ std::vector<field> fields = {field("title", "blah", true),
+ field("points", "int", false)};
+
+ std::vector<sort_by> sort_fields = {sort_by("points", "DESC")};
+
+ collectionManager.drop_collection("coll1");
+
+ auto create_op = collectionManager.create_collection("coll1", 4, fields, "points");
+
+ ASSERT_FALSE(create_op.ok());
+ ASSERT_STREQ("Field `title` has an invalid data type `blah`, see docs for supported data types.",
+ create_op.error().c_str());
+
+ collectionManager.drop_collection("coll1");
+}