jstests/fle2/implicit_schema_validation.js
// This assumes 'coll' was created encrypted fields specified in 'sampleEncryptedFields'.
This assumes 'coll' was created encrypted fields specified in 'sampleEncryptedFields'.
This assumes ' coll ' was created encrypted fields specified in ' sampleEncryptedFields ' .
function negativeUpdateTests(coll, invert = false) {
    function assertExpectedResult(result, fleErrors) {
        if (invert) {
            assert.commandWorked(result);
        } else {
            assertFailedWithAnnotation(result, coll, fleErrors);
        }
    }
    assert.commandWorked(coll.insert({
        test_id: 0,
        firstName: validEncryptedString,
        a: {
            b: {
                c: validEncryptedInt,
                d: validEncryptedInt,
            },
            x: {
                y: validEncryptedString,
            }
        }
    }));
    jsTestLog("test updating encrypted field with invalid value");
    assertExpectedResult(coll.update({"test_id": 0}, {$set: {"firstName": "roger"}}),
                         {firstName: valueNotEncryptedError});
    assertExpectedResult(coll.update({"test_id": 0}, {$set: {"firstName": nonEncryptedBinData}}),
                         {firstName: valueNotEncryptedError});
    assertExpectedResult(coll.update({"test_id": 0}, {$set: {"firstName": fle1RandomBinData}}),
                         {firstName: wrongEncryptedTypeError});
    assertExpectedResult(coll.update({"test_id": 0}, {$set: {"firstName": validEncryptedInt}}),
                         {firstName: wrongEncryptedTypeError});
    assertExpectedResult(coll.update({"test_id": 0}, {$set: {"a.x.y": [1, 2, 3]}}),
                         {"a.x.y": valueNotEncryptedError});
    assertExpectedResult(coll.update({"test_id": 0}, {$set: {"a.x": {"y": 42}}}),
                         {"a.x": valueNotEncryptedError});
    jsTestLog("test updating prefix of encrypted field with array value");
    assertExpectedResult(coll.update({"test_id": 0}, {$set: {"a.b": [1, 2, 3]}}),
                         {"a.b": typeMatchedArrayError});
}
function negativeUpdateTests ( coll , invert = false ) { function assertExpectedResult ( result , fleErrors ) { if ( invert ) { assert . commandWorked ( result ) ; } else { assertFailedWithAnnotation ( result , coll , fleErrors ) ; } } assert . commandWorked ( coll . insert ( { test_id : 0 , firstName : validEncryptedString , a : { b : { c : validEncryptedInt , d : validEncryptedInt , } , x : { y : validEncryptedString , } } } ) ) ; jsTestLog ( "test updating encrypted field with invalid value" ) ; assertExpectedResult ( coll . update ( { "test_id" : 0 } , { $set : { "firstName" : "roger" } } ) , { firstName : valueNotEncryptedError } ) ; assertExpectedResult ( coll . update ( { "test_id" : 0 } , { $set : { "firstName" : nonEncryptedBinData } } ) , { firstName : valueNotEncryptedError } ) ; assertExpectedResult ( coll . update ( { "test_id" : 0 } , { $set : { "firstName" : fle1RandomBinData } } ) , { firstName : wrongEncryptedTypeError } ) ; assertExpectedResult ( coll . update ( { "test_id" : 0 } , { $set : { "firstName" : validEncryptedInt } } ) , { firstName : wrongEncryptedTypeError } ) ; assertExpectedResult ( coll . update ( { "test_id" : 0 } , { $set : { "a.x.y" : [ 1 , 2 , 3 ] } } ) , { "a.x.y" : valueNotEncryptedError } ) ; assertExpectedResult ( coll . update ( { "test_id" : 0 } , { $set : { "a.x" : { "y" : 42 } } } ) , { "a.x" : valueNotEncryptedError } ) ; jsTestLog ( "test updating prefix of encrypted field with array value" ) ; assertExpectedResult ( coll . update ( { "test_id" : 0 } , { $set : { "a.b" : [ 1 , 2 , 3 ] } } ) , { "a.b" : typeMatchedArrayError } ) ; }
This assumes 'coll' was created encrypted fields specified in 'sampleEncryptedFields'.
This assumes ' coll ' was created encrypted fields specified in ' sampleEncryptedFields ' .
// first, insert a valid document to update