summaryrefslogtreecommitdiff
path: root/jstests/core/json_schema/min_max_items.js
blob: 1dff469747da4f4bfd76f65d833d884de00971f0 (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
// @tags: [requires_non_retryable_commands]

/**
 * Tests the JSON Schema keywords "minItems" and "maxItems".
 */
(function() {
    "use strict";

    load("jstests/libs/assert_schema_match.js");

    const coll = db.getCollection("json_schema_min_max_items");
    coll.drop();

    // Test that the JSON Schema fails to parse if "minItems" is not a valid number.
    assert.throws(() => coll.find({$jsonSchema: {minItems: "blah"}}).itcount());
    assert.throws(() => coll.find({$jsonSchema: {minItems: -1}}).itcount());
    assert.throws(() => coll.find({$jsonSchema: {minItems: 12.5}}).itcount());

    // Test that "minItems" matches when the field is missing or not an array.
    assertSchemaMatch(coll, {properties: {a: {minItems: 1}}}, {}, true);
    assertSchemaMatch(coll, {properties: {a: {minItems: 1}}}, {a: "foo"}, true);

    // Test that "minItems" matches arrays with the requisite number of items.
    assertSchemaMatch(coll, {properties: {a: {minItems: 1}}}, {a: []}, false);
    assertSchemaMatch(coll, {properties: {a: {minItems: 1}}}, {a: ["x"]}, true);
    assertSchemaMatch(coll, {properties: {a: {minItems: 1}}}, {a: [0, 1]}, true);

    // Test that "minItems" has no effect when specified at the top level.
    assertSchemaMatch(coll, {minItems: 2}, {}, true);

    // Test that the JSON Schema fails to parse if "maxItems" is not a valid number.
    assert.throws(() => coll.find({$jsonSchema: {maxItems: "blah"}}).itcount());
    assert.throws(() => coll.find({$jsonSchema: {maxItems: -1}}).itcount());
    assert.throws(() => coll.find({$jsonSchema: {maxItems: 12.5}}).itcount());

    // Test that "maxItems" matches when the field is missing or not an array.
    assertSchemaMatch(coll, {properties: {a: {maxItems: 1}}}, {}, true);
    assertSchemaMatch(coll, {properties: {a: {maxItems: 1}}}, {a: "foo"}, true);

    // Test that "maxItems" matches arrays with the requisite number of items.
    assertSchemaMatch(coll, {properties: {a: {maxItems: 1}}}, {a: []}, true);
    assertSchemaMatch(coll, {properties: {a: {maxItems: 1}}}, {a: ["x"]}, true);
    assertSchemaMatch(coll, {properties: {a: {maxItems: 1}}}, {a: [0, 1]}, false);

    // Test that "maxItems" has no effect when specified at the top level.
    assertSchemaMatch(coll, {maxItems: 2}, {}, true);
}());