summaryrefslogtreecommitdiff
path: root/jstests/core/doc_validation.js
blob: bff7ade34c8b114c0a713a0573266b13f074511b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Cannot implicitly shard accessed collections because of collection existing when none
// expected.
// @tags: [
//   assumes_no_implicit_collection_creation_after_drop,
//   requires_fastcount,
//   requires_non_retryable_commands,
//   requires_non_retryable_writes,
//   requires_fastcount,
// ]

// Test basic inserts and updates with document validation.
(function() {
"use strict";
load("jstests/libs/doc_validation_utils.js");  // for assertDocumentValidationFailure

const collName = "doc_validation";
const coll = db[collName];

const array = [];
for (let i = 0; i < 2048; i++) {
    array.push({arbitrary: i});
}

/**
 * Runs a series of document validation tests using the validator 'validator', which should
 * enforce the existence of a field "a".
 */
function runInsertUpdateValidationTest(validator) {
    coll.drop();

    // Create a collection with document validator 'validator'.
    assert.commandWorked(db.createCollection(collName, {validator: validator}));
    // The default validation level/action are OMITTED from "listCollections".
    // (After a collMod they do appear, see below).
    let info = db.getCollectionInfos({name: collName})[0];
    assert.eq(info.options.validationLevel, undefined, info);
    assert.eq(info.options.validationAction, undefined, info);

    // Insert and upsert documents that will pass validation.
    assert.commandWorked(coll.insert({_id: "valid1", a: 1}));
    assert.commandWorked(coll.update({_id: "valid2"}, {_id: "valid2", a: 2}, {upsert: true}));
    assert.commandWorked(coll.runCommand(
        "findAndModify", {query: {_id: "valid3"}, update: {$set: {a: 3}}, upsert: true}));

    // Insert and upsert documents that will not pass validation.
    assertDocumentValidationFailure(coll.insert({_id: "invalid3", b: 1}), coll);
    assertDocumentValidationFailure(
        coll.update({_id: "invalid4"}, {_id: "invalid4", b: 2}, {upsert: true}), coll);
    assertDocumentValidationFailure(
        coll.runCommand("findAndModify",
                        {query: {_id: "invalid4"}, update: {$set: {b: 3}}, upsert: true}),
        coll);

    // Assert that we can remove the document that passed validation.
    assert.commandWorked(coll.remove({_id: "valid1"}));

    // Check that we can only update documents that pass validation. We insert a valid and an
    // invalid document, then set the validator.
    coll.drop();
    assert.commandWorked(coll.insert({_id: "valid1", a: 1}));
    assert.commandWorked(coll.insert({_id: "invalid2", b: 1}));
    assert.commandWorked(coll.runCommand("collMod", {validator: validator}));
    // After collMod, the default validation level/action are INCLUDED in "listCollections".
    info = db.getCollectionInfos({name: collName})[0];
    assert.eq(info.options.validationLevel, "strict", info);
    assert.eq(info.options.validationAction, "error", info);

    // Assert that updates on a conforming document succeed when they affect fields not involved
    // in validator.
    // Add a new field.
    assert.commandWorked(coll.update({_id: "valid1"}, {$set: {z: 1}}));
    assert.commandWorked(
        coll.runCommand("findAndModify", {query: {_id: "valid1"}, update: {$set: {y: 2}}}));
    // In-place update.
    assert.commandWorked(coll.update({_id: "valid1"}, {$inc: {z: 1}}));
    assert.commandWorked(
        coll.runCommand("findAndModify", {query: {_id: "valid1"}, update: {$inc: {y: 1}}}));
    // Out-of-place update.
    assert.commandWorked(coll.update({_id: "valid1"}, {$set: {z: array}}));
    assert.commandWorked(
        coll.runCommand("findAndModify", {query: {_id: "valid1"}, update: {$set: {y: array}}}));
    // No-op update.
    assert.commandWorked(coll.update({_id: "valid1"}, {a: 1}));
    assert.commandWorked(
        coll.runCommand("findAndModify", {query: {_id: "valid1"}, update: {$set: {a: 1}}}));

    // Verify those same updates will fail on non-conforming document.
    assertDocumentValidationFailure(coll.update({_id: "invalid2"}, {$set: {z: 1}}), coll);
    assertDocumentValidationFailure(coll.update({_id: "invalid2"}, {$inc: {z: 1}}), coll);
    assertDocumentValidationFailure(coll.update({_id: "invalid2"}, {$set: {z: array}}), coll);
    assertDocumentValidationFailure(
        coll.runCommand("findAndModify", {query: {_id: "invalid2"}, update: {$set: {y: 2}}}), coll);
    assertDocumentValidationFailure(
        coll.runCommand("findAndModify", {query: {_id: "invalid2"}, update: {$inc: {y: 1}}}), coll);
    assertDocumentValidationFailure(
        coll.runCommand("findAndModify", {query: {_id: "invalid2"}, update: {$set: {y: array}}}),
        coll);

    // A no-op update of an invalid doc will succeed.
    assert.commandWorked(coll.update({_id: "invalid2"}, {$set: {b: 1}}));
    assert.commandWorked(
        coll.runCommand("findAndModify", {query: {_id: "invalid2"}, update: {$set: {b: 1}}}));

    // Verify that we can't make a conforming document fail validation, but can update a
    // non-conforming document to pass validation.
    coll.drop();
    assert.commandWorked(coll.insert({_id: "valid1", a: 1}));
    assert.commandWorked(coll.insert({_id: "invalid2", b: 1}));
    assert.commandWorked(coll.insert({_id: "invalid3", b: 1}));
    assert.commandWorked(coll.runCommand("collMod", {validator: validator}));

    assertDocumentValidationFailure(coll.update({_id: "valid1"}, {$unset: {a: 1}}), coll);
    assert.commandWorked(coll.update({_id: "invalid2"}, {$set: {a: 1}}));
    assertDocumentValidationFailure(
        coll.runCommand("findAndModify", {query: {_id: "valid1"}, update: {$unset: {a: 1}}}), coll);
    assert.commandWorked(
        coll.runCommand("findAndModify", {query: {_id: "invalid3"}, update: {$set: {a: 1}}}));

    // Modify the collection to remove the document validator.
    assert.commandWorked(coll.runCommand("collMod", {validator: {}}));

    // Verify that no validation is applied to updates.
    assert.commandWorked(coll.update({_id: "valid1"}, {$set: {z: 1}}));
    assert.commandWorked(coll.update({_id: "invalid2"}, {$set: {z: 1}}));
    assert.commandWorked(coll.update({_id: "valid1"}, {$unset: {a: 1}}));
    assert.commandWorked(coll.update({_id: "invalid2"}, {$set: {a: 1}}));
    assert.commandWorked(
        coll.runCommand("findAndModify", {query: {_id: "valid1"}, update: {$set: {z: 2}}}));
    assert.commandWorked(
        coll.runCommand("findAndModify", {query: {_id: "invalid2"}, update: {$set: {z: 2}}}));
    assert.commandWorked(
        coll.runCommand("findAndModify", {query: {_id: "valid1"}, update: {$unset: {a: 1}}}));
    assert.commandWorked(
        coll.runCommand("findAndModify", {query: {_id: "invalid2"}, update: {$set: {a: 1}}}));
}

// Run the test with a normal validator.
runInsertUpdateValidationTest({a: {$exists: true}});

// Run the test again with an equivalent JSON Schema.
runInsertUpdateValidationTest({$jsonSchema: {required: ["a"]}});

/**
 * 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}}));

    // An insert that matches the validator should succeed.
    assert.commandWorked(coll.insert({_id: 0, a: "xyz", b: "foo"}));

    const isJSONSchema = validator.hasOwnProperty("$jsonSchema");

    // A normal validator should respect the collation and the inserts should succeed. A JSON
    // Schema validator ignores the collation and the inserts should fail.
    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"}}}));

    // Test an insert and an update that should always fail.
    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);

    // 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.
    assertCorrectResult(coll.insert({a: ["xyz"]}));
    assertCorrectResult(coll.insert({a: ["XYZ"]}));
    assertCorrectResult(coll.insert({a: ["XyZ"], b: "foo"}));
}

runCollationValidationTest({a: "xyz"});
runCollationValidationTest({$jsonSchema: {properties: {a: {enum: ["xyz"]}}}});

// The validator is allowed to contain $expr.
coll.drop();
assert.commandWorked(db.createCollection(collName, {validator: {$expr: {$eq: ["$a", 5]}}}));
assert.commandWorked(coll.insert({a: 5}));
assertDocumentValidationFailure(coll.insert({a: 4}), coll);
assert.commandWorked(db.runCommand({"collMod": collName, "validator": {$expr: {$eq: ["$a", 4]}}}));
assert.commandWorked(coll.insert({a: 4}));
assertDocumentValidationFailure(coll.insert({a: 5}), coll);

// The validator will generate detailed errors when $expr throws.
assert.commandWorked(
    db.runCommand({"collMod": collName, "validator": {$expr: {$divide: [10, 0]}}}));
assertDocumentValidationFailure(coll.insert({a: 4}), coll);
assert.commandWorked(
    db.runCommand({"collMod": collName, "validator": {$nor: [{$expr: {$divide: [10, 0]}}]}}));
assertDocumentValidationFailure(coll.insert({a: 4}), coll);

// The validator supports $expr with the date extraction expressions (with a timezone
// specified).
coll.drop();
assert.commandWorked(db.createCollection(
    collName,
    {validator: {$expr: {$eq: [1, {$dayOfMonth: {date: "$a", timezone: "America/New_York"}}]}}}));
assert.commandWorked(coll.insert({a: ISODate("2017-10-01T22:00:00")}));
assertDocumentValidationFailure(coll.insert({a: ISODate("2017-10-01T00:00:00")}), coll);

// The validator supports $expr with a $dateToParts expression.
coll.drop();
assert.commandWorked(db.createCollection(collName, {
    validator: {
        $expr: {
            $eq: [
                {
                    "year": 2017,
                    "month": 10,
                    "day": 1,
                    "hour": 18,
                    "minute": 0,
                    "second": 0,
                    "millisecond": 0
                },
                {$dateToParts: {date: "$a", timezone: "America/New_York"}}
            ]
        }
    }
}));
assert.commandWorked(coll.insert({a: ISODate("2017-10-01T22:00:00")}));
assertDocumentValidationFailure(coll.insert({a: ISODate("2017-10-01T00:00:00")}), coll);

// The validator supports $expr with $dateToString expression.
coll.drop();
assert.commandWorked(db.createCollection(collName, {
    validator: {
        $expr: {
            $eq: [
                "2017-07-04 14:56:42 +0000 (0 minutes)",
                {
                    $dateToString: {
                        format: "%Y-%m-%d %H:%M:%S %z (%Z minutes)",
                        date: "$date",
                        timezone: "$tz"
                    }
                }
            ]
        }
    }
}));
assert.commandWorked(coll.insert({date: new ISODate("2017-07-04T14:56:42.911Z"), tz: "UTC"}));
assertDocumentValidationFailure(
    coll.insert({date: new ISODate("2017-07-04T14:56:42.911Z"), tz: "America/New_York"}), coll);

// The validator supports $expr with $dateFromParts expression.
coll.drop();
assert.commandWorked(db.createCollection(collName, {
    validator: {
        $expr: {
            $eq: [
                ISODate("2016-12-31T15:00:00Z"),
                {'$dateFromParts': {year: "$year", "timezone": "$timezone"}}
            ]
        }
    }
}));
assert.commandWorked(coll.insert({_id: 0, year: 2017, month: 6, day: 19, timezone: "Asia/Tokyo"}));
assertDocumentValidationFailure(
    coll.insert({_id: 1, year: 2022, month: 1, day: 1, timezone: "America/New_York"}), coll);

// The validator supports $expr with $dateFromString expression.
coll.drop();
assert.commandWorked(db.createCollection(collName, {
    validator: {
        $expr: {
            $eq: [
                ISODate("2017-07-04T15:56:02Z"),
                {'$dateFromString': {dateString: "$date", timezone: 'America/New_York'}}
            ]
        }
    }
}));
assert.commandWorked(coll.insert({_id: 0, date: "2017-07-04T11:56:02"}));
assertDocumentValidationFailure(coll.insert({_id: 1, date: "2015-02-02T11:00:00"}), coll);

// The validator can contain an $expr that may throw at runtime.
coll.drop();
assert.commandWorked(
    db.createCollection(collName, {validator: {$expr: {$eq: ["$a", {$divide: [1, "$b"]}]}}}));
assert.commandWorked(coll.insert({a: 1, b: 1}));
let res = coll.insert({a: 1, b: 0});
assert.writeError(res);
assert.eq(res.getWriteError().code, ErrorCodes.DocumentValidationFailure);
assert.commandWorked(coll.insert({a: -1, b: -1}));

// The validator can contain an $expr that respects the collation.
coll.drop();
// Create collection with validator that uses case insensitive collation.
assert.commandWorked(db.createCollection(
    collName,
    {validator: {$expr: {$eq: ["$a", "foobar"]}}, collation: {locale: "en", strength: 2}}));
assert.commandWorked(coll.insert({a: "foobar"}));
assert.commandWorked(coll.insert({a: "fooBAR"}));

// Recreate a collection with a simple validator.
coll.drop();
assert.commandWorked(db.createCollection(collName, {validator: {a: 1}}));

// Insert a document that fails validation.
res = coll.insert({_id: 1, a: 2});

// Verify that the document validation error attribute 'failingDocumentId' equals to the document
// '_id' attribute.
assertDocumentValidationFailure(res, coll);
const errorInfo = res.getWriteError().errInfo;
const expectedError = {
    failingDocumentId: 1,
    details:
        {operatorName: "$eq", specifiedAs: {a: 1}, reason: "comparison failed", consideredValue: 2}
};
assert.docEq(errorInfo, expectedError, tojson(res));

// Insert a valid document.
assert.commandWorked(coll.insert({_id: 1, a: 1}));

// Issues the update command and returns the response.
function updateCommand(coll, query, update) {
    return coll.update(query, update);
}

// Issues the findAndModify command and returns the response.
function findAndModifyCommand(coll, query, update) {
    return coll.runCommand("findAndModify", {query: query, update: update});
}

for (const command of [updateCommand, findAndModifyCommand]) {
    // Attempt to update the document by replacing it with a document that does not pass validation.
    const res = command(coll, {}, {a: 2});

    // Verify that the document validation error attribute 'failingDocumentId' equals to the
    // document '_id' attribute.
    assertDocumentValidationFailure(res, coll);
    const errorInfo = (res instanceof WriteResult ? res.getWriteError() : res).errInfo;
    const expectedError = {
        failingDocumentId: 1,
        details: {
            operatorName: "$eq",
            specifiedAs: {a: 1},
            reason: "comparison failed",
            consideredValue: 2
        }
    };
    assert.docEq(errorInfo, expectedError, tojson(res));
}
})();