Debian Codegraph @

test/specs/properties.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 merger = require('../../src')
3 const sinon = require('sinon')
4 const _ = require('lodash')
5 const expect = chai.expect
6 const Ajv = require('ajv').default
7
8 const ajv = new Ajv({
9 allowMatchingProperties: true
10 })
11 describe('properties', function() {
12 describe('when property name has same as a reserved word', function() {
13 it('does not treat it as a reserved word', function() {
14 const stub = sinon.stub().returns({
15 properties: {
16 properties: {
17 type: 'string',
18 minLength: 5
19 }
20 }
21 })
22
23 merger({
24 allOf: [{
25 properties: {
26 properties: {
27 type: 'string',
28 minLength: 5
29 }
30 }
31 }, {
32 properties: {
33 properties: {
34 type: 'string',
35 minLength: 7
36 }
37 }
38 }]
39 }, {
40 complexResolvers: {
41 properties: {
42 keywords: ['properties', 'patternProperties', 'additionalProperties'],
43 resolver: stub
44 }
45 }
46 })
47
48 sinon.assert.calledOnce(stub)
49 })
50 })
51
52 describe('additionalProperties', function() {
53 it('allows no extra properties if additionalProperties is false', function() {
54 const result = merger({
55 allOf: [{
56 additionalProperties: true
57 }, {
58 additionalProperties: false
59 }]
60 })
61
62 expect(result).to.eql({
63 additionalProperties: false
64 })
65 })
66
67 it('allows only intersecting properties', function() {
68 const result = merger({
69 allOf: [{
70 properties: {
71 foo: true
72 },
73 additionalProperties: true
74 }, {
75 properties: {
76 bar: true
77 },
78 additionalProperties: false
79 }]
80 })
81
82 expect(result).to.eql({
83 properties: {
84 bar: true
85 },
86 additionalProperties: false
87 })
88 })
89
90 it('allows intersecting patternproperties', function() {
91 const result = merger({
92 allOf: [{
93 properties: {
94 foo: true,
95 foo123: true
96 },
97 additionalProperties: true
98 }, {
99 properties: {
100 bar: true
101 },
102 patternProperties: {
103 '.+\\d+$': true
104 },
105 additionalProperties: false
106 }]
107 })
108
109 expect(result).to.eql({
110 properties: {
111 bar: true,
112 foo123: true
113 },
114 patternProperties: {
115 '.+\\d+$': true
116 },
117 additionalProperties: false
118 })
119 })
120
121 it('disallows all except matching patternProperties if both false', function() {
122 const result = merger({
123 allOf: [{
124 properties: {
125 foo: true,
126 foo123: true
127 },
128 additionalProperties: false
129 }, {
130 properties: {
131 bar: true
132 },
133 patternProperties: {
134 '.+\\d+$': true
135 },
136 additionalProperties: false
137 }]
138 })
139
140 expect(result).to.eql({
141 properties: {
142 foo123: true
143 },
144 additionalProperties: false
145 })
146 })
147
148 it('disallows all except matching patternProperties if both false', function() {
149 const result = merger({
150 allOf: [{
151 properties: {
152 foo: true,
153 foo123: true
154 },
155 patternProperties: {
156 '.+\\d+$': {
157 type: 'string'
158 }
159 },
160 additionalProperties: false
161 }, {
162 properties: {
163 bar: true,
164 bar123: true
165 },
166 patternProperties: {
167 '.+\\d+$': true
168 },
169 additionalProperties: false
170 }]
171 })
172
173 expect(result).to.eql({
174 properties: {
175 foo123: true,
176 bar123: true
177 },
178 patternProperties: {
179 '.+\\d+$': {
180 type: 'string'
181 }
182 },
183 additionalProperties: false
184 })
185 })
186
187 it('disallows all except matching patternProperties if both false', function() {
188 const schema = {
189 allOf: [{
190 type: 'object',
191 properties: {
192 foo: true,
193 foo123: true
194 },
195 patternProperties: {
196 '^bar': true
197 },
198 additionalProperties: false
199 }, {
200 type: 'object',
201 properties: {
202 bar: true,
203 bar123: true
204 },
205 patternProperties: {
206 '.+\\d+$': true
207 },
208 additionalProperties: false
209 }]
210 }
211 const origSchema = _.cloneDeep(schema)
212 const result = merger(schema)
213 expect(result).not.to.eql(origSchema)
214
215 expect(result).to.eql({
216 type: 'object',
217 properties: {
218 bar: true,
219 foo123: true,
220 bar123: true
221 },
222 additionalProperties: false
223 })
224
225 ;
226 [{
227 foo123: 'testfdsdfsfd'
228 }, {
229 bar123: 'testfdsdfsfd'
230 }, {
231 foo123: 'testfdsdfsfd'
232 }, {
233 bar: 'fdsaf'
234 }, {
235 abc123: 'fdsaf'
236 }, {
237 bar123: 'fdsaf'
238 }, {
239 barabc: 'fdsaf'
240 }, {
241 // additionalProp
242 foo234: 'testffdsafdsads'
243 }].forEach(function(val) {
244 validateInputOutput(origSchema, result, val)
245 })
246 })
247
248 it('disallows all except matching patternProperties if both true', function() {
249 const schema = {
250 allOf: [{
251 type: 'object',
252 properties: {
253 foo: true,
254 foo123: true
255 },
256 patternProperties: {
257 '^bar': true
258 }
259 }, {
260 type: 'object',
261 properties: {
262 bar: true,
263 bar123: true
264 },
265 patternProperties: {
266 '.+\\d+$': true
267 }
268 }]
269 }
270 const origSchema = _.cloneDeep(schema)
271 const result = merger(schema)
272 expect(result).not.to.eql(origSchema)
273
274 expect(result).to.eql({
275 type: 'object',
276 properties: {
277 foo: true,
278 bar: true,
279 foo123: true,
280 bar123: true
281 },
282 patternProperties: {
283 '^bar': true,
284 '.+\\d+$': true
285 }
286 })
287
288 ;
289 [{
290 foo123: 'testfdsdfsfd'
291 }, {
292 bar123: 'testfdsdfsfd'
293 }, {
294 foo123: 'testfdsdfsfd'
295 }, {
296 foo: 'fdsaf'
297 }, {
298 bar: 'fdsaf'
299 }, {
300 abc123: 'fdsaf'
301 }, {
302 bar123: 'fdsaf'
303 }, {
304 barabc: 'fdsaf'
305 }, {
306 foo234: 'testffdsafdsads'
307 }].forEach(function(val) {
308 validateInputOutput(origSchema, result, val)
309 })
310 })
311
312 it('disallows all except matching patternProperties if one false', function() {
313 const schema = {
314 allOf: [{
315 type: 'object',
316 properties: {
317 foo: true,
318 foo123: true
319 }
320 }, {
321 type: 'object',
322 properties: {
323 bar: true,
324 bar123: true
325 },
326 patternProperties: {
327 '.+\\d+$': true
328 },
329 additionalProperties: false
330 }]
331 }
332 const origSchema = _.cloneDeep(schema)
333 const result = merger(schema)
334 expect(result).not.to.eql(origSchema)
335
336 expect(result).to.eql({
337 type: 'object',
338 properties: {
339 bar: true,
340 foo123: true,
341 bar123: true
342 },
343 patternProperties: {
344 '.+\\d+$': true
345 },
346 additionalProperties: false
347 })
348
349 ;
350 [{
351 foo123: 'testfdsdfsfd'
352 }, {
353 bar123: 'testfdsdfsfd'
354 }, {
355 foo123: 'testfdsdfsfd'
356 }, {
357 foo: 'fdsaf'
358 }, {
359 bar: 'fdsaf'
360 }, {
361 abc123: 'fdsaf'
362 }, {
363 bar123: 'fdsaf'
364 }, {
365 barabc: 'fdsaf'
366 }, {
367 foo234: 'testffdsafdsads'
368 }].forEach(function(val) {
369 validateInputOutput(origSchema, result, val)
370 })
371 })
372
373 it('disallows all if no patternProperties and if both false', function() {
374 const result = merger({
375 allOf: [{
376 properties: {
377 foo: true,
378 foo123: true
379 },
380 additionalProperties: false
381 }, {
382 properties: {
383 bar: true
384 },
385 additionalProperties: false
386 }]
387 })
388
389 expect(result).to.eql({
390 additionalProperties: false
391 })
392 })
393
394 it('applies additionalProperties to other schemas properties if they have any', function() {
395 const result = merger({
396 properties: {
397 common: true,
398 root: true
399 },
400 additionalProperties: false,
401 allOf: [{
402 properties: {
403 common: {
404 type: 'string'
405 },
406 allof1: true
407 },
408 additionalProperties: {
409 type: [
410 'string', 'null'
411 ],
412 maxLength: 10
413 }
414 }, {
415 properties: {
416 common: {
417 minLength: 1
418 },
419 allof2: true
420 },
421 additionalProperties: {
422 type: [
423 'string', 'integer', 'null'
424 ],
425 maxLength: 8
426 }
427 }, {
428 properties: {
429 common: {
430 minLength: 6
431 },
432 allof3: true
433 }
434 }]
435 })
436
437 expect(result).to.eql({
438 properties: {
439 common: {
440 type: 'string',
441 minLength: 6
442 },
443 root: {
444 type: [
445 'string', 'null'
446 ],
447 maxLength: 8
448 }
449 },
450 additionalProperties: false
451 })
452 })
453
454 it('considers patternProperties before merging additionalProperties to other schemas properties if they have any', function() {
455 const result = merger({
456 properties: {
457 common: true,
458 root: true
459 },
460 patternProperties: {
461 '.+\\d{2,}$': {
462 minLength: 7
463 }
464 },
465 additionalProperties: false,
466 allOf: [{
467 properties: {
468 common: {
469 type: 'string'
470 },
471 allof1: true
472 },
473 additionalProperties: {
474 type: [
475 'string', 'null', 'integer'
476 ],
477 maxLength: 10
478 }
479 }, {
480 properties: {
481 common: {
482 minLength: 1
483 },
484 allof2: true,
485 allowed123: {
486 type: 'string'
487 }
488 },
489 patternProperties: {
490 '.+\\d{2,}$': {
491 minLength: 9
492 }
493 },
494 additionalProperties: {
495 type: [
496 'string', 'integer', 'null'
497 ],
498 maxLength: 8
499 }
500 }, {
501 properties: {
502 common: {
503 minLength: 6
504 },
505 allof3: true,
506 allowed456: {
507 type: 'integer'
508 }
509 }
510 }]
511 })
512
513 expect(result).to.eql({
514 properties: {
515 common: {
516 type: 'string',
517 minLength: 6
518 },
519 root: {
520 type: [
521 'string', 'null', 'integer'
522 ],
523 maxLength: 8
524 },
525 allowed123: {
526 type: 'string',
527 maxLength: 10
528 },
529 allowed456: {
530 type: 'integer',
531 maxLength: 10
532 }
533 },
534 patternProperties: {
535 '.+\\d{2,}$': {
536 minLength: 9
537 }
538 },
539 additionalProperties: false
540 })
541 })
542
543 it('combines additionalProperties when schemas', function() {
544 const result = merger({
545 additionalProperties: true,
546 allOf: [{
547 additionalProperties: {
548 type: [
549 'string', 'null'
550 ],
551 maxLength: 10
552 }
553 }, {
554 additionalProperties: {
555 type: [
556 'string', 'integer', 'null'
557 ],
558 maxLength: 8
559 }
560 }]
561 })
562
563 expect(result).to.eql({
564 additionalProperties: {
565 type: [
566 'string', 'null'
567 ],
568 maxLength: 8
569 }
570 })
571 })
572 })
573
574 describe('patternProperties', function() {
575 it('merges simliar schemas', function() {
576 const result = merger({
577 patternProperties: {
578 '^\\$.+': {
579 type: [
580 'string', 'null', 'integer'
581 ],
582 allOf: [{
583 minimum: 5
584 }]
585 }
586 },
587 allOf: [{
588 patternProperties: {
589 '^\\$.+': {
590 type: [
591 'string', 'null'
592 ],
593 allOf: [{
594 minimum: 7
595 }]
596 },
597 '.*': {
598 type: 'null'
599 }
600 }
601 }]
602 })
603
604 expect(result).to.eql({
605 patternProperties: {
606 '^\\$.+': {
607 type: [
608 'string', 'null'
609 ],
610 minimum: 7
611 },
612 '.*': {
613 type: 'null'
614 }
615 }
616 })
617 })
618 })
619
620 describe('when patternProperties present', function() {
621 it('merges patternproperties', function() {
622 const result = merger({
623 allOf: [{
624 patternProperties: {
625 '.*': {
626 type: 'string',
627 minLength: 5
628 }
629 }
630 }, {
631 patternProperties: {
632 '.*': {
633 type: 'string',
634 minLength: 7
635 }
636 }
637 }]
638 })
639
640 expect(result).to.eql({
641 patternProperties: {
642 '.*': {
643 type: 'string',
644 minLength: 7
645 }
646 }
647 })
648 })
649
650 it('merges with properties if matching property name', function() {
651 const schema = {
652 allOf: [{
653 type: 'object',
654 properties: {
655 name: {
656 type: 'string',
657 minLength: 1
658 }
659 },
660 patternProperties: {
661 _long$: {
662 type: 'string',
663 minLength: 7
664 }
665 }
666 }, {
667 type: 'object',
668 properties: {
669 foo_long: {
670 type: 'string',
671 minLength: 9
672 }
673 },
674 patternProperties: {
675 '^name.*': {
676 type: 'string',
677 minLength: 8
678 }
679 }
680 }]
681 }
682
683 const origSchema = _.cloneDeep(schema)
684 const result = merger(schema)
685
686 expect(result).not.to.eql(origSchema)
687
688 expect(result).to.eql({
689 type: 'object',
690 properties: {
691 foo_long: {
692 type: 'string',
693 minLength: 9
694 },
695 name: {
696 type: 'string',
697 minLength: 1
698 }
699 },
700 patternProperties: {
701 _long$: {
702 type: 'string',
703 minLength: 7
704 },
705 '^name.*': {
706 type: 'string',
707 minLength: 8
708 }
709 }
710 });
711 [{
712 name: 'test'
713 }, {
714 name: 'fdsaffsda',
715 name_long: 'testfdsdfsfd'
716 }, {
717 name: 'fdsafdsafas',
718 foo_long: 'testfdsdfsfd'
719 }, {
720 name: 'dfsafdsa',
721 name_long: 'testfdsdfsfd'
722 }, {
723 name: 'test',
724 name2: 'testffdsafdsads'
725 }].forEach(function(val) {
726 validateInputOutput(schema, result, val)
727 })
728 })
729 })
730 })
731
732 function validateInputOutput(schema, transformedSchema, obj) {
733 const validOriginal = ajv.validate(schema, obj)
734 const validNew = ajv.validate(transformedSchema, obj)
735 expect(validOriginal).to.eql(validNew)
736 }