UNPKG

3.77 kBJavaScriptView Raw
1'use strict';
2
3var Ajv = require('ajv');
4var addKeywords = require('..');
5var addMerge = require('../keywords/merge');
6var test = require('./test_validate');
7
8describe('keyword $merge', function() {
9 var ajvInstances;
10
11 beforeEach(function() {
12 ajvInstances = [ new Ajv, new Ajv ];
13 addKeywords(ajvInstances[0]);
14 addMerge(ajvInstances[1]);
15 });
16
17 it('should extend schema defined in $merge', function() {
18 ajvInstances.forEach(testMerge);
19
20 function testMerge(ajv) {
21 var schema = {
22 "$merge": {
23 "source": {
24 "type": "object",
25 "properties": { "p": { "type": "string" } },
26 "additionalProperties": false
27 },
28 "with": {
29 "properties": { "q": { "type": "number" } }
30 }
31 }
32 };
33
34 var validate = ajv.compile(schema);
35 test(validate, '$merge');
36 }
37 });
38
39 it('should extend schema defined in $ref', function() {
40 ajvInstances.forEach(testMerge);
41
42 function testMerge(ajv) {
43 var sourceSchema = {
44 "$id": "obj.json#",
45 "type": "object",
46 "properties": { "p": { "type": "string" } },
47 "additionalProperties": false
48 };
49
50 ajv.addSchema(sourceSchema);
51
52 var schema = {
53 "$merge": {
54 "source": { "$ref": "obj.json#" },
55 "with": {
56 "properties": { "q": { "type": "number" } }
57 }
58 }
59 };
60
61 var validate = ajv.compile(schema);
62 test(validate, '$merge');
63 }
64 });
65
66 it('should extend schema defined with relative $ref', function() {
67 ajvInstances.forEach(testMerge);
68
69 function testMerge(ajv) {
70 var schema = {
71 "$id": "obj.json#",
72 "definitions": {
73 "source": {
74 "type": "object",
75 "properties": { "p": { "type": "string" } },
76 "additionalProperties": false
77 }
78 },
79 "$merge": {
80 "source": { "$ref": "#/definitions/source" },
81 "with": {
82 "properties": { "q": { "type": "number" } }
83 }
84 }
85 };
86
87 var validate = ajv.compile(schema);
88 test(validate, '$merge');
89 }
90 });
91
92 it('should extend schema with patch in $ref', function() {
93 ajvInstances.forEach(testMerge);
94
95 function testMerge(ajv) {
96 var sourceSchema = {
97 "type": "object",
98 "properties": { "p": { "type": "string" } },
99 "additionalProperties": false
100 };
101
102 var patchSchema = {
103 "type": "object",
104 "properties": { "q": { "type": "number" } },
105 "additionalProperties": false
106 };
107
108 ajv.addSchema(sourceSchema, "obj1.json#");
109 ajv.addSchema(patchSchema, "obj2.json#");
110
111 var schema = {
112 "$merge": {
113 "source": { "$ref": "obj1.json#" },
114 "with": { "$ref": "obj2.json#" }
115 }
116 };
117
118 var validate = ajv.compile(schema);
119 test(validate, '$merge');
120 }
121 });
122
123 it('should extend schema with patch defined with relative $ref', function() {
124 ajvInstances.forEach(testMerge);
125
126 function testMerge(ajv) {
127 var sourceSchema = {
128 "type": "object",
129 "properties": { "p": { "type": "string" } },
130 "additionalProperties": false
131 };
132
133 ajv.addSchema(sourceSchema, "obj1.json#");
134
135 var schema = {
136 "$id": "obj2.json#",
137 "definitions": {
138 "patch":{
139 "type": "object",
140 "properties": { "q": { "type": "number" } },
141 "additionalProperties": false
142 }
143 },
144 "$merge": {
145 "source": { "$ref": "obj1.json#" },
146 "with": { "$ref": "#/definitions/patch" }
147 }
148 };
149
150 var validate = ajv.compile(schema);
151 test(validate, '$merge');
152 }
153 });
154});