Spaces:
Runtime error
Runtime error
File size: 7,011 Bytes
23ac194 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
'use strict'
const { test } = require('node:test')
const validator = require('is-my-json-valid')
const build = require('..')
function buildTest (schema, toStringify) {
test(`render a ${schema.title} as JSON`, (t) => {
t.plan(3)
const validate = validator(schema)
const stringify = build(schema)
const output = stringify(toStringify)
t.assert.deepStrictEqual(JSON.parse(output), toStringify)
t.assert.equal(output, JSON.stringify(toStringify))
t.assert.ok(validate(JSON.parse(output)), 'valid schema')
})
}
buildTest({
title: 'string',
type: 'string',
format: 'unsafe'
}, 'hello world')
buildTest({
title: 'basic',
type: 'object',
properties: {
firstName: {
type: 'string'
},
lastName: {
type: 'string'
},
age: {
description: 'Age in years',
type: 'integer',
minimum: 0
},
magic: {
type: 'number'
}
},
required: ['firstName', 'lastName']
}, {
firstName: 'Matteo',
lastName: 'Collina',
age: 32,
magic: 42.42
})
buildTest({
title: 'string',
type: 'string'
}, 'hello world')
buildTest({
title: 'string',
type: 'string'
}, 'hello\nworld')
buildTest({
title: 'string with quotes',
type: 'string'
}, 'hello """" world')
buildTest({
title: 'boolean true',
type: 'boolean'
}, true)
buildTest({
title: 'boolean false',
type: 'boolean'
}, false)
buildTest({
title: 'an integer',
type: 'integer'
}, 42)
buildTest({
title: 'a number',
type: 'number'
}, 42.42)
buildTest({
title: 'deep',
type: 'object',
properties: {
firstName: {
type: 'string'
},
lastName: {
type: 'string'
},
more: {
description: 'more properties',
type: 'object',
properties: {
something: {
type: 'string'
}
}
}
}
}, {
firstName: 'Matteo',
lastName: 'Collina',
more: {
something: 'else'
}
})
buildTest({
title: 'null',
type: 'null'
}, null)
buildTest({
title: 'deep object with weird keys',
type: 'object',
properties: {
'@version': {
type: 'integer'
}
}
}, {
'@version': 1
})
buildTest({
title: 'deep object with weird keys of type object',
type: 'object',
properties: {
'@data': {
type: 'object',
properties: {
id: {
type: 'string'
}
}
}
}
}, {
'@data': {
id: 'string'
}
})
buildTest({
title: 'deep object with spaces in key',
type: 'object',
properties: {
'spaces in key': {
type: 'object',
properties: {
something: {
type: 'integer'
}
}
}
}
}, {
'spaces in key': {
something: 1
}
})
buildTest({
title: 'with null',
type: 'object',
properties: {
firstName: {
type: 'null'
}
}
}, {
firstName: null
})
buildTest({
title: 'array with objects',
type: 'array',
items: {
type: 'object',
properties: {
name: {
type: 'string'
}
}
}
}, [{
name: 'Matteo'
}, {
name: 'Dave'
}])
buildTest({
title: 'array with strings',
type: 'array',
items: {
type: 'string'
}
}, [
'Matteo',
'Dave'
])
buildTest({
title: 'array with numbers',
type: 'array',
items: {
type: 'number'
}
}, [
42.42,
24
])
buildTest({
title: 'array with integers',
type: 'array',
items: {
type: 'number'
}
}, [
42,
24
])
buildTest({
title: 'nested array with objects',
type: 'object',
properties: {
data: {
type: 'array',
items: {
type: 'object',
properties: {
name: {
type: 'string'
}
}
}
}
}
}, {
data: [{
name: 'Matteo'
}, {
name: 'Dave'
}]
})
buildTest({
title: 'object with boolean',
type: 'object',
properties: {
readonly: {
type: 'boolean'
}
}
}, {
readonly: true
})
test('throw an error or coerce numbers and integers that are not numbers', (t) => {
const stringify = build({
title: 'basic',
type: 'object',
properties: {
age: {
type: 'number'
},
distance: {
type: 'integer'
}
}
})
t.assert.throws(() => {
stringify({ age: 'hello ', distance: 'long' })
}, { message: 'The value "hello " cannot be converted to a number.' })
const result = stringify({
age: '42',
distance: true
})
t.assert.deepStrictEqual(JSON.parse(result), { age: 42, distance: 1 })
})
test('Should throw on invalid schema', t => {
t.plan(1)
t.assert.throws(() => {
build({
type: 'Dinosaur',
properties: {
claws: { type: 'sharp' }
}
})
}, { message: 'schema is invalid: data/properties/claws/type must be equal to one of the allowed values' })
})
test('additionalProperties - throw on unknown type', (t) => {
t.plan(1)
t.assert.throws(() => {
build({
title: 'check array coerce',
type: 'object',
properties: {},
additionalProperties: {
type: 'strangetype'
}
})
t.fail('should be an invalid schema')
}, { message: 'schema is invalid: data/additionalProperties/type must be equal to one of the allowed values' })
})
test('patternProperties - throw on unknown type', (t) => {
t.plan(1)
t.assert.throws(() => {
build({
title: 'check array coerce',
type: 'object',
properties: {},
patternProperties: {
foo: {
type: 'strangetype'
}
}
})
}, { message: 'schema is invalid: data/patternProperties/foo/type must be equal to one of the allowed values' })
})
test('render a double quote as JSON /1', (t) => {
t.plan(2)
const schema = {
type: 'string'
}
const toStringify = '" double quote'
const validate = validator(schema)
const stringify = build(schema)
const output = stringify(toStringify)
t.assert.equal(output, JSON.stringify(toStringify))
t.assert.ok(validate(JSON.parse(output)), 'valid schema')
})
test('render a double quote as JSON /2', (t) => {
t.plan(2)
const schema = {
type: 'string'
}
const toStringify = 'double quote " 2'
const validate = validator(schema)
const stringify = build(schema)
const output = stringify(toStringify)
t.assert.equal(output, JSON.stringify(toStringify))
t.assert.ok(validate(JSON.parse(output)), 'valid schema')
})
test('render a long string', (t) => {
t.plan(2)
const schema = {
type: 'string'
}
const toStringify = 'the Ultimate Question of Life, the Universe, and Everything.'
const validate = validator(schema)
const stringify = build(schema)
const output = stringify(toStringify)
t.assert.equal(output, JSON.stringify(toStringify))
t.assert.ok(validate(JSON.parse(output)), 'valid schema')
})
test('returns JSON.stringify if schema type is boolean', t => {
t.plan(1)
const schema = {
type: 'array',
items: true
}
const array = [1, true, 'test']
const stringify = build(schema)
t.assert.equal(stringify(array), JSON.stringify(array))
})
|