|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const test = require('tap').test |
| 4 | +const build = require('..') |
| 5 | + |
| 6 | +function buildTest (schema, toStringify, expected) { |
| 7 | + test(`render a ${schema.title} with default as JSON`, (t) => { |
| 8 | + t.plan(2) |
| 9 | + |
| 10 | + const stringify = build(schema) |
| 11 | + |
| 12 | + const stringifyUgly = build(schema, {uglify: true}) |
| 13 | + const output = stringify(toStringify) |
| 14 | + const outputUglify = stringifyUgly(toStringify) |
| 15 | + |
| 16 | + t.strictEqual(output, JSON.stringify(expected)) |
| 17 | + t.strictEqual(outputUglify, JSON.stringify(expected)) |
| 18 | + }) |
| 19 | +} |
| 20 | + |
| 21 | +buildTest({ |
| 22 | + 'title': 'default string', |
| 23 | + 'type': 'object', |
| 24 | + 'properties': { |
| 25 | + 'firstName': { |
| 26 | + 'type': 'string' |
| 27 | + }, |
| 28 | + 'lastName': { |
| 29 | + 'type': 'string', |
| 30 | + default: 'Collina' |
| 31 | + }, |
| 32 | + 'age': { |
| 33 | + 'description': 'Age in years', |
| 34 | + 'type': 'integer', |
| 35 | + 'minimum': 0 |
| 36 | + }, |
| 37 | + 'magic': { |
| 38 | + 'type': 'number' |
| 39 | + } |
| 40 | + }, |
| 41 | + 'required': ['firstName', 'lastName'] |
| 42 | +}, { |
| 43 | + firstName: 'Matteo', |
| 44 | + magic: 42, |
| 45 | + age: 32 |
| 46 | +}, { |
| 47 | + firstName: 'Matteo', |
| 48 | + lastName: 'Collina', |
| 49 | + age: 32, |
| 50 | + magic: 42 |
| 51 | +}) |
| 52 | + |
| 53 | +buildTest({ |
| 54 | + 'title': 'default string with value', |
| 55 | + 'type': 'object', |
| 56 | + 'properties': { |
| 57 | + 'firstName': { |
| 58 | + 'type': 'string' |
| 59 | + }, |
| 60 | + 'lastName': { |
| 61 | + 'type': 'string', |
| 62 | + default: 'Collina' |
| 63 | + }, |
| 64 | + 'age': { |
| 65 | + 'description': 'Age in years', |
| 66 | + 'type': 'integer', |
| 67 | + 'minimum': 0 |
| 68 | + }, |
| 69 | + 'magic': { |
| 70 | + 'type': 'number' |
| 71 | + } |
| 72 | + }, |
| 73 | + 'required': ['firstName', 'lastName'] |
| 74 | +}, { |
| 75 | + firstName: 'Matteo', |
| 76 | + lastName: 'collina', |
| 77 | + magic: 42, |
| 78 | + age: 32 |
| 79 | +}, { |
| 80 | + firstName: 'Matteo', |
| 81 | + lastName: 'collina', |
| 82 | + age: 32, |
| 83 | + magic: 42 |
| 84 | +}) |
| 85 | + |
| 86 | +buildTest({ |
| 87 | + 'title': 'default number', |
| 88 | + 'type': 'object', |
| 89 | + 'properties': { |
| 90 | + 'firstName': { |
| 91 | + 'type': 'string' |
| 92 | + }, |
| 93 | + 'lastName': { |
| 94 | + 'type': 'string' |
| 95 | + }, |
| 96 | + 'age': { |
| 97 | + 'description': 'Age in years', |
| 98 | + 'type': 'integer', |
| 99 | + 'minimum': 0 |
| 100 | + }, |
| 101 | + 'magic': { |
| 102 | + 'type': 'number', |
| 103 | + default: 42 |
| 104 | + } |
| 105 | + }, |
| 106 | + 'required': ['firstName', 'lastName'] |
| 107 | +}, { |
| 108 | + firstName: 'Matteo', |
| 109 | + lastName: 'Collina', |
| 110 | + age: 32 |
| 111 | +}, { |
| 112 | + firstName: 'Matteo', |
| 113 | + lastName: 'Collina', |
| 114 | + age: 32, |
| 115 | + magic: 42 |
| 116 | +}) |
| 117 | + |
| 118 | +buildTest({ |
| 119 | + 'title': 'default number with value', |
| 120 | + 'type': 'object', |
| 121 | + 'properties': { |
| 122 | + 'firstName': { |
| 123 | + 'type': 'string' |
| 124 | + }, |
| 125 | + 'lastName': { |
| 126 | + 'type': 'string' |
| 127 | + }, |
| 128 | + 'age': { |
| 129 | + 'description': 'Age in years', |
| 130 | + 'type': 'integer', |
| 131 | + 'minimum': 0 |
| 132 | + }, |
| 133 | + 'magic': { |
| 134 | + 'type': 'number', |
| 135 | + default: 42 |
| 136 | + } |
| 137 | + }, |
| 138 | + 'required': ['firstName', 'lastName'] |
| 139 | +}, { |
| 140 | + firstName: 'Matteo', |
| 141 | + lastName: 'Collina', |
| 142 | + age: 32, |
| 143 | + magic: 66 |
| 144 | +}, { |
| 145 | + firstName: 'Matteo', |
| 146 | + lastName: 'Collina', |
| 147 | + age: 32, |
| 148 | + magic: 66 |
| 149 | +}) |
| 150 | + |
| 151 | +buildTest({ |
| 152 | + 'title': 'default object', |
| 153 | + 'type': 'object', |
| 154 | + 'properties': { |
| 155 | + 'firstName': { |
| 156 | + 'type': 'string' |
| 157 | + }, |
| 158 | + 'lastName': { |
| 159 | + 'type': 'string' |
| 160 | + }, |
| 161 | + 'age': { |
| 162 | + 'description': 'Age in years', |
| 163 | + 'type': 'integer', |
| 164 | + 'minimum': 0 |
| 165 | + }, |
| 166 | + 'otherProps': { |
| 167 | + 'type': 'object', |
| 168 | + default: { foo: 'bar' } |
| 169 | + } |
| 170 | + }, |
| 171 | + 'required': ['firstName', 'lastName'] |
| 172 | +}, { |
| 173 | + firstName: 'Matteo', |
| 174 | + lastName: 'Collina', |
| 175 | + age: 32 |
| 176 | +}, { |
| 177 | + firstName: 'Matteo', |
| 178 | + lastName: 'Collina', |
| 179 | + age: 32, |
| 180 | + otherProps: { foo: 'bar' } |
| 181 | +}) |
| 182 | + |
| 183 | +buildTest({ |
| 184 | + 'title': 'default object with value', |
| 185 | + 'type': 'object', |
| 186 | + 'properties': { |
| 187 | + 'firstName': { |
| 188 | + 'type': 'string' |
| 189 | + }, |
| 190 | + 'lastName': { |
| 191 | + 'type': 'string' |
| 192 | + }, |
| 193 | + 'age': { |
| 194 | + 'description': 'Age in years', |
| 195 | + 'type': 'integer', |
| 196 | + 'minimum': 0 |
| 197 | + }, |
| 198 | + 'otherProps': { |
| 199 | + 'type': 'object', |
| 200 | + additionalProperties: true, |
| 201 | + default: { foo: 'bar' } |
| 202 | + } |
| 203 | + }, |
| 204 | + 'required': ['firstName', 'lastName'] |
| 205 | +}, { |
| 206 | + firstName: 'Matteo', |
| 207 | + lastName: 'Collina', |
| 208 | + age: 32, |
| 209 | + otherProps: { hello: 'world' } |
| 210 | +}, { |
| 211 | + firstName: 'Matteo', |
| 212 | + lastName: 'Collina', |
| 213 | + age: 32, |
| 214 | + otherProps: { hello: 'world' } |
| 215 | +}) |
| 216 | + |
| 217 | +buildTest({ |
| 218 | + 'title': 'default array', |
| 219 | + 'type': 'object', |
| 220 | + 'properties': { |
| 221 | + 'firstName': { |
| 222 | + 'type': 'string' |
| 223 | + }, |
| 224 | + 'lastName': { |
| 225 | + 'type': 'string' |
| 226 | + }, |
| 227 | + 'age': { |
| 228 | + 'description': 'Age in years', |
| 229 | + 'type': 'integer', |
| 230 | + 'minimum': 0 |
| 231 | + }, |
| 232 | + 'otherProps': { |
| 233 | + 'type': 'array', |
| 234 | + items: { type: 'string' }, |
| 235 | + default: ['FOO'] |
| 236 | + } |
| 237 | + }, |
| 238 | + 'required': ['firstName', 'lastName'] |
| 239 | +}, { |
| 240 | + firstName: 'Matteo', |
| 241 | + lastName: 'Collina', |
| 242 | + age: 32 |
| 243 | +}, { |
| 244 | + firstName: 'Matteo', |
| 245 | + lastName: 'Collina', |
| 246 | + age: 32, |
| 247 | + otherProps: ['FOO'] |
| 248 | +}) |
| 249 | + |
| 250 | +buildTest({ |
| 251 | + 'title': 'default array with value', |
| 252 | + 'type': 'object', |
| 253 | + 'properties': { |
| 254 | + 'firstName': { |
| 255 | + 'type': 'string' |
| 256 | + }, |
| 257 | + 'lastName': { |
| 258 | + 'type': 'string' |
| 259 | + }, |
| 260 | + 'age': { |
| 261 | + 'description': 'Age in years', |
| 262 | + 'type': 'integer', |
| 263 | + 'minimum': 0 |
| 264 | + }, |
| 265 | + 'otherProps': { |
| 266 | + 'type': 'array', |
| 267 | + items: { type: 'string' }, |
| 268 | + default: ['FOO'] |
| 269 | + } |
| 270 | + }, |
| 271 | + 'required': ['firstName', 'lastName'] |
| 272 | +}, { |
| 273 | + firstName: 'Matteo', |
| 274 | + lastName: 'Collina', |
| 275 | + age: 32, |
| 276 | + otherProps: ['BAR'] |
| 277 | +}, { |
| 278 | + firstName: 'Matteo', |
| 279 | + lastName: 'Collina', |
| 280 | + age: 32, |
| 281 | + otherProps: ['BAR'] |
| 282 | +}) |
| 283 | + |
| 284 | +buildTest({ |
| 285 | + 'title': 'default deeper value', |
| 286 | + 'type': 'object', |
| 287 | + 'properties': { |
| 288 | + 'level1': { |
| 289 | + 'type': 'object', |
| 290 | + 'properties': { |
| 291 | + 'level2': { |
| 292 | + 'type': 'object', |
| 293 | + 'properties': { |
| 294 | + 'level3': { |
| 295 | + 'type': 'object', |
| 296 | + 'properties': { |
| 297 | + 'level4': { |
| 298 | + 'type': 'object', |
| 299 | + 'default': { 'foo': 'bar' } |
| 300 | + } |
| 301 | + } |
| 302 | + } |
| 303 | + } |
| 304 | + } |
| 305 | + } |
| 306 | + } |
| 307 | + } |
| 308 | +}, { |
| 309 | + level1: { level2: { level3: { } } } |
| 310 | +}, { |
| 311 | + level1: { level2: { level3: { level4: { foo: 'bar' } } } } |
| 312 | +}) |
| 313 | + |
| 314 | +buildTest({ |
| 315 | + 'title': 'default deeper value with value', |
| 316 | + 'type': 'object', |
| 317 | + 'properties': { |
| 318 | + 'level1': { |
| 319 | + 'type': 'object', |
| 320 | + 'properties': { |
| 321 | + 'level2': { |
| 322 | + 'type': 'object', |
| 323 | + 'properties': { |
| 324 | + 'level3': { |
| 325 | + 'type': 'object', |
| 326 | + 'properties': { |
| 327 | + 'level4': { |
| 328 | + 'type': 'object', |
| 329 | + 'default': { 'foo': 'bar' } |
| 330 | + } |
| 331 | + } |
| 332 | + } |
| 333 | + } |
| 334 | + } |
| 335 | + } |
| 336 | + } |
| 337 | + } |
| 338 | +}, { |
| 339 | + level1: { level2: { level3: { level4: { } } } } |
| 340 | +}, { |
| 341 | + level1: { level2: { level3: { level4: { } } } } |
| 342 | +}) |
0 commit comments