|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const test = require('tap').test |
| 4 | +const build = require('..') |
| 5 | + |
| 6 | +test('ref internal - properties', (t) => { |
| 7 | + t.plan(2) |
| 8 | + |
| 9 | + const schema = { |
| 10 | + title: 'object with $ref', |
| 11 | + definitions: { |
| 12 | + def: { |
| 13 | + type: 'object', |
| 14 | + properties: { |
| 15 | + str: { |
| 16 | + type: 'string' |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | + }, |
| 21 | + type: 'object', |
| 22 | + properties: { |
| 23 | + obj: { |
| 24 | + $ref: '#/definitions/def' |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + const object = { |
| 30 | + obj: { |
| 31 | + str: 'test' |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + const stringify = build(schema) |
| 36 | + const output = stringify(object) |
| 37 | + |
| 38 | + try { |
| 39 | + JSON.parse(output) |
| 40 | + t.pass() |
| 41 | + } catch (e) { |
| 42 | + t.fail() |
| 43 | + } |
| 44 | + |
| 45 | + t.equal('{"obj":{"str":"test"}}', output) |
| 46 | +}) |
| 47 | + |
| 48 | +test('ref external - properties', (t) => { |
| 49 | + t.plan(2) |
| 50 | + |
| 51 | + const schema = { |
| 52 | + title: 'object with $ref', |
| 53 | + definitions: { |
| 54 | + def: { |
| 55 | + type: 'object', |
| 56 | + properties: { |
| 57 | + str: { |
| 58 | + type: 'string' |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + }, |
| 63 | + type: 'object', |
| 64 | + properties: { |
| 65 | + obj: { |
| 66 | + $ref: __dirname + '/ref.json#/definitions/def' // eslint-disable-line |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + const object = { |
| 72 | + obj: { |
| 73 | + str: 'test' |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + const stringify = build(schema) |
| 78 | + const output = stringify(object) |
| 79 | + |
| 80 | + try { |
| 81 | + JSON.parse(output) |
| 82 | + t.pass() |
| 83 | + } catch (e) { |
| 84 | + t.fail() |
| 85 | + } |
| 86 | + |
| 87 | + t.equal('{"obj":{"str":"test"}}', output) |
| 88 | +}) |
| 89 | + |
| 90 | +test('ref internal - patternProperties', (t) => { |
| 91 | + t.plan(2) |
| 92 | + |
| 93 | + const schema = { |
| 94 | + title: 'object with $ref', |
| 95 | + definitions: { |
| 96 | + def: { |
| 97 | + type: 'object', |
| 98 | + properties: { |
| 99 | + str: { |
| 100 | + type: 'string' |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + }, |
| 105 | + type: 'object', |
| 106 | + properties: {}, |
| 107 | + patternProperties: { |
| 108 | + obj: { |
| 109 | + $ref: '#/definitions/def' |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + const object = { |
| 115 | + obj: { |
| 116 | + str: 'test' |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + const stringify = build(schema) |
| 121 | + const output = stringify(object) |
| 122 | + |
| 123 | + try { |
| 124 | + JSON.parse(output) |
| 125 | + t.pass() |
| 126 | + } catch (e) { |
| 127 | + t.fail() |
| 128 | + } |
| 129 | + |
| 130 | + t.equal('{"obj":{"str":"test"}}', output) |
| 131 | +}) |
| 132 | + |
| 133 | +test('ref internal - additionalProperties', (t) => { |
| 134 | + t.plan(2) |
| 135 | + |
| 136 | + const schema = { |
| 137 | + title: 'object with $ref', |
| 138 | + definitions: { |
| 139 | + def: { |
| 140 | + type: 'object', |
| 141 | + properties: { |
| 142 | + str: { |
| 143 | + type: 'string' |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + }, |
| 148 | + type: 'object', |
| 149 | + properties: {}, |
| 150 | + additionalProperties: { |
| 151 | + $ref: '#/definitions/def' |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + const object = { |
| 156 | + obj: { |
| 157 | + str: 'test' |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + const stringify = build(schema) |
| 162 | + const output = stringify(object) |
| 163 | + |
| 164 | + try { |
| 165 | + JSON.parse(output) |
| 166 | + t.pass() |
| 167 | + } catch (e) { |
| 168 | + t.fail() |
| 169 | + } |
| 170 | + |
| 171 | + t.equal('{"obj":{"str":"test"}}', output) |
| 172 | +}) |
| 173 | + |
| 174 | +test('ref internal - pattern-additional Properties', (t) => { |
| 175 | + t.plan(2) |
| 176 | + |
| 177 | + const schema = { |
| 178 | + title: 'object with $ref', |
| 179 | + definitions: { |
| 180 | + def: { |
| 181 | + type: 'object', |
| 182 | + properties: { |
| 183 | + str: { |
| 184 | + type: 'string' |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + }, |
| 189 | + type: 'object', |
| 190 | + properties: {}, |
| 191 | + patternProperties: { |
| 192 | + reg: { |
| 193 | + $ref: '#/definitions/def' |
| 194 | + } |
| 195 | + }, |
| 196 | + additionalProperties: { |
| 197 | + $ref: '#/definitions/def' |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + const object = { |
| 202 | + reg: { |
| 203 | + str: 'test' |
| 204 | + }, |
| 205 | + obj: { |
| 206 | + str: 'test' |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + const stringify = build(schema) |
| 211 | + const output = stringify(object) |
| 212 | + |
| 213 | + try { |
| 214 | + JSON.parse(output) |
| 215 | + t.pass() |
| 216 | + } catch (e) { |
| 217 | + t.fail() |
| 218 | + } |
| 219 | + |
| 220 | + t.equal('{"reg":{"str":"test"},"obj":{"str":"test"}}', output) |
| 221 | +}) |
0 commit comments