jstests/core/doc_validation.js
/**
 * Run a series of document validation tests involving collation using the validator
 * 'validator', which should enforce that the field "a" has the value "xyz".
 */
Run a series of document validation tests involving collation using the validator
'validator', which should enforce that the field "a" has the value "xyz".
Run a series of document validation tests involving collation using the validator ' validator ' which should enforce that the field " a " has the value " xyz " .
function runCollationValidationTest(validator) {
    coll.drop();
    assert.commandWorked(db.createCollection(
        collName, {validator: validator, collation: {locale: "en_US", strength: 2}}));
    assert.commandWorked(coll.insert({_id: 0, a: "xyz", b: "foo"}));
    const isJSONSchema = validator.hasOwnProperty("$jsonSchema");
    const assertCorrectResult = isJSONSchema ? res => assertDocumentValidationFailure(res, coll)
                                             : res => assert.commandWorked(res);
    assertCorrectResult(coll.insert({a: "XYZ"}));
    assertCorrectResult(coll.insert({a: "XyZ", b: "foo"}));
    assertCorrectResult(coll.update({_id: 0}, {a: "xyZ", b: "foo"}));
    assertCorrectResult(coll.update({_id: 0}, {$set: {a: "Xyz"}}));
    assertCorrectResult(
        coll.runCommand("findAndModify", {query: {_id: 0}, update: {a: "xyZ", b: "foo"}}));
    assertCorrectResult(
        coll.runCommand("findAndModify", {query: {_id: 0}, update: {$set: {a: "Xyz"}}}));
    assertDocumentValidationFailure(coll.insert({a: "not xyz"}), coll);
    assertDocumentValidationFailure(coll.update({_id: 0}, {$set: {a: "xyzz"}}), coll);
    assertDocumentValidationFailure(
        coll.runCommand("findAndModify", {query: {_id: 0}, update: {$set: {a: "xyzz"}}}), coll);
    assertCorrectResult(coll.insert({a: ["xyz"]}));
    assertCorrectResult(coll.insert({a: ["XYZ"]}));
    assertCorrectResult(coll.insert({a: ["XyZ"], b: "foo"}));
}
function runCollationValidationTest ( validator ) { coll . drop ( ) ; assert . commandWorked ( db . createCollection ( collName , { validator : validator , collation : { locale : "en_US" , strength : 2 } } ) ) ; assert . commandWorked ( coll . insert ( { _id : 0 , a : "xyz" , b : "foo" } ) ) ; const isJSONSchema = validator . hasOwnProperty ( "$jsonSchema" ) ; const assertCorrectResult = isJSONSchema ? res => assertDocumentValidationFailure ( res , coll ) : res => assert . commandWorked ( res ) ; assertCorrectResult ( coll . insert ( { a : "XYZ" } ) ) ; assertCorrectResult ( coll . insert ( { a : "XyZ" , b : "foo" } ) ) ; assertCorrectResult ( coll . update ( { _id : 0 } , { a : "xyZ" , b : "foo" } ) ) ; assertCorrectResult ( coll . update ( { _id : 0 } , { $set : { a : "Xyz" } } ) ) ; assertCorrectResult ( coll . runCommand ( "findAndModify" , { query : { _id : 0 } , update : { a : "xyZ" , b : "foo" } } ) ) ; assertCorrectResult ( coll . runCommand ( "findAndModify" , { query : { _id : 0 } , update : { $set : { a : "Xyz" } } } ) ) ; assertDocumentValidationFailure ( coll . insert ( { a : "not xyz" } ) , coll ) ; assertDocumentValidationFailure ( coll . update ( { _id : 0 } , { $set : { a : "xyzz" } } ) , coll ) ; assertDocumentValidationFailure ( coll . runCommand ( "findAndModify" , { query : { _id : 0 } , update : { $set : { a : "xyzz" } } } ) , coll ) ; assertCorrectResult ( coll . insert ( { a : [ "xyz" ] } ) ) ; assertCorrectResult ( coll . insert ( { a : [ "XYZ" ] } ) ) ; assertCorrectResult ( coll . insert ( { a : [ "XyZ" ] , b : "foo" } ) ) ; }
Run a series of document validation tests involving collation using the validator
 'validator', which should enforce that the field "a" has the value "xyz".
Run a series of document validation tests involving collation using the validator ' validator ' which should enforce that the field " a " has the value " xyz " .
// An insert that matches the validator should succeed. // A normal validator should respect the collation and the inserts should succeed. A JSON // Schema validator ignores the collation and the inserts should fail. // Test an insert and an update that should always fail. // A normal validator expands leaf arrays, such that if "a" is an array containing "xyz", it // matches {a: "xyz"}. A JSON Schema validator does not expand leaf arrays and treats arrays // as a single array value.