Debian Codegraph @

test/specs/index.spec.js

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 const chai = require('chai')
2 const mergerModule = require('../../src')
3 const Ajv = require('ajv').default
4 const _ = require('lodash')
5 const $RefParser = require('json-schema-ref-parser')
6
7 const expect = chai.expect
8 const ajv = new Ajv()
9
10 function merger(schema, options) {
11 const result = mergerModule(schema, options)
12 try {
13 if (!ajv.validateSchema(result)) {
14 throw new Error('Schema returned by resolver isn\'t valid.')
15 }
16 return result
17 } catch (e) {
18 if (!/stack/i.test(e.message)) {
19 throw e
20 }
21 }
22 }
23
24 describe('module', function() {
25 it('merges schema with same object reference multiple places', () => {
26 const commonSchema = {
27 allOf: [{
28 properties: {
29 test: true
30 }
31 }]
32 }
33 const result = merger({
34 properties: {
35 list: {
36 items: commonSchema
37 }
38 },
39 allOf: [commonSchema]
40 })
41
42 expect(result).to.eql({
43 properties: {
44 list: {
45 items: {
46 properties: {
47 test: true
48 }
49 }
50 },
51 test: true
52 }
53 })
54 })
55
56 it('does not alter original schema', () => {
57 const schema = {
58 allOf: [{
59 properties: {
60 test: true
61 }
62 }]
63 }
64
65 const result = merger(schema)
66
67 expect(result).to.eql({
68 properties: {
69 test: true
70 }
71 })
72
73 expect(result).not.to.equal(schema) // not strict equal (identity)
74 expect(schema).to.eql({
75 allOf: [{
76 properties: {
77 test: true
78 }
79 }]
80 })
81 })
82
83 it('does not use any original objects or arrays', () => {
84 const schema = {
85 properties: {
86 arr: {
87 type: 'array',
88 items: {
89 type: 'object'
90 },
91 additionalItems: [{
92 type: 'array'
93 }]
94 }
95 },
96 allOf: [{
97 properties: {
98 test: true
99 }
100 }]
101 }
102
103 function innerDeconstruct(schema) {
104 const allChildObj = Object.entries(schema).map(([key, val]) => {
105 if (_.isObject(val)) return innerDeconstruct(val)
106 else return undefined
107 })
108 return [schema, ..._.flatten(allChildObj)]
109 }
110
111 const getAllObjects = schema => _(innerDeconstruct(schema)).compact().value()
112 const inputObjects = getAllObjects(schema)
113
114 const result = merger(schema)
115 const resultObjects = getAllObjects(result)
116
117 const commonObjects = _.intersection(inputObjects, resultObjects)
118 expect(commonObjects).to.have.length(0)
119 })
120
121 it('combines simple usecase', function() {
122 const result = merger({
123 allOf: [{
124 type: 'string',
125 minLength: 1
126 }, {
127 type: 'string',
128 maxLength: 5
129 }]
130 })
131
132 expect(result).to.eql({
133 type: 'string',
134 minLength: 1,
135 maxLength: 5
136 })
137 })
138
139 it('combines without allOf', function() {
140 const result = merger({
141 properties: {
142 foo: {
143 type: 'string'
144 }
145 }
146 })
147
148 expect(result).to.eql({
149 properties: {
150 foo: {
151 type: 'string'
152 }
153 }
154 })
155 })
156
157 describe('simple resolve functionality', function() {
158 it('merges with default resolver if not defined resolver', function() {
159 const result = merger({
160 title: 'schema1',
161 allOf: [{
162 title: 'schema2'
163 }, {
164 title: 'schema3'
165 }]
166 })
167
168 expect(result).to.eql({
169 title: 'schema1'
170 })
171
172 const result3 = merger({
173 allOf: [{
174 title: 'schema2'
175 }, {
176 title: 'schema3'
177 }]
178 })
179
180 expect(result3).to.eql({
181 title: 'schema2'
182 })
183 })
184
185 it('merges minLength if conflict', function() {
186 const result = merger({
187 allOf: [{
188 minLength: 1
189 }, {
190 minLength: 5
191 }]
192 })
193
194 expect(result).to.eql({
195 minLength: 5
196 })
197 })
198
199 it('merges minimum if conflict', function() {
200 const result = merger({
201 allOf: [{
202 minimum: 1
203 }, {
204 minimum: 5
205 }]
206 })
207
208 expect(result).to.eql({
209 minimum: 5
210 })
211 })
212
213 it('merges exclusiveMinimum if conflict', function() {
214 const result = merger({
215 allOf: [{
216 exclusiveMinimum: 1
217 }, {
218 exclusiveMinimum: 5
219 }]
220 })
221
222 expect(result).to.eql({
223 exclusiveMinimum: 5
224 })
225 })
226
227 it('merges minItems if conflict', function() {
228 const result = merger({
229 allOf: [{
230 minItems: 1
231 }, {
232 minItems: 5
233 }]
234 })
235
236 expect(result).to.eql({
237 minItems: 5
238 })
239 })
240
241 it('merges maximum if conflict', function() {
242 const result = merger({
243 allOf: [{
244 maximum: 1
245 }, {
246 maximum: 5
247 }]
248 })
249
250 expect(result).to.eql({
251 maximum: 1
252 })
253 })
254
255 it('merges exclusiveMaximum if conflict', function() {
256 const result = merger({
257 allOf: [{
258 exclusiveMaximum: 1
259 }, {
260 exclusiveMaximum: 5
261 }]
262 })
263
264 expect(result).to.eql({
265 exclusiveMaximum: 1
266 })
267 })
268
269 it('merges maxItems if conflict', function() {
270 const result = merger({
271 allOf: [{
272 maxItems: 1
273 }, {
274 maxItems: 5
275 }]
276 })
277
278 expect(result).to.eql({
279 maxItems: 1
280 })
281 })
282
283 it('merges maxLength if conflict', function() {
284 const result = merger({
285 allOf: [{
286 maxLength: 4
287 }, {
288 maxLength: 5
289 }]
290 })
291
292 expect(result).to.eql({
293 maxLength: 4
294 })
295 })
296
297 it('merges uniqueItems to most restrictive if conflict', function() {
298 const result = merger({
299 allOf: [{
300 uniqueItems: true
301 }, {
302 uniqueItems: false
303 }]
304 })
305
306 expect(result).to.eql({
307 uniqueItems: true
308 })
309
310 expect(merger({
311 allOf: [{
312 uniqueItems: false
313 }, {
314 uniqueItems: false
315 }]
316 })).to.eql({
317 uniqueItems: false
318 })
319 })
320
321 it('throws if merging incompatible type', function() {
322 expect(function() {
323 merger({
324 allOf: [{
325 type: 'null'
326 }, {
327 type: 'text'
328 }]
329 })
330 }).to.throw(/incompatible/)
331 })
332
333 it('merges type if conflict', function() {
334 const result = merger({
335 allOf: [{}, {
336 type: ['string', 'null', 'object', 'array']
337 }, {
338 type: ['string', 'null']
339 }, {
340 type: ['null', 'string']
341 }]
342 })
343
344 expect(result).to.eql({
345 type: ['string', 'null']
346 })
347
348 const result2 = merger({
349 allOf: [{}, {
350 type: ['string', 'null', 'object', 'array']
351 }, {
352 type: 'string'
353 }, {
354 type: ['null', 'string']
355 }]
356 })
357
358 expect(result2).to.eql({
359 type: 'string'
360 })
361
362 expect(function() {
363 merger({
364 allOf: [{
365 type: ['null']
366 }, {
367 type: ['text', 'object']
368 }]
369 })
370 }).to.throw(/incompatible/)
371 })
372
373 it('merges enum', function() {
374 const result = merger({
375 allOf: [{}, {
376 enum: [
377 'string',
378 'null',
379 'object',
380 {},
381 [2],
382 [1],
383 null
384 ]
385 }, {
386 enum: ['string', {},
387 [1],
388 [1]
389 ]
390 }, {
391 enum: [
392 'null',
393 'string',
394 {},
395 [3],
396 [1],
397 null
398 ]
399 }]
400 })
401
402 expect(result).to.eql({
403 enum: [
404 [1], {}, 'string'
405 ]
406 })
407 })
408
409 it('throws if enum is incompatible', function() {
410 expect(function() {
411 merger({
412 allOf: [{}, {
413 enum: ['string', {}]
414 }, {
415 enum: [{}, 'string']
416 }]
417 })
418 }).not.to.throw(/incompatible/)
419
420 expect(function() {
421 merger({
422 allOf: [{}, {
423 enum: ['string', {}]
424 }, {
425 enum: [
426 [], false
427 ]
428 }]
429 })
430 }).to.throw(/incompatible/)
431 })
432
433 it('merges const', function() {
434 const result = merger({
435 allOf: [{}, {
436 const: ['string', {}]
437 }, {
438 const: ['string', {}]
439 }]
440 })
441
442 expect(result).to.eql({
443 const: ['string', {}]
444 })
445 })
446
447 it('merges anyOf', function() {
448 const result = merger({
449 allOf: [{
450 anyOf: [{
451 required: ['123']
452 }]
453 }, {
454 anyOf: [{
455 required: ['123']
456 }, {
457 required: ['456']
458 }]
459 }]
460 })
461
462 expect(result).to.eql({
463 anyOf: [{
464 required: ['123']
465 }, {
466 required: ['123', '456']
467 }]
468 })
469 })
470
471 it('merges anyOf by finding valid combinations', function() {
472 const result = merger({
473 allOf: [{
474 anyOf: [{
475 type: ['null', 'string', 'array']
476 }, {
477 type: ['null', 'string', 'object']
478 }]
479 }, {
480 anyOf: [{
481 type: ['null', 'string']
482 }, {
483 type: ['integer', 'object', 'null']
484 }]
485 }]
486 })
487
488 expect(result).to.eql({
489 anyOf: [{
490 type: ['null', 'string']
491 }, {
492 type: 'null'
493 }, {
494 type: ['object', 'null']
495 }]
496 })
497 })
498
499 it.skip('extracts common logic', function() {
500 const result = merger({
501 allOf: [{
502 anyOf: [{
503 type: [
504 'null', 'string', 'array'
505 ],
506 minLength: 5
507 }, {
508 type: [
509 'null', 'string', 'object'
510 ],
511 minLength: 5
512 }]
513 }, {
514 anyOf: [{
515 type: [
516 'null', 'string'
517 ],
518 minLength: 5
519 }, {
520 type: ['integer', 'object', 'null']
521 }]
522 }]
523 })
524
525 // TODO I think this is correct
526 // TODO implement functionality
527 expect(result).to.eql({
528 type: 'null',
529 minLength: 5,
530 anyOf: [{
531 type: 'string'
532 }]
533 })
534 })
535
536 it.skip('merges anyOf into main schema if left with only one combination', function() {
537 const result = merger({
538 required: ['abc'],
539 allOf: [{
540 anyOf: [{
541 required: ['123']
542 }, {
543 required: ['456']
544 }]
545 }, {
546 anyOf: [{
547 required: ['123']
548 }]
549 }]
550 })
551
552 expect(result).to.eql({
553 required: ['abc', '123']
554 })
555 })
556
557 it('merges nested allOf if inside singular anyOf', function() {
558 const result = merger({
559 allOf: [{
560 anyOf: [{
561 required: ['123'],
562 allOf: [{
563 required: ['768']
564 }]
565 }]
566 }, {
567 anyOf: [{
568 required: ['123']
569 }, {
570 required: ['456']
571 }]
572 }]
573 })
574
575 expect(result).to.eql({
576 anyOf: [{
577 required: ['123', '768']
578 }, {
579 required: ['123', '456', '768']
580 }]
581 })
582 })
583
584 it('throws if no intersection at all', function() {
585 expect(function() {
586 merger({
587 allOf: [{
588 anyOf: [{
589 type: ['object', 'string', 'null']
590 }]
591 }, {
592 anyOf: [{
593 type: ['array', 'integer']
594 }]
595 }]
596 })
597 }).to.throw(/incompatible/)
598
599 expect(function() {
600 merger({
601 allOf: [{
602 anyOf: [{
603 type: ['object', 'string', 'null']
604 }]
605 }, {
606 anyOf: [{
607 type: ['array', 'integer']
608 }]
609 }]
610 })
611 }).to.throw(/incompatible/)
612 })
613
614 it('merges more complex oneOf', function() {
615 const result = merger({
616 allOf: [{
617 oneOf: [{
618 type: [
619 'array', 'string', 'object'
620 ],
621 required: ['123']
622 }, {
623 required: ['abc']
624 }]
625 }, {
626 oneOf: [{
627 type: ['string']
628 }, {
629 type: [
630 'object', 'array'
631 ],
632 required: ['abc']
633 }]
634 }]
635 })
636
637 expect(result).to.eql({
638 oneOf: [{
639 type: 'string',
640 required: ['123']
641 }, {
642 type: [
643 'object', 'array'
644 ],
645 required: ['123', 'abc']
646 }, {
647 type: ['string'],
648 required: ['abc']
649 }, {
650 type: [
651 'object', 'array'
652 ],
653 required: ['abc']
654 }]
655 })
656 })
657
658 it('merges nested allOf if inside singular oneOf', function() {
659 const result = merger({
660 allOf: [{
661 type: [
662 'array', 'string', 'number'
663 ],
664 oneOf: [{
665 required: ['123'],
666 allOf: [{
667 required: ['768']
668 }]
669 }]
670 }, {
671 type: ['array', 'string']
672 }]
673 })
674
675 expect(result).to.eql({
676 type: [
677 'array', 'string'
678 ],
679 oneOf: [{
680 required: ['123', '768']
681 }]
682 })
683 })
684
685 it('merges nested allOf if inside multiple oneOf', function() {
686 const result = merger({
687 allOf: [{
688 type: [
689 'array', 'string', 'number'
690 ],
691 oneOf: [{
692 type: [
693 'array', 'object'
694 ],
695 allOf: [{
696 type: 'object'
697 }]
698 }]
699 }, {
700 type: [
701 'array', 'string'
702 ],
703 oneOf: [{
704 type: 'string'
705 }, {
706 type: 'object'
707 }]
708 }]
709 })
710
711 expect(result).to.eql({
712 type: [
713 'array', 'string'
714 ],
715 oneOf: [{
716 type: 'object'
717 }]
718 })
719 })
720
721 it.skip('throws if no compatible when merging oneOf', function() {
722 expect(function() {
723 merger({
724 allOf: [{}, {
725 oneOf: [{
726 required: ['123']
727 }]
728 }, {
729 oneOf: [{
730 required: ['fdasfd']
731 }]
732 }]
733 })
734 }).to.throw(/incompatible/)
735
736 expect(function() {
737 merger({
738 allOf: [{}, {
739 oneOf: [{
740 required: ['123']
741 }, {
742 properties: {
743 name: {
744 type: 'string'
745 }
746 }
747 }]
748 }, {
749 oneOf: [{
750 required: ['fdasfd']
751 }]
752 }]
753 })
754 }).to.throw(/incompatible/)
755 })
756
757 // not ready to implement this yet
758 it.skip('merges singular oneOf', function() {
759 const result = merger({
760 properties: {
761 name: {
762 type: 'string'
763 }
764 },
765 allOf: [{
766 properties: {
767 name: {
768 type: 'string',
769 minLength: 10
770 }
771 }
772 }, {
773 oneOf: [{
774 required: ['123']
775 }, {
776 properties: {
777 name: {
778 type: 'string',
779 minLength: 15
780 }
781 }
782 }]
783 }, {
784 oneOf: [{
785 required: ['abc']
786 }, {
787 properties: {
788 name: {
789 type: 'string',
790 minLength: 15
791 }
792 }
793 }]
794 }]
795 })
796
797 expect(result).to.eql({
798 properties: {
799 name: {
800 type: 'string',
801 minLength: 15
802 }
803 }
804 })
805 })
806
807 it('merges not using allOf', function() {
808 const result = merger({
809 allOf: [{
810 not: {
811 properties: {
812 name: {
813 type: 'string'
814 }
815 }
816 }
817 }, {
818 not: {
819 properties: {
820 name: {
821 type: ['string', 'null']
822 }
823 }
824 }
825 }]
826 })
827
828 expect(result).to.eql({
829 not: {
830 anyOf: [{
831 properties: {
832 name: {
833 type: 'string'
834 }
835 }
836 },
837 {
838 properties: {
839 name: {
840 type: ['string', 'null']
841 }
842 }
843 }
844 ]
845 }
846 })
847 })
848
849 it('merges contains', function() {
850 const result = merger({
851 allOf: [{}, {
852 contains: {
853 properties: {
854 name: {
855 type: 'string',
856 pattern: 'bar'
857 }
858 }
859 }
860 }, {
861 contains: {
862 properties: {
863 name: {
864 type: 'string',
865 pattern: 'foo'
866 }
867 }
868 }
869 }]
870 })
871
872 expect(result).to.eql({
873 contains: {
874 properties: {
875 name: {
876 type: 'string',
877 pattern: '(?=bar)(?=foo)'
878 }
879 }
880 }
881 })
882 })
883
884 it('merges pattern using allOf', function() {
885 const result = merger({
886 allOf: [{}, {
887 pattern: 'fdsaf'
888 }, {
889 pattern: 'abba'
890 }]
891 })
892
893 expect(result).to.eql({
894 pattern: '(?=fdsaf)(?=abba)'
895 })
896
897 const result2 = merger({
898 allOf: [{
899 pattern: 'abba'
900 }]
901 })
902
903 expect(result2).to.eql({
904 pattern: 'abba'
905 })
906 })
907
908 it('extracts pattern from anyOf and oneOf using | operator in regexp')
909
910 it.skip('merges multipleOf using allOf or direct assignment', function() {
911 const result = merger({
912 allOf: [{
913 title: 'foo',
914 type: [
915 'number', 'integer'
916 ],
917 multipleOf: 2
918 }, {
919 type: 'integer',
920 multipleOf: 3
921 }]
922 })
923
924 expect(result).to.eql({
925 type: 'integer',
926 title: 'foo',
927 allOf: [{
928 multipleOf: 2
929 }, {
930 multipleOf: 3
931 }]
932 })
933
934 const result2 = merger({
935 allOf: [{
936 multipleOf: 1
937 }]
938 })
939
940 expect(result2).to.eql({
941 multipleOf: 1
942 })
943 })
944
945 it('merges multipleOf by finding lowest common multiple (LCM)', function() {
946 const result = merger({
947 allOf: [{}, {
948 multipleOf: 0.2,
949 allOf: [{
950 multipleOf: 2,
951 allOf: [{
952 multipleOf: 2,
953 allOf: [{
954 multipleOf: 2,
955 allOf: [{
956 multipleOf: 3,
957 allOf: [{
958 multipleOf: 1.5,
959 allOf: [{
960 multipleOf: 0.5
961 }]
962 }]
963 }]
964 }]
965 }]
966 }]
967 }, {
968 multipleOf: 0.3
969 }]
970 })
971
972 expect(result).to.eql({
973 multipleOf: 6
974 })
975
976 expect(merger({
977 allOf: [{
978 multipleOf: 4
979 }, {
980 multipleOf: 15
981 }, {
982 multipleOf: 3
983 }]
984 })).to.eql({
985 multipleOf: 60
986 })
987
988 expect(merger({
989 allOf: [{
990 multipleOf: 0.3
991 }, {
992 multipleOf: 0.7
993 }, {
994 multipleOf: 1
995 }]
996 })).to.eql({
997 multipleOf: 21
998 })
999
1000 expect(merger({
1001 allOf: [{
1002 multipleOf: 0.5
1003 }, {
1004 multipleOf: 2
1005 }]
1006 })).to.eql({
1007 multipleOf: 2
1008 })
1009
1010 expect(merger({
1011 allOf: [{
1012 multipleOf: 0.3
1013 }, {
1014 multipleOf: 0.5
1015 }, {
1016 multipleOf: 1
1017 }]
1018 })).to.eql({
1019 multipleOf: 3
1020 })
1021
1022 expect(merger({
1023 allOf: [{
1024 multipleOf: 0.3
1025 }, {
1026 multipleOf: 0.7
1027 }, {
1028 multipleOf: 1
1029 }]
1030 })).to.eql({
1031 multipleOf: 21
1032 })
1033
1034 expect(merger({
1035 allOf: [{
1036 multipleOf: 0.4
1037 }, {
1038 multipleOf: 0.7
1039 }, {
1040 multipleOf: 3
1041 }]
1042 })).to.eql({
1043 multipleOf: 42
1044 })
1045
1046 expect(merger({
1047 allOf: [{
1048 multipleOf: 0.2
1049 }, {
1050 multipleOf: 0.65
1051 }, {
1052 multipleOf: 1
1053 }]
1054 })).to.eql({
1055 multipleOf: 13
1056 })
1057
1058 expect(merger({
1059 allOf: [{
1060 multipleOf: 100000
1061 }, {
1062 multipleOf: 1000000
1063 }, {
1064 multipleOf: 500000
1065 }]
1066 })).to.eql({
1067 multipleOf: 1000000
1068 })
1069 })
1070 })
1071
1072 describe('merging arrays', function() {
1073 it('merges required object', function() {
1074 expect(merger({
1075 required: ['prop2'],
1076 allOf: [{
1077 required: ['prop2', 'prop1']
1078 }]
1079 })).to.eql({
1080 required: ['prop1', 'prop2']
1081 })
1082 })
1083
1084 it('merges default value', function() {
1085 expect(merger({
1086 default: [
1087 'prop2', {
1088 prop1: 'foo'
1089 }
1090 ],
1091 allOf: [{
1092 default: ['prop2', 'prop1']
1093 }]
1094 })).to.eql({
1095 default: [
1096 'prop2', {
1097 prop1: 'foo'
1098 }
1099 ]
1100 })
1101 })
1102
1103 it('merges default value', function() {
1104 expect(merger({
1105 default: {
1106 foo: 'bar'
1107 },
1108 allOf: [{
1109 default: ['prop2', 'prop1']
1110 }]
1111 })).to.eql({
1112 default: {
1113 foo: 'bar'
1114 }
1115 })
1116 })
1117 })
1118
1119 describe('merging objects', function() {
1120 it('merges child objects', function() {
1121 expect(merger({
1122 properties: {
1123 name: {
1124 title: 'Name',
1125 type: 'string'
1126 }
1127 },
1128 allOf: [{
1129 properties: {
1130 name: {
1131 title: 'allof1',
1132 type: 'string'
1133 },
1134 added: {
1135 type: 'integer'
1136 }
1137 }
1138 }, {
1139 properties: {
1140 name: {
1141 type: 'string'
1142 }
1143 }
1144 }]
1145 })).to.eql({
1146 properties: {
1147 name: {
1148 title: 'Name',
1149 type: 'string'
1150 },
1151 added: {
1152 type: 'integer'
1153 }
1154 }
1155 })
1156 })
1157
1158 it('merges boolean schemas', function() {
1159 expect(merger({
1160 properties: {
1161 name: true
1162 },
1163 allOf: [{
1164 properties: {
1165 name: {
1166 title: 'allof1',
1167 type: 'string'
1168 },
1169 added: {
1170 type: 'integer'
1171 }
1172 }
1173 }, {
1174 properties: {
1175 name: {
1176 type: 'string',
1177 minLength: 5
1178 }
1179 }
1180 }]
1181 })).to.eql({
1182 properties: {
1183 name: {
1184 title: 'allof1',
1185 type: 'string',
1186 minLength: 5
1187 },
1188 added: {
1189 type: 'integer'
1190 }
1191 }
1192 })
1193
1194 expect(merger({
1195 properties: {
1196 name: false
1197 },
1198 allOf: [{
1199 properties: {
1200 name: {
1201 title: 'allof1',
1202 type: 'string'
1203 },
1204 added: {
1205 type: 'integer'
1206 }
1207 }
1208 }, {
1209 properties: {
1210 name: true
1211 }
1212 }]
1213 })).to.eql({
1214 properties: {
1215 name: false,
1216 added: {
1217 type: 'integer'
1218 }
1219 }
1220 })
1221
1222 expect(merger({
1223 allOf: [true, false]
1224 })).to.eql(false)
1225
1226 expect(merger({
1227 properties: {
1228 name: true
1229 },
1230 allOf: [{
1231 properties: {
1232 name: false,
1233 added: {
1234 type: 'integer'
1235 }
1236 }
1237 }, {
1238 properties: {
1239 name: true
1240 }
1241 }]
1242 })).to.eql({
1243 properties: {
1244 name: false,
1245 added: {
1246 type: 'integer'
1247 }
1248 }
1249 })
1250 })
1251
1252 it('merges all allOf', function() {
1253 expect(merger({
1254 properties: {
1255 name: {
1256 allOf: [{
1257 pattern: '^.+$'
1258 }]
1259 }
1260 },
1261 allOf: [{
1262 properties: {
1263 name: true,
1264 added: {
1265 type: 'integer',
1266 title: 'pri1',
1267 allOf: [{
1268 title: 'pri2',
1269 type: [
1270 'string', 'integer'
1271 ],
1272 minimum: 15,
1273 maximum: 10
1274 }]
1275 }
1276 },
1277 allOf: [{
1278 properties: {
1279 name: true,
1280 added: {
1281 type: 'integer',
1282 minimum: 5
1283 }
1284 },
1285 allOf: [{
1286 properties: {
1287 added: {
1288 title: 'pri3',
1289 type: 'integer',
1290 minimum: 10
1291 }
1292 }
1293 }]
1294 }]
1295 }, {
1296 properties: {
1297 name: true,
1298 added: {
1299 minimum: 7
1300 }
1301 }
1302 }]
1303 })).to.eql({
1304 properties: {
1305 name: {
1306 pattern: '^.+$'
1307 },
1308 added: {
1309 type: 'integer',
1310 title: 'pri1',
1311 minimum: 15,
1312 maximum: 10
1313 }
1314 }
1315 })
1316 })
1317 })
1318
1319 describe.skip('merging definitions', function() {
1320 it('merges circular', function() {
1321 const schema = {
1322 properties: {
1323 person: {
1324 properties: {
1325 name: {
1326 type: 'string',
1327 minLength: 8
1328 }
1329 },
1330 allOf: [{
1331 properties: {
1332 name: {
1333 minLength: 5,
1334 maxLength: 10
1335 }
1336 },
1337 allOf: [{
1338 properties: {
1339 prop1: {
1340 minLength: 7
1341 }
1342 }
1343 }]
1344 }]
1345 }
1346 }
1347 }
1348
1349 schema.properties.person.properties.child = schema.properties.person
1350
1351 const expected = {
1352 person: {
1353 properties: {
1354 name: {
1355 minLength: 8,
1356 maxLength: 10,
1357 type: 'string'
1358 },
1359 prop1: {
1360 minLength: 7
1361 }
1362 }
1363 }
1364 }
1365
1366 expected.person.properties.child = expected.person
1367
1368 const result = merger(schema)
1369
1370 expect(result).to.eql({
1371 properties: expected
1372 })
1373 })
1374
1375 it('merges any definitions and circular', function() {
1376 const schema = {
1377 properties: {
1378 person: {
1379 $ref: '#/definitions/person'
1380 }
1381 },
1382 definitions: {
1383 person: {
1384 properties: {
1385 name: {
1386 type: 'string',
1387 minLength: 8
1388 },
1389 child: {
1390 $ref: '#/definitions/person'
1391 }
1392 },
1393 allOf: [{
1394 properties: {
1395 name: {
1396 minLength: 5,
1397 maxLength: 10
1398 }
1399 },
1400 allOf: [{
1401 properties: {
1402 prop1: {
1403 minLength: 7
1404 }
1405 }
1406 }]
1407 }]
1408 }
1409 }
1410 }
1411
1412 return $RefParser.dereference(schema).then(function(dereferenced) {
1413 const expected = {
1414 person: {
1415 properties: {
1416 name: {
1417 minLength: 8,
1418 maxLength: 10,
1419 type: 'string'
1420 },
1421 prop1: {
1422 minLength: 7
1423 }
1424 }
1425 }
1426 }
1427
1428 expected.person.properties.child = expected.person
1429
1430 const result = merger(schema)
1431
1432 expect(result).to.eql({
1433 properties: expected,
1434 definitions: expected
1435 })
1436
1437 expect(result).to.equal(dereferenced)
1438
1439 expect(result.properties.person.properties.child).to.equal(result.definitions.person.properties.child)
1440 expect(result.properties.person.properties.child).to.equal(dereferenced.properties.person)
1441 })
1442 })
1443 })
1444
1445 describe('dependencies', function() {
1446 it('merges simliar schemas', function() {
1447 const result = merger({
1448 dependencies: {
1449 foo: {
1450 type: [
1451 'string', 'null', 'integer'
1452 ],
1453 allOf: [{
1454 minimum: 5
1455 }]
1456 },
1457 bar: ['prop1', 'prop2']
1458 },
1459 allOf: [{
1460 dependencies: {
1461 foo: {
1462 type: [
1463 'string', 'null'
1464 ],
1465 allOf: [{
1466 minimum: 7
1467 }]
1468 },
1469 bar: ['prop4']
1470 }
1471 }]
1472 })
1473
1474 expect(result).to.eql({
1475 dependencies: {
1476 foo: {
1477 type: [
1478 'string', 'null'
1479 ],
1480 minimum: 7
1481 },
1482 bar: ['prop1', 'prop2', 'prop4']
1483 }
1484 })
1485 })
1486
1487 it('merges mixed mode dependency', function() {
1488 const result = merger({
1489 dependencies: {
1490 bar: {
1491 type: [
1492 'string', 'null', 'integer'
1493 ],
1494 required: ['abc']
1495 }
1496 },
1497 allOf: [{
1498 dependencies: {
1499 bar: ['prop4']
1500 }
1501 }]
1502 })
1503
1504 expect(result).to.eql({
1505 dependencies: {
1506 bar: {
1507 type: [
1508 'string', 'null', 'integer'
1509 ],
1510 required: ['abc', 'prop4']
1511 }
1512 }
1513 })
1514 })
1515 })
1516
1517 describe('propertyNames', function() {
1518 it('merges simliar schemas', function() {
1519 const result = merger({
1520 propertyNames: {
1521 type: 'string',
1522 allOf: [{
1523 minLength: 5
1524 }]
1525 },
1526 allOf: [{
1527 propertyNames: {
1528 type: 'string',
1529 pattern: 'abc.*',
1530 allOf: [{
1531 maxLength: 7
1532 }]
1533 }
1534 }]
1535 })
1536
1537 expect(result).to.eql({
1538 propertyNames: {
1539 type: 'string',
1540 pattern: 'abc.*',
1541 minLength: 5,
1542 maxLength: 7
1543 }
1544 })
1545 })
1546 })
1547 })