|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const test = require('tap').test |
| 4 | +const build = require('..') |
| 5 | + |
| 6 | +test('simple object with multi-type property', (t) => { |
| 7 | + t.plan(2) |
| 8 | + |
| 9 | + const schema = { |
| 10 | + title: 'simple object with multi-type property', |
| 11 | + type: 'object', |
| 12 | + properties: { |
| 13 | + stringOrNumber: { |
| 14 | + type: ['string', 'number'] |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | + const stringify = build(schema) |
| 19 | + |
| 20 | + try { |
| 21 | + const value = stringify({ |
| 22 | + stringOrNumber: 'string' |
| 23 | + }) |
| 24 | + t.is(value, '{"stringOrNumber":"string"}') |
| 25 | + } catch (e) { |
| 26 | + t.fail() |
| 27 | + } |
| 28 | + |
| 29 | + try { |
| 30 | + const value = stringify({ |
| 31 | + stringOrNumber: 42 |
| 32 | + }) |
| 33 | + t.is(value, '{"stringOrNumber":42}') |
| 34 | + } catch (e) { |
| 35 | + t.fail() |
| 36 | + } |
| 37 | +}) |
| 38 | + |
| 39 | +test('object with array of multiple types', (t) => { |
| 40 | + t.plan(3) |
| 41 | + |
| 42 | + const schema = { |
| 43 | + title: 'object with array of multiple types', |
| 44 | + type: 'object', |
| 45 | + properties: { |
| 46 | + arrayOfStringsAndNumbers: { |
| 47 | + type: 'array', |
| 48 | + items: { |
| 49 | + type: ['string', 'number'] |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + const stringify = build(schema) |
| 55 | + |
| 56 | + try { |
| 57 | + const value = stringify({ |
| 58 | + arrayOfStringsAndNumbers: ['string1', 'string2'] |
| 59 | + }) |
| 60 | + t.is(value, '{"arrayOfStringsAndNumbers":["string1","string2"]}') |
| 61 | + } catch (e) { |
| 62 | + console.log(e) |
| 63 | + t.fail() |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + const value = stringify({ |
| 68 | + arrayOfStringsAndNumbers: [42, 7] |
| 69 | + }) |
| 70 | + t.is(value, '{"arrayOfStringsAndNumbers":[42,7]}') |
| 71 | + } catch (e) { |
| 72 | + t.fail() |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + const value = stringify({ |
| 77 | + arrayOfStringsAndNumbers: ['string1', 42, 7, 'string2'] |
| 78 | + }) |
| 79 | + t.is(value, '{"arrayOfStringsAndNumbers":["string1",42,7,"string2"]}') |
| 80 | + } catch (e) { |
| 81 | + t.fail() |
| 82 | + } |
| 83 | +}) |
| 84 | + |
| 85 | +// TODO: Multiple types inside tuples are currently unsupported |
| 86 | +// test('object with tuple of multiple types', (t) => { |
| 87 | +// t.plan(2) |
| 88 | + |
| 89 | +// const schema = { |
| 90 | +// title: 'object with array of multiple types', |
| 91 | +// type: 'object', |
| 92 | +// properties: { |
| 93 | +// fixedTupleOfStringsAndNumbers: { |
| 94 | +// type: 'array', |
| 95 | +// items: [ |
| 96 | +// { |
| 97 | +// type: 'string' |
| 98 | +// }, |
| 99 | +// { |
| 100 | +// type: 'number' |
| 101 | +// }, |
| 102 | +// { |
| 103 | +// type: ['string', 'number'] |
| 104 | +// } |
| 105 | +// ] |
| 106 | +// } |
| 107 | +// } |
| 108 | +// } |
| 109 | +// const stringify = build(schema) |
| 110 | + |
| 111 | +// try { |
| 112 | +// const value = stringify({ |
| 113 | +// fixedTupleOfStringsAndNumbers: ['string1', 42, 7] |
| 114 | +// }) |
| 115 | +// t.is(value, '{"fixedTupleOfStringsAndNumbers":["string1",42,7]}') |
| 116 | +// } catch (e) { |
| 117 | +// console.log(e) |
| 118 | +// t.fail() |
| 119 | +// } |
| 120 | + |
| 121 | +// try { |
| 122 | +// const value = stringify({ |
| 123 | +// fixedTupleOfStringsAndNumbers: ['string1', 42, 7] |
| 124 | +// }) |
| 125 | +// t.is(value, '{"fixedTupleOfStringsAndNumbers":["string1",42,"string2"]}') |
| 126 | +// } catch (e) { |
| 127 | +// console.log(e) |
| 128 | +// t.fail() |
| 129 | +// } |
| 130 | +// }) |
| 131 | + |
| 132 | +test('object with anyOf and multiple types', (t) => { |
| 133 | + t.plan(3) |
| 134 | + |
| 135 | + const schema = { |
| 136 | + title: 'object with anyOf and multiple types', |
| 137 | + type: 'object', |
| 138 | + properties: { |
| 139 | + objectOrBoolean: { |
| 140 | + anyOf: [ |
| 141 | + { |
| 142 | + type: 'object', |
| 143 | + properties: { |
| 144 | + stringOrNumber: { |
| 145 | + type: ['string', 'number'] |
| 146 | + } |
| 147 | + } |
| 148 | + }, |
| 149 | + { |
| 150 | + type: 'boolean' |
| 151 | + } |
| 152 | + ] |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + const stringify = build(schema) |
| 157 | + |
| 158 | + try { |
| 159 | + const value = stringify({ |
| 160 | + objectOrBoolean: { stringOrNumber: 'string' } |
| 161 | + }) |
| 162 | + t.is(value, '{"objectOrBoolean":{"stringOrNumber":"string"}}') |
| 163 | + } catch (e) { |
| 164 | + console.log(e) |
| 165 | + t.fail() |
| 166 | + } |
| 167 | + |
| 168 | + try { |
| 169 | + const value = stringify({ |
| 170 | + objectOrBoolean: { stringOrNumber: 42 } |
| 171 | + }) |
| 172 | + t.is(value, '{"objectOrBoolean":{"stringOrNumber":42}}') |
| 173 | + } catch (e) { |
| 174 | + t.fail() |
| 175 | + } |
| 176 | + |
| 177 | + try { |
| 178 | + const value = stringify({ |
| 179 | + objectOrBoolean: true |
| 180 | + }) |
| 181 | + t.is(value, '{"objectOrBoolean":true}') |
| 182 | + } catch (e) { |
| 183 | + t.fail() |
| 184 | + } |
| 185 | +}) |
0 commit comments