summaryrefslogtreecommitdiff
path: root/jstests/core/json_schema/pattern_properties.js
blob: f09a030633a5b789130d91d99d112900de1aa03c (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
// @tags: [
//   requires_non_retryable_commands,
//   sbe_incompatible,
// ]

/**
 * Tests for the JSON Schema 'patternProperties' keyword.
 */
(function() {
"use strict";

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

const coll = db.schema_pattern_properties;

// Test top-level patternProperties.
assertSchemaMatch(
    coll, {patternProperties: {"^a": {type: "number"}, "^b": {type: "string"}}}, {}, true);
assertSchemaMatch(
    coll, {patternProperties: {"^a": {type: "number"}, "^b": {type: "string"}}}, {c: 1}, true);
assertSchemaMatch(coll,
                  {patternProperties: {"^a": {type: "number"}, "^b": {type: "string"}}},
                  {ca: 1, cb: 1},
                  true);
assertSchemaMatch(coll,
                  {patternProperties: {"^a": {type: "number"}, "^b": {type: "string"}}},
                  {a: "str", ca: 1, cb: 1},
                  false);
assertSchemaMatch(coll,
                  {patternProperties: {"^a": {type: "number"}, "^b": {type: "string"}}},
                  {a: 1, b: 1, ca: 1, cb: 1},
                  false);
assertSchemaMatch(coll,
                  {patternProperties: {"^a": {type: "number"}, "^b": {type: "string"}}},
                  {a: 1, b: "str", ca: 1, cb: 1},
                  true);

// Test patternProperties within a nested schema.
assertSchemaMatch(
    coll,
    {properties: {obj: {patternProperties: {"^a": {type: "number"}, "^b": {type: "string"}}}}},
    {},
    true);
assertSchemaMatch(
    coll,
    {properties: {obj: {patternProperties: {"^a": {type: "number"}, "^b": {type: "string"}}}}},
    {obj: 1},
    true);
assertSchemaMatch(
    coll,
    {properties: {obj: {patternProperties: {"^a": {type: "number"}, "^b": {type: "string"}}}}},
    {obj: {}},
    true);
assertSchemaMatch(
    coll,
    {properties: {obj: {patternProperties: {"^a": {type: "number"}, "^b": {type: "string"}}}}},
    {obj: {ca: 1, cb: 1}},
    true);
assertSchemaMatch(
    coll,
    {properties: {obj: {patternProperties: {"^a": {type: "number"}, "^b": {type: "string"}}}}},
    {obj: {ac: "str", ca: 1, cb: 1}},
    false);
assertSchemaMatch(
    coll,
    {properties: {obj: {patternProperties: {"^a": {type: "number"}, "^b": {type: "string"}}}}},
    {obj: {ac: 1, bc: 1, ca: 1, cb: 1}},
    false);
assertSchemaMatch(
    coll,
    {properties: {obj: {patternProperties: {"^a": {type: "number"}, "^b": {type: "string"}}}}},
    {obj: {ac: 1, bc: "str", ca: 1, cb: 1}},
    true);

// Test that 'patternProperties' still applies, even if the field name also appears in
// 'properties'.
assertSchemaMatch(coll,
                  {properties: {aa: {type: "number"}}, patternProperties: {"^a": {type: "string"}}},
                  {aa: 1},
                  false);
assertSchemaMatch(
    coll,
    {
        properties:
            {obj: {properties: {aa: {type: "number"}}, patternProperties: {"^a": {type: "string"}}}}
    },
    {obj: {aa: 1}},
    false);
}());