Spaces:
Runtime error
Runtime error
File size: 4,196 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 |
'use strict'
const { test } = require('node:test')
const clone = require('rfdc/default')
const build = require('..')
test('oneOf with $ref should not change the input schema', t => {
t.plan(2)
const referenceSchema = {
$id: 'externalId',
type: 'object',
properties: {
name: { type: 'string' }
}
}
const schema = {
$id: 'mainSchema',
type: 'object',
properties: {
people: {
oneOf: [{ $ref: 'externalId' }]
}
}
}
const clonedSchema = clone(schema)
const stringify = build(schema, {
schema: {
[referenceSchema.$id]: referenceSchema
}
})
const value = stringify({ people: { name: 'hello', foo: 'bar' } })
t.assert.equal(value, '{"people":{"name":"hello"}}')
t.assert.deepStrictEqual(schema, clonedSchema)
})
test('oneOf and anyOf with $ref should not change the input schema', t => {
t.plan(3)
const referenceSchema = {
$id: 'externalSchema',
type: 'object',
properties: {
name: { type: 'string' }
}
}
const schema = {
$id: 'rootSchema',
type: 'object',
properties: {
people: {
oneOf: [{ $ref: 'externalSchema' }]
},
love: {
anyOf: [
{ $ref: '#/definitions/foo' },
{ type: 'boolean' }
]
}
},
definitions: {
foo: { type: 'string' }
}
}
const clonedSchema = clone(schema)
const stringify = build(schema, {
schema: {
[referenceSchema.$id]: referenceSchema
}
})
const valueAny1 = stringify({ people: { name: 'hello', foo: 'bar' }, love: 'music' })
const valueAny2 = stringify({ people: { name: 'hello', foo: 'bar' }, love: true })
t.assert.equal(valueAny1, '{"people":{"name":"hello"},"love":"music"}')
t.assert.equal(valueAny2, '{"people":{"name":"hello"},"love":true}')
t.assert.deepStrictEqual(schema, clonedSchema)
})
test('multiple $ref tree', t => {
t.plan(2)
const referenceDeepSchema = {
$id: 'deepId',
type: 'number'
}
const referenceSchema = {
$id: 'externalId',
type: 'object',
properties: {
name: { $ref: '#/definitions/foo' },
age: { $ref: 'deepId' }
},
definitions: {
foo: { type: 'string' }
}
}
const schema = {
$id: 'mainSchema',
type: 'object',
properties: {
people: {
oneOf: [{ $ref: 'externalId' }]
}
}
}
const clonedSchema = clone(schema)
const stringify = build(schema, {
schema: {
[referenceDeepSchema.$id]: referenceDeepSchema,
[referenceSchema.$id]: referenceSchema
}
})
const value = stringify({ people: { name: 'hello', foo: 'bar', age: 42 } })
t.assert.equal(value, '{"people":{"name":"hello","age":42}}')
t.assert.deepStrictEqual(schema, clonedSchema)
})
test('must not mutate items $ref', t => {
t.plan(2)
const referenceSchema = {
$id: 'ShowSchema',
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
properties: {
name: {
type: 'string'
}
}
}
const schema = {
$id: 'ListSchema',
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'array',
items: {
$ref: 'ShowSchema#'
}
}
const clonedSchema = clone(schema)
const stringify = build(schema, {
schema: {
[referenceSchema.$id]: referenceSchema
}
})
const value = stringify([{ name: 'foo' }])
t.assert.equal(value, '[{"name":"foo"}]')
t.assert.deepStrictEqual(schema, clonedSchema)
})
test('must not mutate items referred by $ref', t => {
t.plan(2)
const firstSchema = {
$id: 'example1',
type: 'object',
properties: {
name: {
type: 'string'
}
}
}
const reusedSchema = {
$id: 'example2',
type: 'object',
properties: {
name: {
oneOf: [
{
$ref: 'example1'
}
]
}
}
}
const clonedSchema = clone(firstSchema)
const stringify = build(reusedSchema, {
schema: {
[firstSchema.$id]: firstSchema
}
})
const value = stringify({ name: { name: 'foo' } })
t.assert.equal(value, '{"name":{"name":"foo"}}')
t.assert.deepStrictEqual(firstSchema, clonedSchema)
})
|