diff options
Diffstat (limited to 'jstests/core/json_schema/items.js')
-rw-r--r-- | jstests/core/json_schema/items.js | 114 |
1 files changed, 60 insertions, 54 deletions
diff --git a/jstests/core/json_schema/items.js b/jstests/core/json_schema/items.js index 57974a3d612..3bb71c79c7d 100644 --- a/jstests/core/json_schema/items.js +++ b/jstests/core/json_schema/items.js @@ -4,58 +4,64 @@ * Tests the JSON Schema "items" keyword. */ (function() { - "use strict"; - - load("jstests/libs/assert_schema_match.js"); - - const coll = db.getCollection("json_schema_items"); - coll.drop(); - - // Test that the JSON Schema fails to parse if "items" is not an object or array. - assert.throws(() => coll.find({$jsonSchema: {items: 1}}).itcount()); - assert.throws(() => coll.find({$jsonSchema: {items: 1.0}}).itcount()); - assert.throws(() => coll.find({$jsonSchema: {items: "true"}}).itcount()); - - // Test that "items" has no effect at the top level (but is still accepted). - assertSchemaMatch(coll, {items: {type: "number"}}, {}, true); - assertSchemaMatch(coll, {items: [{type: "number"}]}, {}, true); - - // Test that "items" matches documents where the field is missing or not an array. - assertSchemaMatch(coll, {properties: {a: {items: {minimum: 0}}}}, {}, true); - assertSchemaMatch(coll, {properties: {a: {items: {minimum: 0}}}}, {a: -1}, true); - assertSchemaMatch(coll, {properties: {a: {items: [{minimum: 0}]}}}, {}, true); - assertSchemaMatch(coll, {properties: {a: {items: [{minimum: 0}]}}}, {a: -1}, true); - - // Test that when "items" is an object, the schema applies to all elements of the array. - let schema = {properties: {a: {items: {pattern: "a+b"}}}}; - assertSchemaMatch(coll, schema, {a: []}, true); - assertSchemaMatch(coll, schema, {a: [7]}, true); - assertSchemaMatch(coll, schema, {a: [null]}, true); - assertSchemaMatch(coll, schema, {a: ["cab"]}, true); - assertSchemaMatch(coll, schema, {a: ["cab", "caab"]}, true); - assertSchemaMatch(coll, schema, {a: ["cab", "caab", "b"]}, false); - - // Test that when "items" is an array, each element schema only apply to elements at that - // position. - schema = {properties: {a: {items: [{multipleOf: 2}]}}}; - assertSchemaMatch(coll, schema, {a: []}, true); - assertSchemaMatch(coll, schema, {a: [2]}, true); - assertSchemaMatch(coll, schema, {a: [2, 3]}, true); - assertSchemaMatch(coll, schema, {a: [3]}, false); - - schema = {properties: {a: {items: [{maxLength: 1}, {maxLength: 2}]}}}; - assertSchemaMatch(coll, schema, {a: []}, true); - assertSchemaMatch(coll, schema, {a: ["1"]}, true); - assertSchemaMatch(coll, schema, {a: ["1"]}, true); - assertSchemaMatch(coll, schema, {a: ["1", "12"]}, true); - assertSchemaMatch(coll, schema, {a: ["1", "12", "123"]}, true); - assertSchemaMatch(coll, schema, {a: ["12"]}, false); - assertSchemaMatch(coll, schema, {a: ["1", "123"]}, false); - - // Test that "items" has no effect when it is an empty array (but is still accepted). - schema = {properties: {a: {items: []}}}; - assertSchemaMatch(coll, schema, {}, true); - assertSchemaMatch(coll, schema, {a: "blah"}, true); - assertSchemaMatch(coll, schema, {a: []}, true); - assertSchemaMatch(coll, schema, {a: [1, "foo", {}]}, true); +"use strict"; + +load("jstests/libs/assert_schema_match.js"); + +const coll = db.getCollection("json_schema_items"); +coll.drop(); + +// Test that the JSON Schema fails to parse if "items" is not an object or array. +assert.throws(() => coll.find({$jsonSchema: {items: 1}}).itcount()); +assert.throws(() => coll.find({$jsonSchema: {items: 1.0}}).itcount()); +assert.throws(() => coll.find({$jsonSchema: {items: "true"}}).itcount()); + +// Test that "items" has no effect at the top level (but is still accepted). +assertSchemaMatch(coll, {items: {type: "number"}}, {}, true); +assertSchemaMatch(coll, {items: [{type: "number"}]}, {}, true); + +// Test that "items" matches documents where the field is missing or not an array. +assertSchemaMatch(coll, {properties: {a: {items: {minimum: 0}}}}, {}, true); +assertSchemaMatch(coll, {properties: {a: {items: {minimum: 0}}}}, {a: -1}, true); +assertSchemaMatch(coll, {properties: {a: {items: [{minimum: 0}]}}}, {}, true); +assertSchemaMatch(coll, {properties: {a: {items: [{minimum: 0}]}}}, {a: -1}, true); + +// Test that when "items" is an object, the schema applies to all elements of the array. +let schema = {properties: {a: {items: {pattern: "a+b"}}}}; +assertSchemaMatch(coll, schema, {a: []}, true); +assertSchemaMatch(coll, schema, {a: [7]}, true); +assertSchemaMatch(coll, schema, {a: [null]}, true); +assertSchemaMatch(coll, schema, {a: ["cab"]}, true); +assertSchemaMatch(coll, schema, {a: ["cab", "caab"]}, true); +assertSchemaMatch(coll, schema, {a: ["cab", "caab", "b"]}, false); + +// Test that when "items" is an array, each element schema only apply to elements at that +// position. +schema = { + properties: {a: {items: [{multipleOf: 2}]}} +}; +assertSchemaMatch(coll, schema, {a: []}, true); +assertSchemaMatch(coll, schema, {a: [2]}, true); +assertSchemaMatch(coll, schema, {a: [2, 3]}, true); +assertSchemaMatch(coll, schema, {a: [3]}, false); + +schema = { + properties: {a: {items: [{maxLength: 1}, {maxLength: 2}]}} +}; +assertSchemaMatch(coll, schema, {a: []}, true); +assertSchemaMatch(coll, schema, {a: ["1"]}, true); +assertSchemaMatch(coll, schema, {a: ["1"]}, true); +assertSchemaMatch(coll, schema, {a: ["1", "12"]}, true); +assertSchemaMatch(coll, schema, {a: ["1", "12", "123"]}, true); +assertSchemaMatch(coll, schema, {a: ["12"]}, false); +assertSchemaMatch(coll, schema, {a: ["1", "123"]}, false); + +// Test that "items" has no effect when it is an empty array (but is still accepted). +schema = { + properties: {a: {items: []}} +}; +assertSchemaMatch(coll, schema, {}, true); +assertSchemaMatch(coll, schema, {a: "blah"}, true); +assertSchemaMatch(coll, schema, {a: []}, true); +assertSchemaMatch(coll, schema, {a: [1, "foo", {}]}, true); }()); |