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
|
/**
* Tests the JSON Schema "uniqueItems" keyword.
*/
(function() {
"use strict";
load("jstests/libs/assert_schema_match.js");
const coll = db.getCollection("json_schema_unique_items");
coll.drop();
// Test that the JSON Schema fails to parse if "uniqueItems" is not a boolean.
assert.throws(() => coll.find({$jsonSchema: {uniqueItems: 1}}).itcount());
assert.throws(() => coll.find({$jsonSchema: {uniqueItems: 1.0}}).itcount());
assert.throws(() => coll.find({$jsonSchema: {uniqueItems: "true"}}).itcount());
// Test that "uniqueItems" has no effect at the top level (but still succeeds).
assertSchemaMatch(coll, {uniqueItems: true}, {}, true);
assertSchemaMatch(coll, {uniqueItems: false}, {}, true);
// Test that "uniqueItems" matches when the field is missing or not an array.
let schema = {properties: {a: {uniqueItems: true}}};
assertSchemaMatch(coll, schema, {}, true);
assertSchemaMatch(coll, schema, {a: "foo"}, true);
assertSchemaMatch(coll, schema, {a: {foo: [1, 1], bar: [2, 2]}}, true);
// Test that {uniqueItems: true} matches arrays whose items are all unique.
schema = {properties: {a: {uniqueItems: true}}};
assertSchemaMatch(coll, schema, {a: []}, true);
assertSchemaMatch(coll, schema, {a: [1]}, true);
assertSchemaMatch(coll, schema, {a: [1, 2, 3]}, true);
assertSchemaMatch(coll, schema, {a: ["foo", "FOO"]}, true);
assertSchemaMatch(coll, schema, {a: [{}, "", [], null]}, true);
assertSchemaMatch(coll, schema, {a: [[1, 2], [2, 1]]}, true);
// Test that {uniqueItems: true} rejects arrays with duplicates.
schema = {properties: {a: {uniqueItems: true}}};
assertSchemaMatch(coll, schema, {a: [1, 1]}, false);
assertSchemaMatch(coll, schema, {a: [NumberLong(1), NumberInt(1)]}, false);
assertSchemaMatch(coll, schema, {a: ["foo", "foo"]}, false);
assertSchemaMatch(coll, schema, {a: [{a: 1}, {a: 1}]}, false);
assertSchemaMatch(coll, schema, {a: [[1, 2], [1, 2]]}, false);
assertSchemaMatch(coll, schema, {a: [null, null]}, false);
// TODO (SERVER-30695): uniqueItems should compare objects in a field order-insensitive way when
// testing for uniqueness.
assertSchemaMatch(coll, schema, {a: [{x: 1, y: 1}, {y: 1, x: 1}]}, true);
// Test that {uniqueItems: false} has no effect.
schema = {properties: {a: {uniqueItems: false}}};
assertSchemaMatch(coll, schema, {a: []}, true);
assertSchemaMatch(coll, schema, {a: [1]}, true);
assertSchemaMatch(coll, schema, {a: [1, 2, 3]}, true);
assertSchemaMatch(coll, schema, {a: ["foo", "FOO"]}, true);
assertSchemaMatch(coll, schema, {a: [{}, "", [], null]}, true);
assertSchemaMatch(coll, schema, {a: [[1, 2], [2, 1]]}, true);
assertSchemaMatch(coll, schema, {a: [1, 1]}, true);
assertSchemaMatch(coll, schema, {a: [NumberLong(1), NumberInt(1)]}, true);
assertSchemaMatch(coll, schema, {a: ["foo", "foo"]}, true);
assertSchemaMatch(coll, schema, {a: [{a: 1}, {a: 1}]}, true);
assertSchemaMatch(coll, schema, {a: [[1, 2], [1, 2]]}, true);
assertSchemaMatch(coll, schema, {a: [null, null]}, true);
}());
|