diff --git a/src/validator.cpp b/src/validator.cpp index 38288673..b2ea3e87 100644 --- a/src/validator.cpp +++ b/src/validator.cpp @@ -627,7 +627,7 @@ Option validator_t::validate_index_in_memory(nlohmann::json& document, continue; } - if((a_field.optional || op == UPDATE || op == EMPLACE) && document.count(field_name) == 0) { + if((a_field.optional || op == UPDATE || (op == EMPLACE && is_update)) && document.count(field_name) == 0) { continue; } diff --git a/test/collection_nested_fields_test.cpp b/test/collection_nested_fields_test.cpp index 6def5d1a..98a94f37 100644 --- a/test/collection_nested_fields_test.cpp +++ b/test/collection_nested_fields_test.cpp @@ -2578,6 +2578,19 @@ TEST_F(CollectionNestedFieldsTest, EmplaceWithNullValueOnRequiredField) { ASSERT_TRUE(op.ok()); Collection *coll1 = op.get(); + auto doc_with_null = R"({ + "id": "0", + "currency": { + "eu": null + } + })"_json; + + auto add_op = coll1->add(doc_with_null.dump(), EMPLACE); + ASSERT_FALSE(add_op.ok()); + + add_op = coll1->add(doc_with_null.dump(), CREATE); + ASSERT_FALSE(add_op.ok()); + auto doc1 = R"({ "id": "0", "currency": { @@ -2585,7 +2598,7 @@ TEST_F(CollectionNestedFieldsTest, EmplaceWithNullValueOnRequiredField) { } })"_json; - auto add_op = coll1->add(doc1.dump(), CREATE); + add_op = coll1->add(doc1.dump(), CREATE); ASSERT_TRUE(add_op.ok()); // now update with null value -- should not be allowed