Debian Codegraph @

test/specs/custom-resolvers.spec.js

raw symbols SCIP
1 const chai = require('chai')
2 const merger = require('../../src')
3 const expect = chai.expect
4
5 describe('simple resolver', () => {
6 it('merges as expected (with enum)', () => {
7 const result = merger({
8 enum: [1, 2],
9 allOf: [{
10 enum: [2, 3]
11 }]
12 })
13
14 expect(result).to.eql({
15 enum: [2]
16 })
17
18 const opts = {
19 resolvers: {
20 enum(schemas, paths, mergeSchemas, options) {
21 expect(options).to.eql(opts)
22 expect(schemas).to.have.length(2)
23 expect(paths).to.have.length(1)
24 expect(paths).to.eql(['enum'])
25
26 // inner merge test
27 const innerSchemas = [{
28 minLength: 1
29 }, {
30 minLength: 7
31 }]
32
33 const innerResult = mergeSchemas(innerSchemas)
34 expect(innerResult).to.eql({
35 minLength: 7
36 })
37
38 return [5]
39 }
40 }
41 }
42
43 const resultCustom = merger({
44 enum: [1, 2],
45 allOf: [{
46 enum: [2, 3]
47 }]
48 }, opts)
49
50 expect(resultCustom).to.eql({
51 enum: [5]
52 })
53 })
54
55 describe('group resolvers', () => {
56 it('works as intended with if then else copy resolver', () => {
57 const conditonalRelated = ['if', 'then', 'else']
58 const has = (obj, propName) => Object.prototype.hasOwnProperty.call(obj, propName)
59 const opts = {
60 complexResolvers: {
61 if: {
62 // test with same if-then-else resolver
63 keywords: conditonalRelated,
64 resolver(schemas, paths, mergers, options) {
65 const allWithConditional = schemas.filter(schema =>
66 conditonalRelated.some(keyword => has(schema, keyword)))
67
68 // merge sub schemas completely
69 // if,then,else must not be merged to the base schema, but if they contain allOf themselves, that should be merged
70 function merge(schema) {
71 const obj = {}
72 if (has(schema, 'if')) obj.if = mergers.if([schema.if])
73 if (has(schema, 'then')) obj.then = mergers.then([schema.then])
74 if (has(schema, 'else')) obj.else = mergers.else([schema.else])
75 return obj
76 }
77
78 // first schema with any of the 3 keywords is used as base
79 const first = merge(allWithConditional.shift())
80 return allWithConditional.reduce((all, schema) => {
81 all.allOf = (all.allOf || []).concat(merge(schema))
82 return all
83 }, first)
84 }
85 }
86 }
87 }
88
89 const resultCustom = merger({
90 allOf: [{
91 if: {
92 required: ['def']
93 },
94 then: {},
95 else: {}
96 }]
97 }, opts)
98
99 expect(resultCustom).to.eql({
100 if: {
101 required: ['def']
102 },
103 then: {},
104 else: {}
105 })
106 })
107 })
108 })