You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.3 KiB
80 lines
1.3 KiB
4 years ago
|
'use strict'
|
||
|
|
||
|
const test = require('tap').test
|
||
|
const build = require('..')
|
||
|
|
||
|
test('object with nested random property', (t) => {
|
||
|
t.plan(4)
|
||
|
|
||
|
const schema = {
|
||
|
title: 'empty schema to allow any object',
|
||
|
type: 'object',
|
||
|
properties: {
|
||
|
id: {
|
||
|
type: 'number'
|
||
|
},
|
||
|
name: {}
|
||
|
}
|
||
|
}
|
||
|
const stringify = build(schema)
|
||
|
|
||
|
try {
|
||
|
const value = stringify({
|
||
|
id: 1,
|
||
|
name: 'string'
|
||
|
})
|
||
|
t.is(value, '{"id":1,"name":"string"}')
|
||
|
} catch (e) {
|
||
|
t.fail()
|
||
|
}
|
||
|
try {
|
||
|
const value = stringify({
|
||
|
id: 1,
|
||
|
name: {
|
||
|
first: 'name',
|
||
|
last: 'last'
|
||
|
}
|
||
|
})
|
||
|
t.is(value, '{"id":1,"name":{"first":"name","last":"last"}}')
|
||
|
} catch (e) {
|
||
|
t.fail()
|
||
|
}
|
||
|
try {
|
||
|
const value = stringify({
|
||
|
id: 1,
|
||
|
name: null
|
||
|
})
|
||
|
t.is(value, '{"id":1,"name":null}')
|
||
|
} catch (e) {
|
||
|
t.fail()
|
||
|
}
|
||
|
try {
|
||
|
const value = stringify({
|
||
|
id: 1,
|
||
|
name: ['first', 'last']
|
||
|
})
|
||
|
t.is(value, '{"id":1,"name":["first","last"]}')
|
||
|
} catch (e) {
|
||
|
t.fail()
|
||
|
}
|
||
|
})
|
||
|
|
||
|
test('array with random items', (t) => {
|
||
|
t.plan(1)
|
||
|
|
||
|
const schema = {
|
||
|
title: 'empty schema to allow any object',
|
||
|
type: 'array',
|
||
|
items: {
|
||
|
}
|
||
|
}
|
||
|
const stringify = build(schema)
|
||
|
|
||
|
try {
|
||
|
const value = stringify([1, 'string', null])
|
||
|
t.is(value, '[1,"string",null]')
|
||
|
} catch (e) {
|
||
|
t.fail()
|
||
|
}
|
||
|
})
|