mirror of
https://github.com/typesense/typesense.git
synced 2025-05-22 06:40:30 +08:00
Improve error message for nested array object string field.
This commit is contained in:
parent
0579398993
commit
86535e24aa
@ -5557,6 +5557,10 @@ Option<uint32_t> Index::coerce_string(const DIRTY_VALUES& dirty_values, const st
|
||||
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 + " string.");
|
||||
}
|
||||
|
||||
@ -5568,6 +5572,10 @@ Option<uint32_t> Index::coerce_string(const DIRTY_VALUES& dirty_values, const st
|
||||
}
|
||||
} 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 + " string.");
|
||||
}
|
||||
}
|
||||
|
@ -1445,6 +1445,31 @@ TEST_F(CollectionNestedFieldsTest, ExplicitSchemaOptionalFieldValidation) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(CollectionNestedFieldsTest, ExplicitSchemaForNestedArrayTypeValidation) {
|
||||
nlohmann::json schema = R"({
|
||||
"name": "coll1",
|
||||
"enable_nested_fields": true,
|
||||
"fields": [
|
||||
{"name": "blocks.text", "type": "object[]"},
|
||||
{"name": "blocks.text.description", "type": "string"}
|
||||
]
|
||||
})"_json;
|
||||
|
||||
auto op = collectionManager.create_collection(schema);
|
||||
ASSERT_TRUE(op.ok());
|
||||
Collection* coll1 = op.get();
|
||||
|
||||
auto doc1 = R"({
|
||||
"blocks": {"text": [{"description": "Hello world."}]}
|
||||
})"_json;
|
||||
|
||||
auto add_op = coll1->add(doc1.dump(), CREATE);
|
||||
|
||||
ASSERT_FALSE(add_op.ok());
|
||||
ASSERT_EQ("Field `blocks.text.description` has an incorrect type. "
|
||||
"Hint: field inside an array of objects must be an array type as well.", add_op.error());
|
||||
}
|
||||
|
||||
TEST_F(CollectionNestedFieldsTest, SortByNestedField) {
|
||||
nlohmann::json schema = R"({
|
||||
"name": "coll1",
|
||||
|
Loading…
x
Reference in New Issue
Block a user