Spaces:
Runtime error
Runtime error
File size: 5,403 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 |
'use strict'
const assert = require('node:assert/strict')
const { test } = require('node:test')
const { RefResolver } = require('../index.js')
test('should get schema by schema id', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const schema = {
$id: schemaId,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
refResolver.addSchema(schema)
const resolvedSchema = refResolver.getSchema(schemaId)
assert.equal(resolvedSchema, schema)
})
test('should return null if schema was not found', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const schema = {
$id: schemaId,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
refResolver.addSchema(schema)
const resolvedSchema = refResolver.getSchema(schemaId, '#/definitions/missingSchema')
assert.equal(resolvedSchema, null)
})
test('should get a sub schema by sub schema id', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const subSchemaId = 'subSchemaId'
const schema = {
$id: schemaId,
definitions: {
subSchema: {
$id: subSchemaId,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
}
}
refResolver.addSchema(schema)
const resolvedSchema = refResolver.getSchema(subSchemaId)
assert.equal(resolvedSchema, schema.definitions.subSchema)
})
test('should get a sub schema by schema json pointer', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const subSchemaId = 'subSchemaId'
const schema = {
$id: schemaId,
definitions: {
subSchema: {
$id: subSchemaId,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
}
}
refResolver.addSchema(schema)
const jsonPointer = '#/definitions/subSchema'
const resolvedSchema = refResolver.getSchema(schemaId, jsonPointer)
assert.equal(resolvedSchema, schema.definitions.subSchema)
})
test('should get a sub schema by sub schema json pointer', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const subSchemaId = 'subSchemaId'
const schema = {
$id: schemaId,
definitions: {
subSchema: {
$id: subSchemaId,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
}
}
refResolver.addSchema(schema)
const jsonPointer = '#/properties/foo'
const resolvedSchema = refResolver.getSchema(subSchemaId, jsonPointer)
assert.equal(resolvedSchema, schema.definitions.subSchema.properties.foo)
})
test('should handle null schema correctly', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const schema = {
$id: schemaId,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
refResolver.addSchema(schema)
const resolvedSchema = refResolver.getSchema(schemaId)
assert.equal(resolvedSchema, schema)
})
test('should add a schema without root $id', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const schema = {
type: 'object',
properties: {
foo: { type: 'string' }
}
}
refResolver.addSchema(schema, schemaId)
const resolvedSchema = refResolver.getSchema(schemaId)
assert.equal(resolvedSchema, schema)
})
test('root $id has higher priority than a schemaId argument', () => {
const refResolver = new RefResolver()
const schemaIdProperty = 'schemaId1'
const schemaIdArgument = 'schemaId2'
const schema = {
$id: schemaIdProperty,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
refResolver.addSchema(schema, schemaIdArgument)
const resolvedSchema = refResolver.getSchema(schemaIdProperty)
assert.equal(resolvedSchema, schema)
try {
refResolver.getSchema(schemaIdArgument)
assert.fail('should have thrown an error')
} catch (err) {
assert.equal(
err.message,
`Cannot resolve ref "${schemaIdArgument}#". Schema with id "${schemaIdArgument}" is not found.`
)
}
})
test('should not use a root $id if it is an anchor', () => {
const refResolver = new RefResolver()
const schemaIdProperty = '#schemaId1'
const schemaIdArgument = 'schemaId2'
const schema = {
$id: schemaIdProperty,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
refResolver.addSchema(schema, schemaIdArgument)
try {
refResolver.getSchema(schemaIdProperty)
assert.fail('should have thrown an error')
} catch (err) {
assert.equal(
err.message,
`Cannot resolve ref "${schemaIdProperty}#". Schema with id "${schemaIdProperty}" is not found.`
)
}
const resolvedSchema2 = refResolver.getSchema(schemaIdArgument)
assert.equal(resolvedSchema2, schema)
const resolvedSchema3 = refResolver.getSchema(schemaIdArgument, schemaIdProperty)
assert.equal(resolvedSchema3, schema)
})
test('should return null if sub schema by json pointer is not found', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const schema = {
$id: 'schemaId',
type: 'object',
properties: {
foo: { type: 'string' }
}
}
refResolver.addSchema(schema)
const schemaRefs = refResolver.getSchema(schemaId, '#/missingSchema')
assert.equal(schemaRefs, null)
})
|