summaryrefslogtreecommitdiff
path: root/jstests/core/json_schema/misc_validation.js
blob: 90d25fff0e2e6828b17d84782d6fe182930c944e (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
/*
 * Test for $jsonSchema behavior in contexts other than document validation, including:
 * - read commands (count, geoNear, distinct, etc)
 * - queries that specify a collation
 * - $match stage within a view
 * - listCollections
 * - listDatabases
 * - graphLookup
 * - delete
 * - update
 * - findAndModify
 * - applyOps
 * - $elemMatch projection
 *
 * @tags: [assumes_no_implicit_collection_creation_after_drop]
 */
(function() {
    "use strict";

    const testName = "json_schema_misc_validation";
    const testDB = db.getSiblingDB(testName);
    assert.commandWorked(testDB.dropDatabase());
    assert.commandWorked(testDB.createCollection(testName));
    const coll = testDB.getCollection(testName);
    coll.drop();

    const isMongos = (testDB.runCommand("ismaster").msg === "isdbgrid");

    // Test that $jsonSchema is rejected in an $elemMatch projection.
    assert.throws(function() {
        coll.find({}, {a: {$elemMatch: {$jsonSchema: {}}}}).itcount();
    });

    // Test that an invalid $jsonSchema fails to parse in a count command.
    const invalidSchema = {invalid: {}};
    assert.throws(function() {
        coll.count({$jsonSchema: invalidSchema});
    });

    // Test that an invalid $jsonSchema fails to parse in a geoNear command.
    assert.commandWorked(coll.createIndex({geo: "2dsphere"}));
    let res = testDB.runCommand({
        geoNear: coll.getName(),
        near: [30, 40],
        spherical: true,
        query: {$jsonSchema: invalidSchema}
    });
    assert.commandFailed(res);
    assert.neq(-1,
               res.errmsg.indexOf("Can't parse filter"),
               "geoNear command failed for a reason other than invalid query");

    // Test that an invalid $jsonSchema fails to parse in a distinct command.
    assert.throws(function() {
        coll.distinct("a", {$jsonSchema: invalidSchema});
    });

    // Test that an invalid $jsonSchema fails to parse in a $match stage within a view.
    res = testDB.createView("invalid", coll.getName(), [{$match: {$jsonSchema: invalidSchema}}]);
    assert.commandFailedWithCode(res, ErrorCodes.FailedToParse);

    // Test that an invalid $jsonSchema fails to parse in a listCollections command.
    res = testDB.runCommand({listCollections: 1, filter: {$jsonSchema: invalidSchema}});
    assert.commandFailedWithCode(res, ErrorCodes.FailedToParse);

    // Test that a valid $jsonSchema is legal in a count command.
    coll.drop();
    assert.writeOK(coll.insert({a: 1, b: "str"}));
    assert.writeOK(coll.insert({a: 1, b: 1}));
    assert.eq(1,
              coll.count({$jsonSchema: {properties: {a: {type: "number"}, b: {type: "string"}}}}));

    // Test that a valid $jsonSchema is legal in a geoNear command.
    const point = {type: "Point", coordinates: [31.0, 41.0]};
    assert.writeOK(coll.insert({geo: point, a: 1}));
    assert.writeOK(coll.insert({geo: point, a: 0}));
    assert.commandWorked(coll.createIndex({geo: "2dsphere"}));
    res = testDB.runCommand({
        geoNear: coll.getName(),
        near: [30, 40],
        spherical: true,
        includeLocs: true,
        query: {$jsonSchema: {properties: {a: {minimum: 1}}}}
    });
    assert.commandWorked(res);
    assert.eq(1, res.results.length);
    assert.eq(res.results[0].loc, point);

    // Test that a valid $jsonSchema is legal in a distinct command.
    coll.drop();
    assert.writeOK(coll.insert({a: 1}));
    assert.writeOK(coll.insert({a: 2}));
    assert.writeOK(coll.insert({a: "str"}));
    assert.writeOK(coll.insert({a: ["STR", "str"]}));

    assert.eq([1, 2], coll.distinct("a", {$jsonSchema: {properties: {a: {type: "number"}}}}));

    // Test that $jsonSchema in a query does not respect the collection-default collation.
    let schema = {properties: {a: {enum: ["STR"]}}};
    const caseInsensitiveCollation = {locale: "en_US", strength: 1};
    coll.drop();
    assert.commandWorked(
        testDB.createCollection(coll.getName(), {collation: caseInsensitiveCollation}));
    assert.writeOK(coll.insert({a: "str"}));
    assert.writeOK(coll.insert({a: ["STR", "sTr"]}));
    assert.eq(0, coll.find({$jsonSchema: schema}).itcount());
    assert.eq(2, coll.find({$jsonSchema: {properties: {a: {uniqueItems: true}}}}).itcount());
    assert.eq(2, coll.find({a: "STR"}).itcount());

    // Test that $jsonSchema does not respect the collation set explicitly on a query.
    coll.drop();
    assert.writeOK(coll.insert({a: "str"}));
    assert.writeOK(coll.insert({a: ["STR", "sTr"]}));

    if (testDB.getMongo().useReadCommands()) {
        assert.eq(0,
                  coll.find({$jsonSchema: schema}).collation(caseInsensitiveCollation).itcount());
        assert.eq(2,
                  coll.find({$jsonSchema: {properties: {a: {uniqueItems: true}}}})
                      .collation(caseInsensitiveCollation)
                      .itcount());
        assert.eq(2, coll.find({a: "STR"}).collation(caseInsensitiveCollation).itcount());

        // Test that $jsonSchema can be used in a $match stage within a view.
        coll.drop();
        let bulk = coll.initializeUnorderedBulkOp();
        bulk.insert({name: "Peter", age: 65});
        bulk.insert({name: "Paul", age: 105});
        bulk.insert({name: "Mary", age: 10});
        bulk.insert({name: "John", age: "unknown"});
        bulk.insert({name: "Mark"});
        bulk.insert({});
        assert.writeOK(bulk.execute());

        assert.commandWorked(testDB.createView(
            "seniorCitizens", coll.getName(), [{
                $match: {
                    $jsonSchema: {
                        required: ["name", "age"],
                        properties:
                            {name: {type: "string"}, age: {type: "number", minimum: 65}}
                    }
                }
            }]));
        assert.eq(2, testDB.seniorCitizens.find().itcount());
    }

    // Test that $jsonSchema can be used in the listCollections filter.
    res = testDB.runCommand({
        listCollections: 1,
        filter: {$jsonSchema: {properties: {name: {enum: [coll.getName()]}}}}
    });
    assert.commandWorked(res);
    assert.eq(1, res.cursor.firstBatch.length);

    // Test that $jsonSchema can be used in the listDatabases filter.
    res = testDB.adminCommand(
        {listDatabases: 1, filter: {$jsonSchema: {properties: {name: {enum: [coll.getName()]}}}}});
    assert.commandWorked(res);
    assert.eq(1, res.databases.length);

    // Test that $jsonSchema can be used in the filter of a $graphLookup stage.
    const foreign = testDB.json_schema_foreign;
    foreign.drop();
    coll.drop();
    for (let i = 0; i < 10; i++) {
        assert.writeOK(foreign.insert({_id: i, n: [i - 1, i + 1]}));
    }
    assert.writeOK(coll.insert({starting: 0}));

    res = coll.aggregate({
                  $graphLookup: {
                      from: foreign.getName(),
                      startWith: "$starting",
                      connectFromField: "n",
                      connectToField: "_id",
                      as: "integers",
                      restrictSearchWithMatch: {$jsonSchema: {properties: {_id: {maximum: 4}}}}
                  }
              })
              .toArray();
    assert.eq(1, res.length);
    assert.eq(res[0].integers.length, 5);

    // Test that $jsonSchema is legal in a delete command.
    coll.drop();
    assert.writeOK(coll.insert({a: 1}));
    assert.writeOK(coll.insert({a: 2}));
    assert.writeOK(coll.insert({a: "str"}));
    assert.writeOK(coll.insert({a: [3]}));

    schema = {properties: {a: {type: "number", maximum: 2}}};

    res = coll.deleteMany({$jsonSchema: schema});
    assert.eq(2, res.deletedCount);
    assert.eq(0, coll.find({$jsonSchema: schema}).itcount());

    // Test that $jsonSchema does not respect the collation specified in a delete command.
    if (db.getMongo().writeMode() === "commands") {
        res = coll.deleteMany({$jsonSchema: {properties: {a: {enum: ["STR"]}}}},
                              {collation: caseInsensitiveCollation});
        assert.eq(0, res.deletedCount);
    } else {
        res = testDB.runCommand({
            delete: coll.getName(),
            deletes: [{q: {$jsonSchema: {properties: {a: {enum: ["STR"]}}}}}],
            collation: caseInsensitiveCollation,
        });
        assert.eq(res.deletedCount);
    }

    // Test that $jsonSchema is legal in an update command.
    coll.drop();
    assert.writeOK(coll.insert({a: 1}));
    assert.writeOK(coll.insert({a: 2}));

    res = coll.update({$jsonSchema: schema}, {$inc: {a: 1}}, {multi: true});
    assert.writeOK(res);
    assert.eq(2, res.nMatched);
    assert.eq(1, coll.find({$jsonSchema: schema}).itcount());

    // Test that $jsonSchema is legal in a findAndModify command.
    coll.drop();
    assert.writeOK(coll.insert({a: "long_string"}));
    assert.writeOK(coll.insert({a: "short"}));

    schema = {properties: {a: {type: "string", minLength: 6}}};
    res = coll.findAndModify(
        {query: {$jsonSchema: schema}, update: {$set: {a: "extra_long_string"}}});
    assert.eq("long_string", res.a);
    assert.eq(1, coll.find({$jsonSchema: schema}).itcount());

    // Test that $jsonSchema works correctly in the presence of a basic b-tree index.
    coll.drop();
    assert.writeOK(coll.insert({_id: 1, a: 1, b: 1}));
    assert.writeOK(coll.insert({_id: 2, a: 2, b: 2, point: [5, 5]}));
    assert.writeOK(coll.insert({_id: 3, a: "temp text test"}));

    assert.commandWorked(coll.createIndex({a: 1}));
    assert.eq(3, coll.find({$jsonSchema: {}}).itcount());
    assert.eq(2, coll.find({$jsonSchema: {properties: {a: {type: "number"}}}}).itcount());
    assert.eq(
        2,
        coll.find({$jsonSchema: {required: ["a"], properties: {a: {type: "number"}}}}).itcount());
    assert.eq(2,
              coll.find({$or: [{$jsonSchema: {properties: {a: {minimum: 2}}}}, {b: 2}]}).itcount());

    // Test that $jsonSchema works correctly in the presence of a geo index.
    coll.dropIndexes();
    assert.commandWorked(coll.createIndex({point: "2dsphere"}));
    assert.eq(1, coll.find({$jsonSchema: {required: ["point"]}}).itcount());

    assert.eq(1,
              coll.find({
                      $jsonSchema: {properties: {point: {minItems: 2}}},
                      point: {$geoNear: {$geometry: {type: "Point", coordinates: [5, 5]}}}
                  })
                  .itcount());

    coll.dropIndexes();
    assert.commandWorked(coll.createIndex({a: 1, point: "2dsphere"}));
    assert.eq(1, coll.find({$jsonSchema: {required: ["a", "point"]}}).itcount());

    assert.eq(1,
              coll.find({
                      $jsonSchema: {required: ["a"], properties: {a: {minLength: 3}}},
                      point: {$geoNear: {$geometry: {type: "Point", coordinates: [5, 5]}}}
                  })
                  .itcount());

    assert.eq(
        1,
        coll.find({
                $and: [
                    {$jsonSchema: {properties: {point: {maxItems: 2}}}},
                    {point: {$geoNear: {$geometry: {type: "Point", coordinates: [5, 5]}}}, a: 2}
                ]
            })
            .itcount());

    // Test that $jsonSchema works correctly in the presence of a text index.
    coll.dropIndexes();
    assert.commandWorked(coll.createIndex({a: "text"}));
    assert.commandWorked(coll.createIndex({a: 1}));
    assert.eq(3, coll.find({$jsonSchema: {properties: {a: {minLength: 5}}}}).itcount());
    assert.eq(1, coll.find({$jsonSchema: {required: ["a"]}, $text: {$search: "test"}}).itcount());
    assert.eq(
        3,
        coll.find({$or: [{$jsonSchema: {required: ["a"]}}, {$text: {$search: "TEST"}}]}).itcount());
    assert.eq(1, coll.find({$and: [{$jsonSchema: {}}, {$text: {$search: "TEST"}}]}).itcount());

    if (!isMongos) {
        coll.drop();
        assert.writeOK(coll.insert({_id: 0, a: true}));

        // Test $jsonSchema in the precondition checking for applyOps.
        res = testDB.adminCommand({
            applyOps: [
                {op: "u", ns: coll.getFullName(), o2: {_id: 0}, o: {$set: {a: false}}},
            ],
            preCondition: [{
                ns: coll.getFullName(),
                q: {$jsonSchema: {properties: {a: {type: "boolean"}}}},
                res: {a: true}
            }]
        });
        assert.commandWorked(res);
        assert.eq(1, res.applied);

        // Test $jsonSchema in an eval function.
        assert.eq(1,
                  testDB.eval(
                      function(coll, schema) {
                          return db[coll].find({$jsonSchema: schema}).itcount();
                      },
                      coll.getName(),
                      {}));

        assert.eq(1, testDB.eval(function(coll, schema) {
            return db[coll].find({$jsonSchema: schema}).itcount();
        }, coll.getName(), {minProperties: 2}));
    }
}());