summaryrefslogtreecommitdiff
path: root/jstests/core/json_schema/logical_keywords.js
blob: 0b7727b49d8e204dbb909ffef2977287781ad673 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// @tags: [
//   requires_non_retryable_commands,
//   sbe_incompatible,
// ]

/**
 * Tests for the JSON Schema logical keywords, including:
 *
 *  - allOf
 *  - anyOf
 *  - oneOf
 *  - not
 *  - enum
 */
(function() {
"use strict";

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

const coll = db.jstests_json_schema_logical;

// Test that $jsonSchema fails to parse if the values for the allOf, anyOf, and oneOf
// keywords are not arrays of valid schema.
assert.throws(function() {
    coll.find({$jsonSchema: {properties: {foo: {allOf: {maximum: "0"}}}}}).itcount();
});
assert.throws(function() {
    coll.find({$jsonSchema: {properties: {foo: {allOf: [0]}}}}).itcount();
});
assert.throws(function() {
    coll.find({$jsonSchema: {properties: {foo: {allOf: [{invalid: "0"}]}}}}).itcount();
});
assert.throws(function() {
    coll.find({$jsonSchema: {properties: {foo: {anyOf: {maximum: "0"}}}}}).itcount();
});
assert.throws(function() {
    coll.find({$jsonSchema: {properties: {foo: {anyOf: [0]}}}}).itcount();
});
assert.throws(function() {
    coll.find({$jsonSchema: {properties: {foo: {anyOf: [{invalid: "0"}]}}}}).itcount();
});
assert.throws(function() {
    coll.find({$jsonSchema: {properties: {foo: {oneOf: {maximum: "0"}}}}}).itcount();
});
assert.throws(function() {
    coll.find({$jsonSchema: {properties: {foo: {oneOf: [0]}}}}).itcount();
});
assert.throws(function() {
    coll.find({$jsonSchema: {properties: {foo: {oneOf: [{invalid: "0"}]}}}}).itcount();
});

// Test that $jsonSchema fails to parse if the value for the 'not' keyword is not a
// valid schema object.
assert.throws(function() {
    coll.find({$jsonSchema: {properties: {foo: {not: {maximum: "0"}}}}}).itcount();
});
assert.throws(function() {
    coll.find({$jsonSchema: {properties: {foo: {not: [0]}}}}).itcount();
});
assert.throws(function() {
    coll.find({$jsonSchema: {properties: {foo: {not: [{}]}}}}).itcount();
});

// Test that the 'allOf' keyword correctly returns documents that match every schema in
// the array.
let schema = {properties: {foo: {allOf: [{minimum: 1}]}}};
assertSchemaMatch(coll, schema, {foo: 1}, true);
assertSchemaMatch(coll, schema, {foo: 0}, false);
assertSchemaMatch(coll, schema, {foo: "string"}, true);

schema = {
    properties: {foo: {allOf: [{}]}}
};
assertSchemaMatch(coll, schema, {foo: {}}, true);
assertSchemaMatch(coll, schema, {}, true);
assertSchemaMatch(coll, schema, {foo: 0}, true);

schema = {
    properties: {foo: {allOf: [{type: 'number'}, {minimum: 0}]}}
};
assertSchemaMatch(coll, schema, {foo: 0}, true);
assertSchemaMatch(coll, schema, {foo: "string"}, false);
assertSchemaMatch(coll, schema, {foo: [0]}, false);

// Test that a top-level 'allOf' keyword matches the correct documents.
assertSchemaMatch(coll, {allOf: [{}]}, {}, true);
assertSchemaMatch(coll, {allOf: [{}]}, {foo: 0}, true);
assertSchemaMatch(coll, {allOf: [{type: 'string'}]}, {}, false);
assertSchemaMatch(coll, {allOf: [{properties: {foo: {type: 'string'}}}]}, {foo: "str"}, true);
assertSchemaMatch(coll, {allOf: [{properties: {foo: {type: 'string'}}}]}, {foo: 1}, false);

// Test that 'allOf' in conjunction with another keyword matches the correct documents.
assertSchemaMatch(
    coll, {properties: {foo: {type: "number", allOf: [{minimum: 1}]}}}, {foo: 1}, true);
assertSchemaMatch(
    coll, {properties: {foo: {type: "number", allOf: [{minimum: 1}]}}}, {foo: "str"}, false);

// Test that the 'anyOf' keyword correctly returns documents that match at least one schema
// in the array.
schema = {
    properties: {foo: {anyOf: [{type: 'string'}, {type: 'number', minimum: 1}]}}
};
assertSchemaMatch(coll, schema, {foo: "str"}, true);
assertSchemaMatch(coll, schema, {foo: 1}, true);
assertSchemaMatch(coll, schema, {foo: 0}, false);

schema = {
    properties: {foo: {anyOf: [{type: 'string'}, {type: 'object'}]}}
};
assertSchemaMatch(coll, schema, {foo: {}}, true);
assertSchemaMatch(coll, schema, {foo: "str"}, true);
assertSchemaMatch(coll, schema, {foo: [{}]}, false);

schema = {
    properties: {foo: {anyOf: [{}]}}
};
assertSchemaMatch(coll, schema, {}, true);
assertSchemaMatch(coll, schema, {foo: {}}, true);
assertSchemaMatch(coll, schema, {foo: 0}, true);

// Test that a top-level 'anyOf' keyword matches the correct documents.
schema = {
    anyOf: [{}]
};
assertSchemaMatch(coll, schema, {}, true);
assertSchemaMatch(coll, schema, {foo: 1}, true);

schema = {
    anyOf: [{properties: {foo: {type: 'string'}}}]
};
assertSchemaMatch(coll, schema, {}, true);
assertSchemaMatch(coll, schema, {foo: "str"}, true);
assertSchemaMatch(coll, schema, {foo: 1}, false);

// Test that 'anyOf' in conjunction with another keyword matches the correct documents.
schema = {
    properties: {foo: {type: "number", anyOf: [{minimum: 1}]}}
};
assertSchemaMatch(coll, schema, {foo: 1}, true);
assertSchemaMatch(coll, schema, {foo: "str"}, false);

// Test that the 'oneOf' keyword correctly returns documents that match exactly one schema
// in the array.
schema = {
    properties: {foo: {oneOf: [{minimum: 0}, {maximum: 3}]}}
};
assertSchemaMatch(coll, schema, {foo: 4}, true);
assertSchemaMatch(coll, schema, {foo: 1}, false);
assertSchemaMatch(coll, schema, {foo: "str"}, false);

schema = {
    properties: {foo: {oneOf: [{type: 'string'}, {pattern: "ing"}]}}
};
assertSchemaMatch(coll, schema, {foo: "str"}, true);
assertSchemaMatch(coll, schema, {foo: "string"}, false);

schema = {
    properties: {foo: {oneOf: [{}]}}
};
assertSchemaMatch(coll, schema, {}, true);
assertSchemaMatch(coll, schema, {foo: 1}, true);

// Test that a top-level 'oneOf' keyword matches the correct documents.
schema = {
    oneOf: [{}]
};
assertSchemaMatch(coll, schema, {}, true);
assertSchemaMatch(coll, schema, {foo: 1}, true);

schema = {
    oneOf: [{properties: {foo: {type: 'string'}}}]
};
assertSchemaMatch(coll, schema, {}, true);
assertSchemaMatch(coll, schema, {foo: "str"}, true);
assertSchemaMatch(coll, schema, {foo: 1}, false);

assertSchemaMatch(coll, {oneOf: [{}, {}]}, {}, false);

// Test that 'oneOf' in conjunction with another keyword matches the correct documents.
schema = {
    properties: {foo: {type: "number", oneOf: [{minimum: 4}]}}
};
assertSchemaMatch(coll, schema, {foo: 4}, true);
assertSchemaMatch(coll, schema, {}, true);
assertSchemaMatch(coll, schema, {foo: "str"}, false);

// Test that the 'not' keyword correctly returns documents that do not match any schema
// in the array.
schema = {
    properties: {foo: {not: {type: 'number'}}}
};
assertSchemaMatch(coll, schema, {}, true);
assertSchemaMatch(coll, schema, {foo: "str"}, true);
assertSchemaMatch(coll, schema, {foo: 1}, false);

schema = {
    properties: {foo: {not: {}}}
};
assertSchemaMatch(coll, schema, {}, true);
assertSchemaMatch(coll, schema, {foo: 1}, false);

// Test that a top-level 'not' keyword matches the correct documents.
assertSchemaMatch(coll, {not: {}}, {}, false);

schema = {
    not: {properties: {foo: {type: 'string'}}}
};
assertSchemaMatch(coll, schema, {foo: 1}, true);
assertSchemaMatch(coll, schema, {foo: "str"}, false);
assertSchemaMatch(coll, schema, {}, false);

// Test that 'not' in conjunction with another keyword matches the correct documents.
schema = {
    properties: {foo: {type: "string", not: {maxLength: 4}}}
};
assertSchemaMatch(coll, schema, {}, true);
assertSchemaMatch(coll, schema, {foo: "string"}, true);
assertSchemaMatch(coll, schema, {foo: "str"}, false);
assertSchemaMatch(coll, schema, {foo: 1}, false);

// Test that the 'enum' keyword correctly matches scalar values.
schema = {
    properties: {a: {enum: ["str", 5]}}
};
assertSchemaMatch(coll, schema, {a: "str"}, true);
assertSchemaMatch(coll, schema, {a: 5}, true);
assertSchemaMatch(coll, schema, {}, true);
assertSchemaMatch(coll, schema, {a: ["str"]}, false);

// Test that the 'enum' keyword with a null value correctly matches literal null elements, but
// not 'missing' or 'undefined.
schema = {
    properties: {a: {enum: [null]}}
};
assertSchemaMatch(coll, schema, {a: null}, true);
assertSchemaMatch(coll, schema, {a: undefined}, false);
assertSchemaMatch(coll, schema, {a: 1}, false);
assertSchemaMatch(coll, {properties: {a: {enum: [null]}}, required: ['a']}, {}, false);

// Test that the 'enum' keyword correctly matches array values.
schema = {
    properties: {a: {enum: [[1, 2, "3"]]}}
};
assertSchemaMatch(coll, schema, {a: [1, 2, "3"]}, true);
assertSchemaMatch(coll, schema, {}, true);
assertSchemaMatch(coll, schema, {a: [2, "3", 1]}, false);

schema = {
    properties: {a: {enum: [[]]}}
};
assertSchemaMatch(coll, schema, {a: []}, true);
assertSchemaMatch(coll, schema, {}, true);
assertSchemaMatch(coll, schema, {a: [1]}, false);

// Test that the 'enum' keyword does not traverse arrays when matching.
schema = {
    properties: {a: {enum: ["str", 1]}}
};
assertSchemaMatch(coll, schema, {a: ["str"]}, false);
assertSchemaMatch(coll, schema, {a: [1]}, false);

// Test that the 'enum' keyword matches objects regardless of the field ordering.
schema = {
    properties: {a: {enum: [{name: "tiny", size: "large"}]}}
};
assertSchemaMatch(coll, schema, {a: {name: "tiny", size: "large"}}, true);
assertSchemaMatch(coll, schema, {a: {size: "large", name: "tiny"}}, true);

// Test that the 'enum' keyword does not match documents with additional fields.
assertSchemaMatch(
    coll, {properties: {a: {enum: [{name: "tiny"}]}}}, {a: {size: "large", name: "tiny"}}, false);

// Test that a top-level 'enum' matches the correct documents.
assertSchemaMatch(coll, {enum: [{_id: 0}]}, {_id: 0}, true);
assertSchemaMatch(coll, {enum: [{_id: 0, a: "str"}]}, {_id: 0, a: "str"}, true);
assertSchemaMatch(coll, {enum: [{}]}, {}, false);
assertSchemaMatch(coll, {enum: [null]}, {}, false);
assertSchemaMatch(coll, {enum: [{_id: 0, a: "str"}]}, {_id: 0, a: "str", b: 1}, false);
assertSchemaMatch(coll, {enum: [1, 2]}, {}, false);
}());