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 alter schema validation error messages.

Kishore Nallanb39dd3584f89
diff --git a/src/collection.cpp b/src/collection.cpp
index ebe6ce13..0c2167cd 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -2811,8 +2811,36 @@ Option<bool> Collection::validate_alter_payload(nlohmann::json& schema_changes,
fallback_field_type,
DIRTY_VALUES::REJECT);
if(!validate_op.ok()) {
- std::string schema_err = "Schema change does not match on-disk data, error: " + validate_op.error();
- return Option<bool>(validate_op.code(), schema_err);
+ std::string err_message = validate_op.error();
+
+ // we've to message the error message to suite the schema alter context
+ if(err_message.find("but is not found in the document.") != std::string::npos) {
+ // missing field
+ err_message.pop_back(); // delete trailing dot
+ err_message += "s already present in the collection. If you still want to add this field, "
+ "set it as `optional: true`.";
+ return Option<bool>(validate_op.code(), err_message);
+ }
+
+ else if(err_message.find("must be") != std::string::npos) {
+ // type of an already stored document conflicts with new schema
+ std::string type_error = "Schema change is incompatible with the type of documents already stored "
+ "in this collection.";
+ std::vector<std::string> err_parts;
+ StringUtils::split(err_message, err_parts, "must be");
+ if(err_parts.size() == 2) {
+ err_parts[0][0] = std::tolower(err_parts[0][0]);
+ type_error += " Existing data for " + err_parts[0] + " cannot be coerced into " + err_parts[1];
+ }
+
+ return Option<bool>(validate_op.code(), type_error);
+ }
+
+ else {
+ std::string schema_err = "Schema change is incompatible with the type of documents already stored "
+ "in this collection. error: " + validate_op.error();
+ return Option<bool>(validate_op.code(), schema_err);
+ }
}
if(num_found_docs % ((1 << 14)) == 0) {
diff --git a/test/collection_schema_change_test.cpp b/test/collection_schema_change_test.cpp
index b0ae25f7..f0924c86 100644
--- a/test/collection_schema_change_test.cpp
+++ b/test/collection_schema_change_test.cpp
@@ -419,7 +419,8 @@ TEST_F(CollectionSchemaChangeTest, AlterValidations) {
})"_json;
alter_op = coll1->alter(schema_changes);
ASSERT_FALSE(alter_op.ok());
- ASSERT_EQ("Schema change does not match on-disk data, error: Field `desc` must be an int32.", alter_op.error());
+ ASSERT_EQ("Schema change is incompatible with the type of documents already stored in this collection. "
+ "Existing data for field `desc` cannot be coerced into an int32.", alter_op.error());
// 6. Prevent non-optional field when on-disk data has missing values
doc.clear();
@@ -439,8 +440,8 @@ TEST_F(CollectionSchemaChangeTest, AlterValidations) {
})"_json;
alter_op = coll1->alter(schema_changes);
ASSERT_FALSE(alter_op.ok());
- ASSERT_EQ("Schema change does not match on-disk data, error: Field `desc` has been declared in the "
- "schema, but is not found in the document.", alter_op.error());
+ ASSERT_EQ("Field `desc` has been declared in the schema, but is not found in the documents already present "
+ "in the collection. If you still want to add this field, set it as `optional: true`.", alter_op.error());
collectionManager.drop_collection("coll1");
}
@@ -490,7 +491,8 @@ TEST_F(CollectionSchemaChangeTest, AbilityToDropAndReAddIndexAtTheSameTime) {
auto alter_op = coll1->alter(schema_changes);
ASSERT_FALSE(alter_op.ok());
- ASSERT_EQ("Schema change does not match on-disk data, error: Field `title` must be an int32.", alter_op.error());
+ ASSERT_EQ("Schema change is incompatible with the type of documents already stored in this collection. "
+ "Existing data for field `title` cannot be coerced into an int32.", alter_op.error());
// existing data should not have been touched
auto res = coll1->search("12", {"title"}, "", {}, {}, {0}, 10, 1, FREQUENCY, {true}, 10).get();