jstests/fle2/implicit_schema_validation.js
/**
 * Asserts the result of a command is a document validation failure.
 * If 'fleErrors' is defined, then this asserts that the errInfo in the result
 * contains an "implicitFLESchema" annotation, and an annotation for each
 * attribute in 'fleErrors'. Each attribute in 'fleErrors' is a pair where the key
 * is the encrypted field path that is expected to cause an error, and the value is
 * an object containing the expected 'operatorName' and detail fields.
 */
Asserts the result of a command is a document validation failure.
If 'fleErrors' is defined, then this asserts that the errInfo in the result
contains an "implicitFLESchema" annotation, and an annotation for each
attribute in 'fleErrors'. Each attribute in 'fleErrors' is a pair where the key
is the encrypted field path that is expected to cause an error, and the value is
an object containing the expected 'operatorName' and detail fields.
Asserts the result of a command is a document validation failure . If ' fleErrors ' is defined then this asserts that the errInfo in the result contains an " implicitFLESchema " annotation and an annotation for each attribute in ' fleErrors ' . Each attribute in ' fleErrors ' is a pair where the key is the encrypted field path that is expected to cause an error and the value is an object containing the expected ' operatorName ' and detail fields .
function assertFailedWithAnnotation(result, coll, fleErrors) {
    assertDocumentValidationFailure(result, coll);
    assert(result instanceof WriteResult);
    const errInfo = result.getWriteError().errInfo;
    const schema = findContainingObject(errInfo, "operatorName", "implicitFLESchema");
    if (fleErrors) {
        assert(schema,
               "Result errInfo does not contain an implicitFLESchema error: " + tojson(errInfo));
    } else {
        assert(!schema,
               "Result errInfo contains unexpected implicitFLESchema error: " + tojson(errInfo));
        return;
    }
    assert(schema.hasOwnProperty("schemaRulesNotSatisfied"));
    for (let path in fleErrors) {
        const pathParts = path.split('.');
        let subschema = schema;
        for (let pathIdx in pathParts) {
            subschema = findContainingObject(subschema, "propertyName", pathParts[pathIdx]);
            assert(subschema, "No errors found for property '" + path + "': " + tojson(errInfo));
        }
        assert(subschema.hasOwnProperty("details"),
               "No error details found for property '" + path + "': " + tojson(errInfo));
        const detail =
            findContainingObject(subschema.details, "operatorName", fleErrors[path].operator);
        assert(detail,
               "Error details for property '" + path +
                   "' does not contain the expected operator '" + fleErrors[path].operator +
                   "': " + tojson(errInfo));
        for (let field in fleErrors[path]) {
            if (field === "operator") {
                continue;
            }
            const detailWithField = findContainingObject(detail, field, fleErrors[path][field]);
            assert(detailWithField,
                   "Error details for property '" + path + "' does not contain the expected " +
                       field + " '" + fleErrors[path][field] + "': " + tojson(errInfo));
        }
    }
}
function assertFailedWithAnnotation ( result , coll , fleErrors ) { assertDocumentValidationFailure ( result , coll ) ; assert ( result instanceof WriteResult ) ; const errInfo = result . getWriteError ( ) . errInfo ; const schema = findContainingObject ( errInfo , "operatorName" , "implicitFLESchema" ) ; if ( fleErrors ) { assert ( schema , "Result errInfo does not contain an implicitFLESchema error: " + tojson ( errInfo ) ) ; } else { assert ( ! schema , "Result errInfo contains unexpected implicitFLESchema error: " + tojson ( errInfo ) ) ; return ; } assert ( schema . hasOwnProperty ( "schemaRulesNotSatisfied" ) ) ; for ( let path in fleErrors ) { const pathParts = path . split ( '.' ) ; let subschema = schema ; for ( let pathIdx in pathParts ) { subschema = findContainingObject ( subschema , "propertyName" , pathParts [ pathIdx ] ) ; assert ( subschema , "No errors found for property '" + path + "': " + tojson ( errInfo ) ) ; } assert ( subschema . hasOwnProperty ( "details" ) , "No error details found for property '" + path + "': " + tojson ( errInfo ) ) ; const detail = findContainingObject ( subschema . details , "operatorName" , fleErrors [ path ] . operator ) ; assert ( detail , "Error details for property '" + path + "' does not contain the expected operator '" + fleErrors [ path ] . operator + "': " + tojson ( errInfo ) ) ; for ( let field in fleErrors [ path ] ) { if ( field === "operator" ) { continue ; } const detailWithField = findContainingObject ( detail , field , fleErrors [ path ] [ field ] ) ; assert ( detailWithField , "Error details for property '" + path + "' does not contain the expected " + field + " '" + fleErrors [ path ] [ field ] + "': " + tojson ( errInfo ) ) ; } } }
Asserts the result of a command is a document validation failure.
Asserts the result of a command is a document validation failure .