jstests/fle2/implicit_schema_validation.js
// If 'hasUserValidator' is true, this assumes it validates the optional field 'name' is a string.
If 'hasUserValidator' is true, this assumes it validates the optional field 'name' is a string.
If ' hasUserValidator ' is true this assumes it validates the optional field ' name ' is a string .
function negativeTests(coll, hasUserValidator, invert = false) {
    function assertExpectedResult(result, fleErrors) {
        if (invert) {
            assert.commandWorked(result);
        } else {
            assertFailedWithAnnotation(result, coll, fleErrors);
        }
        return result;
    }
    jsTestLog("test inserting non-bindata value for encrypted field");
    assertExpectedResult(coll.insert({firstName: "foo"}), {firstName: valueNotEncryptedError});
    assertExpectedResult(coll.insert({
        firstName: validEncryptedString,
        a: {
            b: {
                c: "bar",
                d: "foo",
            },
        }
    }),
                         {"a.b.c": valueNotEncryptedError, "a.b.d": valueNotEncryptedError});
    jsTestLog("test path to encrypted field has arrays");
    assertExpectedResult(coll.insert({a: [{b: {c: validEncryptedInt}}]}),
                         {"a": typeMatchedArrayError});
    assertExpectedResult(coll.insert({a: {b: [{c: validEncryptedInt}]}}),
                         {"a.b": typeMatchedArrayError});
    assertExpectedResult(coll.insert({a: {b: {c: []}}}), {"a.b.c": valueNotEncryptedError});
    jsTestLog("test inserting encrypted field with BinData of incorrect subtype");
    assertExpectedResult(coll.insert({firstName: nonEncryptedBinData}),
                         {firstName: valueNotEncryptedError});
    assertExpectedResult(coll.insert({
        firstName: validEncryptedString,
        a: {
            b: {
                c: nonEncryptedBinData,
                d: validEncryptedInt,
            },
        }
    }),
                         {"a.b.c": valueNotEncryptedError});
    jsTestLog("test inserting encrypted field with incorrect FLE2 subtype");
    assertExpectedResult(coll.insert({firstName: fle1RandomBinData}),
                         {firstName: wrongEncryptedTypeError});
    assertExpectedResult(coll.insert({
        firstName: validEncryptedString,
        a: {
            b: {
                c: fle2PlaceholderBinData,
                d: validEncryptedInt,
            },
        }
    }),
                         {"a.b.c": wrongEncryptedTypeError});
    jsTestLog(
        "test inserting encrypted field with incorrect BSONType specifier for the unencrypted value");
    assertExpectedResult(coll.insert({firstName: validEncryptedInt}),
                         {firstName: wrongEncryptedTypeError});
    assertExpectedResult(coll.insert({
        firstName: validEncryptedString,
        a: {
            b: {
                c: validEncryptedString,
                d: validEncryptedInt,
            },
        }
    }),
                         {"a.b.c": wrongEncryptedTypeError});
    if (!hasUserValidator) {
        return;
    }
    jsTestLog("test insert violating user-provided validator");
    assertExpectedResult(coll.insert({firstName: validEncryptedString, name: 234}));
    assertExpectedResult(coll.insert({firstName: nonEncryptedBinData, name: 234}),
                         {firstName: valueNotEncryptedError});
}
function negativeTests ( coll , hasUserValidator , invert = false ) { function assertExpectedResult ( result , fleErrors ) { if ( invert ) { assert . commandWorked ( result ) ; } else { assertFailedWithAnnotation ( result , coll , fleErrors ) ; } return result ; } jsTestLog ( "test inserting non-bindata value for encrypted field" ) ; assertExpectedResult ( coll . insert ( { firstName : "foo" } ) , { firstName : valueNotEncryptedError } ) ; assertExpectedResult ( coll . insert ( { firstName : validEncryptedString , a : { b : { c : "bar" , d : "foo" , } , } } ) , { "a.b.c" : valueNotEncryptedError , "a.b.d" : valueNotEncryptedError } ) ; jsTestLog ( "test path to encrypted field has arrays" ) ; assertExpectedResult ( coll . insert ( { a : [ { b : { c : validEncryptedInt } } ] } ) , { "a" : typeMatchedArrayError } ) ; assertExpectedResult ( coll . insert ( { a : { b : [ { c : validEncryptedInt } ] } } ) , { "a.b" : typeMatchedArrayError } ) ; assertExpectedResult ( coll . insert ( { a : { b : { c : [ ] } } } ) , { "a.b.c" : valueNotEncryptedError } ) ; jsTestLog ( "test inserting encrypted field with BinData of incorrect subtype" ) ; assertExpectedResult ( coll . insert ( { firstName : nonEncryptedBinData } ) , { firstName : valueNotEncryptedError } ) ; assertExpectedResult ( coll . insert ( { firstName : validEncryptedString , a : { b : { c : nonEncryptedBinData , d : validEncryptedInt , } , } } ) , { "a.b.c" : valueNotEncryptedError } ) ; jsTestLog ( "test inserting encrypted field with incorrect FLE2 subtype" ) ; assertExpectedResult ( coll . insert ( { firstName : fle1RandomBinData } ) , { firstName : wrongEncryptedTypeError } ) ; assertExpectedResult ( coll . insert ( { firstName : validEncryptedString , a : { b : { c : fle2PlaceholderBinData , d : validEncryptedInt , } , } } ) , { "a.b.c" : wrongEncryptedTypeError } ) ; jsTestLog ( "test inserting encrypted field with incorrect BSONType specifier for the unencrypted value" ) ; assertExpectedResult ( coll . insert ( { firstName : validEncryptedInt } ) , { firstName : wrongEncryptedTypeError } ) ; assertExpectedResult ( coll . insert ( { firstName : validEncryptedString , a : { b : { c : validEncryptedString , d : validEncryptedInt , } , } } ) , { "a.b.c" : wrongEncryptedTypeError } ) ; if ( ! hasUserValidator ) { return ; } jsTestLog ( "test insert violating user-provided validator" ) ; assertExpectedResult ( coll . insert ( { firstName : validEncryptedString , name : 234 } ) ) ; assertExpectedResult ( coll . insert ( { firstName : nonEncryptedBinData , name : 234 } ) , { firstName : valueNotEncryptedError } ) ; }
If 'hasUserValidator' is true, this assumes it validates the optional field 'name' is a string.
If ' hasUserValidator ' is true this assumes it validates the optional field ' name ' is a string .