summaryrefslogtreecommitdiff
path: root/jstests/core/json_schema
diff options
context:
space:
mode:
authorDavid Storch <david.storch@10gen.com>2017-08-23 15:53:55 -0400
committerDavid Storch <david.storch@10gen.com>2017-08-24 17:41:30 -0400
commit57f78ab8952dc289e660ad98e104ff0422b8cf20 (patch)
tree8f620f44874f228ca4a12155a4619bae4fcbec11 /jstests/core/json_schema
parent7faaefa4975c2e3ec43e28e38028d9b5215e5978 (diff)
downloadmongo-57f78ab8952dc289e660ad98e104ff0422b8cf20.tar.gz
SERVER-30177 Add support for JSON Schema 'minProperties' and 'maxProperties'.
Diffstat (limited to 'jstests/core/json_schema')
-rw-r--r--jstests/core/json_schema/min_max_properties.js47
1 files changed, 47 insertions, 0 deletions
diff --git a/jstests/core/json_schema/min_max_properties.js b/jstests/core/json_schema/min_max_properties.js
new file mode 100644
index 00000000000..ec11b615329
--- /dev/null
+++ b/jstests/core/json_schema/min_max_properties.js
@@ -0,0 +1,47 @@
+/**
+ * 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);
+}());