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

/**
 * Tests for the JSON Schema 'minProperties' and 'maxProperties' keywords.
 */
(function() {
"use strict";

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

const coll = db.jstests_schema_min_max_properties;

// Test that {minProperties: 0} matches any object.
assertSchemaMatch(coll, {minProperties: 0}, {}, true);
assertSchemaMatch(coll, {minProperties: 0}, {a: 1}, true);
assertSchemaMatch(coll, {minProperties: 0}, {a: 1, b: 2}, true);

// Test that {maxProperties: 0} matches nothing, since objects always must have the "_id" field
// when inserted into a collection.
assertSchemaMatch(coll, {maxProperties: 0}, {}, false);
assertSchemaMatch(coll, {maxProperties: 0}, {a: 1}, false);
assertSchemaMatch(coll, {maxProperties: 0}, {a: 1, b: 2}, false);

// Test top-level minProperties greater than 0.
assertSchemaMatch(coll, {minProperties: 2}, {_id: 0}, false);
assertSchemaMatch(coll, {minProperties: 2}, {_id: 0, a: 1}, true);
assertSchemaMatch(coll, {minProperties: 2}, {_id: 0, a: 1, b: 2}, true);

// Test top-level maxProperties greater than 0.
assertSchemaMatch(coll, {maxProperties: 2}, {_id: 0}, true);
assertSchemaMatch(coll, {maxProperties: 2}, {_id: 0, a: 1}, true);
assertSchemaMatch(coll, {maxProperties: 2}, {_id: 0, a: 1, b: 2}, false);

// Test nested maxProperties greater than 0.
assertSchemaMatch(coll, {properties: {a: {maxProperties: 1}}}, {a: 1}, true);
assertSchemaMatch(coll, {properties: {a: {maxProperties: 1}}}, {a: {}}, true);
assertSchemaMatch(coll, {properties: {a: {maxProperties: 1}}}, {a: {b: 1}}, true);
assertSchemaMatch(coll, {properties: {a: {maxProperties: 1}}}, {a: {b: 1, c: 1}}, false);

// Test nested maxProperties of 0.
assertSchemaMatch(coll, {properties: {a: {maxProperties: 0}}}, {a: {}}, true);
assertSchemaMatch(coll, {properties: {a: {maxProperties: 0}}}, {a: {b: 1}}, false);

// Test nested minProperties greater than 0.
assertSchemaMatch(coll, {properties: {a: {minProperties: 1}}}, {a: 1}, true);
assertSchemaMatch(coll, {properties: {a: {minProperties: 1}}}, {a: {}}, false);
assertSchemaMatch(coll, {properties: {a: {minProperties: 1}}}, {a: {b: 1}}, true);
assertSchemaMatch(coll, {properties: {a: {minProperties: 1}}}, {a: {b: 1, c: 1}}, true);
}());