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

Improve error message for nested array object numerical field.

Kishore Nallanedf2d658ca92
diff --git a/src/validator.cpp b/src/validator.cpp
index f66eaf9d..844ef3bf 100644
--- a/src/validator.cpp
+++ b/src/validator.cpp
@@ -159,11 +159,19 @@ Option<uint32_t> validator_t::coerce_string(const DIRTY_VALUES& dirty_values, co
auto& item = is_array ? array_iter.value() : document[field_name];
if(dirty_values == DIRTY_VALUES::REJECT) {
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " string.");
}
if(dirty_values == DIRTY_VALUES::DROP) {
if(!a_field.optional) {
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " string.");
}
@@ -195,7 +203,7 @@ Option<uint32_t> validator_t::coerce_string(const DIRTY_VALUES& dirty_values, co
if(!a_field.optional) {
if(a_field.nested && item.is_array()) {
return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
- "Hint: field inside an array of objects must be an array type as well.");
+ "Hint: field inside an array of objects must be an array type as well.");
}
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " string.");
}
@@ -210,7 +218,7 @@ Option<uint32_t> validator_t::coerce_string(const DIRTY_VALUES& dirty_values, co
// COERCE_OR_REJECT / non-optional + DROP
if(a_field.nested && item.is_array()) {
return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
- "Hint: field inside an array of objects must be an array type as well.");
+ "Hint: field inside an array of objects must be an array type as well.");
}
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " string.");
}
@@ -226,11 +234,19 @@ Option<uint32_t> validator_t::coerce_int32_t(const DIRTY_VALUES& dirty_values, c
auto& item = is_array ? array_iter.value() : document[field_name];
if(dirty_values == DIRTY_VALUES::REJECT) {
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " int32.");
}
if(dirty_values == DIRTY_VALUES::DROP) {
if(!a_field.optional) {
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " int32.");
}
@@ -261,6 +277,10 @@ Option<uint32_t> validator_t::coerce_int32_t(const DIRTY_VALUES& dirty_values, c
else {
if(dirty_values == DIRTY_VALUES::COERCE_OR_DROP) {
if(!a_field.optional) {
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " int32.");
}
@@ -272,6 +292,10 @@ Option<uint32_t> validator_t::coerce_int32_t(const DIRTY_VALUES& dirty_values, c
}
} else {
// COERCE_OR_REJECT / non-optional + DROP
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " int32.");
}
}
@@ -294,11 +318,19 @@ Option<uint32_t> validator_t::coerce_int64_t(const DIRTY_VALUES& dirty_values, c
auto& item = is_array ? array_iter.value() : document[field_name];
if(dirty_values == DIRTY_VALUES::REJECT) {
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " int64.");
}
if(dirty_values == DIRTY_VALUES::DROP) {
if(!a_field.optional) {
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " int64.");
}
@@ -328,6 +360,10 @@ Option<uint32_t> validator_t::coerce_int64_t(const DIRTY_VALUES& dirty_values, c
else {
if(dirty_values == DIRTY_VALUES::COERCE_OR_DROP) {
if(!a_field.optional) {
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " int64.");
}
@@ -339,6 +375,10 @@ Option<uint32_t> validator_t::coerce_int64_t(const DIRTY_VALUES& dirty_values, c
}
} else {
// COERCE_OR_REJECT / non-optional + DROP
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " int64.");
}
}
@@ -353,11 +393,19 @@ Option<uint32_t> validator_t::coerce_bool(const DIRTY_VALUES& dirty_values, cons
auto& item = is_array ? array_iter.value() : document[field_name];
if(dirty_values == DIRTY_VALUES::REJECT) {
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " bool.");
}
if(dirty_values == DIRTY_VALUES::DROP) {
if(!a_field.optional) {
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " bool.");
}
@@ -393,6 +441,10 @@ Option<uint32_t> validator_t::coerce_bool(const DIRTY_VALUES& dirty_values, cons
else {
if(dirty_values == DIRTY_VALUES::COERCE_OR_DROP) {
if(!a_field.optional) {
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " bool.");
}
@@ -404,6 +456,10 @@ Option<uint32_t> validator_t::coerce_bool(const DIRTY_VALUES& dirty_values, cons
}
} else {
// COERCE_OR_REJECT / non-optional + DROP
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " bool.");
}
}
@@ -478,11 +534,19 @@ Option<uint32_t> validator_t::coerce_float(const DIRTY_VALUES& dirty_values, con
auto& item = is_array ? array_iter.value() : document[field_name];
if(dirty_values == DIRTY_VALUES::REJECT) {
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " float.");
}
if(dirty_values == DIRTY_VALUES::DROP) {
if(!a_field.optional) {
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " float.");
}
@@ -508,6 +572,10 @@ Option<uint32_t> validator_t::coerce_float(const DIRTY_VALUES& dirty_values, con
else {
if(dirty_values == DIRTY_VALUES::COERCE_OR_DROP) {
if(!a_field.optional) {
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " float.");
}
@@ -519,6 +587,10 @@ Option<uint32_t> validator_t::coerce_float(const DIRTY_VALUES& dirty_values, con
}
} else {
// COERCE_OR_REJECT / non-optional + DROP
+ if(a_field.nested && item.is_array()) {
+ return Option<>(400, "Field `" + field_name + "` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.");
+ }
return Option<>(400, "Field `" + field_name + "` must be " + suffix + " float.");
}
}
diff --git a/test/collection_nested_fields_test.cpp b/test/collection_nested_fields_test.cpp
index e50cf0e2..4614aa1d 100644
--- a/test/collection_nested_fields_test.cpp
+++ b/test/collection_nested_fields_test.cpp
@@ -2542,8 +2542,9 @@ TEST_F(CollectionNestedFieldsTest, HighlightArrayOfObjects) {
nlohmann::json schema = R"({
"name": "coll1",
"enable_nested_fields": true,
- "fields": [
- {"name": ".*", "type": "auto"}
+ "fields": [
+ {"name": "variants", "type": "object[]", "facet": true, "index": true},
+ {"name": "variants.sellingPrice", "type": "int32", "facet": true}
]
})"_json;
@@ -2552,45 +2553,83 @@ TEST_F(CollectionNestedFieldsTest, HighlightArrayOfObjects) {
Collection* coll1 = op.get();
auto doc1 = R"({
- "details": [
- {"foo": "John Smith"},
- {"name": "James Peterson"},
- {"bar": "John Galt"}
- ]
+ "variants": [
+ {
+ "sellingPrice": 2300,
+ "timestamp": 10000,
+ "is_deleted": false,
+ "price": 50.50
+ },
+ {
+ "sellingPrice": 1200,
+ "timestamp": 10000,
+ "is_deleted": false,
+ "price": 150.50
+ }
+ ]
+
})"_json;
auto add_op = coll1->add(doc1.dump(), CREATE);
- ASSERT_TRUE(add_op.ok());
+ ASSERT_FALSE(add_op.ok());
+ ASSERT_EQ("Field `variants.sellingPrice` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.", add_op.error());
- auto results = coll1->search("james", {"details.name"}, "", {}, {}, {0}, 10, 1, FREQUENCY,
- {true}, 1, spp::sparse_hash_set<std::string>(),
- spp::sparse_hash_set<std::string>(), 10, "", 30, 4).get();
- ASSERT_EQ(1, results["found"].get<size_t>());
- ASSERT_EQ(3, results["hits"][0]["highlight"]["details"].size());
- ASSERT_EQ(0, results["hits"][0]["highlight"]["details"][0].size());
- ASSERT_EQ(1, results["hits"][0]["highlight"]["details"][1].size());
- ASSERT_EQ(0, results["hits"][0]["highlight"]["details"][2].size());
+ schema = R"({
+ "name": "coll2",
+ "enable_nested_fields": true,
+ "fields": [
+ {"name": "variants", "type": "object[]", "facet": true, "index": true},
+ {"name": "variants.timestamp", "type": "int64", "facet": true}
+ ]
+ })"_json;
- results = coll1->search("james", {"details.name"}, "", {}, {}, {0}, 10, 1, FREQUENCY,
- {true}, 1, spp::sparse_hash_set<std::string>(),
- spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 1, {}, {}, {}, 0,
- "<mark>", "</mark>", {1}, 10000, true, false, true, "details.name").get();
+ op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll2 = op.get();
- ASSERT_EQ(1, results["found"].get<size_t>());
- ASSERT_EQ(0, results["hits"][0]["highlight"]["details"][0].size());
- ASSERT_EQ(1, results["hits"][0]["highlight"]["details"][1].size());
- ASSERT_EQ(0, results["hits"][0]["highlight"]["details"][2].size());
+ add_op = coll2->add(doc1.dump(), CREATE);
+ ASSERT_FALSE(add_op.ok());
+ ASSERT_EQ("Field `variants.timestamp` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.", add_op.error());
- results = coll1->search("james", {"details.name"}, "", {}, {}, {0}, 10, 1, FREQUENCY,
- {true}, 1, spp::sparse_hash_set<std::string>(),
- spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 1, {}, {}, {}, 0,
- "<mark>", "</mark>", {1}, 10000, true, false, true, "details").get();
+ schema = R"({
+ "name": "coll3",
+ "enable_nested_fields": true,
+ "fields": [
+ {"name": "variants", "type": "object[]", "facet": true, "index": true},
+ {"name": "variants.is_deleted", "type": "bool", "facet": true}
+ ]
+ })"_json;
- ASSERT_EQ(1, results["found"].get<size_t>());
- ASSERT_EQ(3, results["hits"][0]["highlight"]["details"].size());
- ASSERT_EQ(1, results["hits"][0]["highlight"]["details"][0].size());
- ASSERT_EQ(1, results["hits"][0]["highlight"]["details"][1].size());
- ASSERT_EQ(1, results["hits"][0]["highlight"]["details"][2].size());
+ op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll3 = op.get();
+
+ add_op = coll3->add(doc1.dump(), CREATE);
+ ASSERT_FALSE(add_op.ok());
+ ASSERT_EQ("Field `variants.is_deleted` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.", add_op.error());
+
+ // float
+
+ schema = R"({
+ "name": "coll4",
+ "enable_nested_fields": true,
+ "fields": [
+ {"name": "variants", "type": "object[]", "facet": true, "index": true},
+ {"name": "variants.price", "type": "float", "facet": true}
+ ]
+ })"_json;
+
+ op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll4 = op.get();
+
+ add_op = coll4->add(doc1.dump(), CREATE);
+ ASSERT_FALSE(add_op.ok());
+ ASSERT_EQ("Field `variants.price` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.", add_op.error());
}
TEST_F(CollectionNestedFieldsTest, HighlightOnFlatFieldWithSnippeting) {