Projektstart

This commit is contained in:
2026-01-22 15:49:12 +01:00
parent 7212eb6f7a
commit 57e5f652f8
10637 changed files with 2598792 additions and 64 deletions

View File

@@ -0,0 +1,56 @@
'use strict'
const assert = require('node:assert/strict')
const { test } = require('node:test')
const { RefResolver } = require('../index.js')
test('should get a sub schema by sub schema anchor', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const subSchemaAnchor = '#subSchemaId'
const schema = {
$id: schemaId,
definitions: {
subSchema: {
$id: subSchemaAnchor,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
}
}
refResolver.addSchema(schema)
const resolvedSchema = refResolver.getSchema(schemaId, subSchemaAnchor)
assert.equal(resolvedSchema, schema.definitions.subSchema)
})
test('should fail to find a schema using an anchor instead of schema id', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const subSchemaAnchor = '#subSchemaId'
const schema = {
$id: schemaId,
definitions: {
subSchema: {
$id: subSchemaAnchor,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
}
}
refResolver.addSchema(schema)
try {
refResolver.getSchema(subSchemaAnchor)
} catch (err) {
assert.equal(
err.message, 'Cannot resolve ref "#subSchemaId#". Schema with id "#subSchemaId" is not found.'
)
}
})

View File

@@ -0,0 +1,199 @@
'use strict'
const assert = require('node:assert/strict')
const { test } = require('node:test')
const { RefResolver } = require('../index.js')
test('should not throw if there is a same schema with a same id', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const schema = {
$id: schemaId,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
refResolver.addSchema(schema)
refResolver.addSchema(schema)
const resolvedSchema = refResolver.getSchema(schemaId)
assert.deepStrictEqual(resolvedSchema, schema)
})
test('should throw if there is a same schema with a same id (allowEqualDuplicates === false)', () => {
const refResolver = new RefResolver({
allowEqualDuplicates: false
})
const schemaId = 'schemaId'
const schema = {
$id: schemaId,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
refResolver.addSchema(schema)
try {
refResolver.addSchema(schema)
assert.fail('should throw')
} catch (err) {
assert.equal(err.message, `There is already another schema with id "${schemaId}".`)
}
})
test('should throw if there is another schema with a same id', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const schema1 = {
$id: schemaId,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
const schema2 = {
$id: schemaId,
type: 'object',
properties: {
bar: { type: 'string' }
}
}
refResolver.addSchema(schema1)
try {
refResolver.addSchema(schema2)
assert.fail('should throw')
} catch (err) {
assert.equal(err.message, `There is already another schema with id "${schemaId}".`)
}
const resolvedSchema = refResolver.getSchema(schemaId)
assert.deepStrictEqual(resolvedSchema, schema1)
})
test('should not throw if there is a same sub schema with a same id', () => {
const refResolver = new RefResolver()
const subSchemaId = 'subSchemaId'
const schema1 = {
$id: 'schemaId1',
definitions: {
subSchema: {
$id: subSchemaId,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
}
}
const schema2 = {
$id: 'schemaId2',
definitions: {
subSchema: {
$id: subSchemaId,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
}
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
const resolvedSchema = refResolver.getSchema(subSchemaId)
assert.deepStrictEqual(resolvedSchema, schema1.definitions.subSchema)
assert.deepStrictEqual(resolvedSchema, schema2.definitions.subSchema)
})
test('should throw if there is another different sub schema with a same id', () => {
const refResolver = new RefResolver()
const subSchemaId = 'subSchemaId'
const schema1 = {
$id: 'schemaId1',
definitions: {
subSchema: {
$id: subSchemaId,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
}
}
const schema2 = {
$id: 'schemaId2',
definitions: {
subSchema: {
$id: subSchemaId,
type: 'object',
properties: {
bar: { type: 'string' }
}
}
}
}
refResolver.addSchema(schema1)
try {
refResolver.addSchema(schema2)
assert.fail('should throw')
} catch (err) {
assert.equal(err.message, `There is already another schema with id "${subSchemaId}".`)
}
const resolvedSchema = refResolver.getSchema(subSchemaId)
assert.deepStrictEqual(resolvedSchema, schema1.definitions.subSchema)
assert.notDeepStrictEqual(resolvedSchema, schema2.definitions.subSchema)
})
test('should throw if there is the same anchor in the same schema', () => {
const refResolver = new RefResolver()
const subSchemaAnchor = '#subSchemaId'
const schema = {
$id: 'schemaId1',
definitions: {
subSchema1: {
$id: subSchemaAnchor,
type: 'object',
properties: {
foo: { type: 'string' }
}
},
subSchema2: {
$id: subSchemaAnchor,
type: 'object',
properties: {
bar: { type: 'string' }
}
}
}
}
try {
refResolver.addSchema(schema)
assert.fail('should throw')
} catch (err) {
assert.equal(err.message, 'There is already another anchor "#subSchemaId" in a schema "schemaId1".')
}
})

View File

@@ -0,0 +1,236 @@
'use strict'
const assert = require('node:assert/strict')
const { test } = require('node:test')
const { RefResolver } = require('../index.js')
test('should throw id schema not found', () => {
const refResolver = new RefResolver()
try {
refResolver.derefSchema('schemaId1')
} catch (err) {
assert.strictEqual(err.message, 'Schema with id "schemaId1" is not found.')
}
})
test('should throw id source schema has a key with a same key as ref schema, but diff value', () => {
const refResolver = new RefResolver()
const schemaId1 = 'schemaId1'
const schemaId2 = 'schemaId2'
const schema1 = {
$id: schemaId1,
$ref: schemaId2,
properties: {
foo: { type: 'string' }
}
}
const schema2 = {
$id: schemaId2,
type: 'object',
properties: {
foo: { type: 'number' }
}
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
try {
refResolver.derefSchema('schemaId1')
assert.fail('should throw error')
} catch (err) {
assert.strictEqual(
err.message,
'Cannot resolve ref "schemaId2". Property "properties" is already exist in schema "schemaId1".'
)
}
})
test('should not throw id source schema has a key with a same key and value as ref schema', () => {
const refResolver = new RefResolver()
const schemaId1 = 'schemaId1'
const schemaId2 = 'schemaId2'
const schema1 = {
$id: schemaId1,
$ref: schemaId2,
properties: {
foo: { type: 'string' }
}
}
const schema2 = {
$id: schemaId2,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
refResolver.derefSchema(schemaId1)
const derefSchema = refResolver.getDerefSchema(schemaId1)
assert.deepStrictEqual(derefSchema, {
$id: schemaId1,
type: 'object',
properties: {
foo: { type: 'string' }
}
})
})
test('should get deref schema from the cache', () => {
const refResolver = new RefResolver()
const schemaId1 = 'schemaId1'
const schemaId2 = 'schemaId2'
const schema1 = {
$id: schemaId1,
$ref: schemaId2,
properties: {
foo: { type: 'string' }
}
}
const schema2 = {
$id: schemaId2,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
refResolver.derefSchema(schemaId1)
schema1.properties = {}
refResolver.derefSchema(schemaId1)
const derefSchema = refResolver.getDerefSchema(schemaId1)
assert.deepStrictEqual(derefSchema, {
$id: schemaId1,
type: 'object',
properties: {
foo: { type: 'string' }
}
})
})
test('should insert ref symbol', () => {
const refResolver = new RefResolver({
insertRefSymbol: true
})
const schemaId1 = 'schemaId1'
const schemaId2 = 'schemaId2'
const schema1 = {
$id: schemaId1,
$ref: schemaId2
}
const schema2 = {
$id: schemaId2,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
refResolver.derefSchema(schemaId1)
const derefSchema = refResolver.getDerefSchema(schemaId1)
assert.deepStrictEqual(derefSchema, {
$id: schemaId1,
type: 'object',
properties: {
foo: { type: 'string' }
},
[Symbol.for('json-schema-ref')]: schemaId2
})
})
test('should clone schema without refs', () => {
const refResolver = new RefResolver({
cloneSchemaWithoutRefs: true
})
const schemaId = 'schemaId2'
const schema = {
$id: schemaId,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
refResolver.addSchema(schema)
refResolver.derefSchema(schemaId)
schema.properties = null
const derefSchema = refResolver.getDerefSchema(schemaId)
assert.deepStrictEqual(derefSchema, {
$id: schemaId,
type: 'object',
properties: {
foo: { type: 'string' }
}
})
})
test('should throw if target ref schema is not found', () => {
const inputSchema = {
$id: 'http://example.com/root.json',
definitions: {
A: { $id: '#foo' },
B: {
$id: 'other.json',
definitions: {
X: { $id: '#bar', type: 'string' },
Y: { $id: 't/inner.json' }
}
},
C: {
$id: 'urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f',
type: 'object'
}
}
}
const addresSchema = {
$id: 'relativeAddress', // Note: prefer always absolute URI like: http://mysite.com
type: 'object',
properties: {
zip: { $ref: 'urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f' },
city2: { $ref: '#foo' }
}
}
const refResolver = new RefResolver()
refResolver.addSchema(inputSchema)
refResolver.addSchema(addresSchema)
try {
refResolver.derefSchema('relativeAddress')
} catch (error) {
assert.strictEqual(
error.message,
'Cannot resolve ref "#foo". Ref "#foo" is not found in schema "relativeAddress".'
)
}
})

View File

@@ -0,0 +1,363 @@
'use strict'
const assert = require('node:assert/strict')
const { test } = require('node:test')
const { RefResolver } = require('../index.js')
test('should resolve reference', () => {
const refResolver = new RefResolver()
const schemaId1 = 'schemaId1'
const schema1 = {
$id: schemaId1,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
const schemaId2 = 'schemaId2'
const schema2 = {
$id: schemaId2,
$ref: schemaId1
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
const derefSchema1 = refResolver.getDerefSchema(schemaId1)
assert.deepStrictEqual(derefSchema1, schema1)
const derefSchema2 = refResolver.getDerefSchema(schemaId2)
assert.deepStrictEqual(derefSchema2, {
$id: schemaId2,
type: 'object',
properties: {
foo: { type: 'string' }
}
})
})
test('should get deref schema by anchor', () => {
const refResolver = new RefResolver()
const schemaId1 = 'schemaId1'
const schemaId2 = 'schemaId2'
const schema1 = {
$id: schemaId1,
definitions: {
$id: '#subschema',
type: 'object',
properties: {
foo: { type: 'string' },
bar: { $ref: schemaId2 }
}
}
}
const schema2 = {
$id: schemaId2,
type: 'object',
properties: {
baz: { type: 'string' }
}
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
const derefSubSchema = refResolver.getDerefSchema(schemaId1, '#subschema')
assert.deepStrictEqual(derefSubSchema, {
$id: '#subschema',
type: 'object',
properties: {
foo: { type: 'string' },
bar: {
type: 'object',
properties: {
baz: { type: 'string' }
}
}
}
})
})
test('should merge main and ref schemas', () => {
const refResolver = new RefResolver()
const schemaId1 = 'schemaId1'
const schema1 = {
$id: schemaId1,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
const schemaId2 = 'schemaId2'
const schema2 = {
$id: schemaId2,
type: 'object',
properties: {
foo: { $ref: schemaId1 + '#/properties/foo' },
bar: { type: 'string' }
}
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
const derefSchema1 = refResolver.getDerefSchema(schemaId1)
assert.deepStrictEqual(derefSchema1, schema1)
const derefSchema2 = refResolver.getDerefSchema(schemaId2)
assert.deepStrictEqual(derefSchema2, {
$id: schemaId2,
type: 'object',
properties: {
foo: { type: 'string' },
bar: { type: 'string' }
}
})
})
test('should merge multiple nested schemas', () => {
const refResolver = new RefResolver()
const schemaId1 = 'schemaId1'
const schema1 = {
$id: schemaId1,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
const schemaId2 = 'schemaId2'
const schema2 = {
$id: schemaId2,
type: 'object',
properties: {
foo: { $ref: schemaId1 + '#/properties/foo' },
bar: { type: 'string' }
}
}
const schemaId3 = 'schemaId3'
const schema3 = {
$id: schemaId3,
type: 'object',
properties: {
foo: { $ref: schemaId2 + '#/properties/foo' },
bar: { $ref: schemaId2 + '#/properties/bar' },
baz: { type: 'string' }
}
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
refResolver.addSchema(schema3)
const derefSchema3 = refResolver.getDerefSchema(schemaId3)
assert.deepStrictEqual(derefSchema3, {
$id: schemaId3,
type: 'object',
properties: {
foo: { type: 'string' },
bar: { type: 'string' },
baz: { type: 'string' }
}
})
})
test('should resolve schema with circular reference', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const schema = {
$id: schemaId,
type: 'object',
properties: {
foo: { $ref: '#' }
}
}
refResolver.addSchema(schema)
const derefSchema = refResolver.getDerefSchema(schemaId)
const expectedSchema = {
$id: schemaId,
type: 'object',
properties: {
foo: {
type: 'object',
properties: {}
}
}
}
expectedSchema.properties.foo.properties.foo = expectedSchema.properties.foo
assert.deepStrictEqual(derefSchema, expectedSchema)
})
test('should resolve schema with cross circular reference', () => {
const refResolver = new RefResolver()
const schemaId1 = 'schemaId1'
const schemaId2 = 'schemaId2'
const schema1 = {
$id: schemaId1,
type: 'object',
properties: {
foo: { $ref: schemaId2 }
}
}
const schema2 = {
$id: schemaId2,
type: 'object',
properties: {
bar: { $ref: schemaId1 }
}
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
const derefSchema1 = refResolver.getDerefSchema(schemaId1)
const derefSchema2 = refResolver.getDerefSchema(schemaId2)
const expectedSchema1 = {
$id: schemaId1,
type: 'object',
properties: {
foo: {
type: 'object',
properties: {
bar: {
type: 'object',
properties: {}
}
}
}
}
}
expectedSchema1.properties.foo.properties.bar.properties.foo = expectedSchema1.properties.foo
const expectedSchema2 = {
$id: schemaId2,
type: 'object',
properties: {
bar: {
type: 'object',
properties: {
foo: {
type: 'object',
properties: {}
}
}
}
}
}
expectedSchema2.properties.bar.properties.foo.properties.bar = expectedSchema2.properties.bar
assert.deepStrictEqual(derefSchema1, expectedSchema1)
assert.deepStrictEqual(derefSchema2, expectedSchema2)
})
test('should resolve nested multiple times refs', () => {
const refResolver = new RefResolver()
const schemaId1 = 'schemaId1'
const schema1 = {
$id: schemaId1,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
const schemaId2 = 'schemaId2'
const schema2 = {
$id: schemaId2,
$ref: schemaId1,
required: ['foo']
}
const schemaId3 = 'schemaId3'
const schema3 = {
$id: schemaId3,
$ref: schemaId2,
additionalProperties: false
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
refResolver.addSchema(schema3)
// Don't switch the order of these two lines.
const derefSchema3 = refResolver.getDerefSchema(schemaId3)
const derefSchema2 = refResolver.getDerefSchema(schemaId2)
assert.deepStrictEqual(derefSchema2, {
$id: schemaId2,
type: 'object',
properties: {
foo: { type: 'string' }
},
required: ['foo']
})
assert.deepStrictEqual(derefSchema3, {
$id: schemaId3,
type: 'object',
properties: {
foo: { type: 'string' }
},
required: ['foo'],
additionalProperties: false
})
})
test('should resolve infinite ref chain', () => {
const refResolver = new RefResolver()
const schemaId1 = 'schemaId1'
const schemaId2 = 'schemaId2'
const schemaId3 = 'schemaId3'
const schema1 = {
$id: schemaId1,
$ref: schemaId2
}
const schema2 = {
$id: schemaId2,
$ref: schemaId3
}
const schema3 = {
$id: schemaId3,
$ref: schemaId1
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
refResolver.addSchema(schema3)
const derefSchema1 = refResolver.getDerefSchema(schemaId1)
const derefSchema2 = refResolver.getDerefSchema(schemaId2)
const derefSchema3 = refResolver.getDerefSchema(schemaId3)
assert.deepStrictEqual(derefSchema1, {
$id: schemaId1
})
assert.deepStrictEqual(derefSchema2, {
$id: schemaId2
})
assert.deepStrictEqual(derefSchema3, {
$id: schemaId3
})
})

View File

@@ -0,0 +1,158 @@
'use strict'
const assert = require('node:assert/strict')
const { test } = require('node:test')
const { RefResolver } = require('../index.js')
test('should return all nested schema dependencies', () => {
const refResolver = new RefResolver()
const schema1Id = 'schemaId1'
const schema1 = {
$id: schema1Id,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
const schema2Id = 'schemaId2'
const schema2 = {
$id: schema2Id,
$ref: schema1Id
}
const schema3Id = 'schemaId3'
const schema3 = {
$id: schema3Id,
$ref: schema2Id
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
refResolver.addSchema(schema3)
const schema1Deps = refResolver.getSchemaDependencies(schema1Id)
assert.deepStrictEqual(schema1Deps, {})
const schema2Deps = refResolver.getSchemaDependencies(schema2Id)
assert.deepStrictEqual(schema2Deps, { [schema1Id]: schema1 })
const schema3Deps = refResolver.getSchemaDependencies(schema3Id)
assert.deepStrictEqual(schema3Deps, {
[schema1Id]: schema1,
[schema2Id]: schema2
})
})
test('should resolve a dependency to a subschema', () => {
const refResolver = new RefResolver()
const schema1Id = 'schemaId1'
const subSchema1Id = 'subSchemaId1'
const schema1 = {
$id: schema1Id,
definitions: {
subSchema: {
$id: subSchema1Id,
type: 'object',
properties: {
bar: { type: 'string' }
}
}
}
}
const schema2Id = 'schemaId2'
const schema2 = {
$id: schema2Id,
$ref: subSchema1Id + '#/definitions/subSchema'
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
const schema1Deps = refResolver.getSchemaDependencies(schema1Id)
assert.deepStrictEqual(schema1Deps, {})
const schema2Deps = refResolver.getSchemaDependencies(schema2Id)
assert.deepStrictEqual(schema2Deps, { [subSchema1Id]: schema1.definitions.subSchema })
})
test('should resolve a dependency with a json path', () => {
const refResolver = new RefResolver()
const schema1Id = 'schemaId1'
const subSchema1Id = 'subSchemaId1'
const schema1 = {
$id: schema1Id,
definitions: {
subSchema: {
$id: subSchema1Id,
type: 'object',
properties: {
bar: { type: 'string' }
}
}
}
}
const schema2Id = 'schemaId2'
const schema2 = {
$id: schema2Id,
$ref: schema1Id + '#/definitions/subSchema'
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
const schema1Deps = refResolver.getSchemaDependencies(schema1Id)
assert.deepStrictEqual(schema1Deps, {})
const schema2Deps = refResolver.getSchemaDependencies(schema2Id)
assert.deepStrictEqual(schema2Deps, { [schema1Id]: schema1 })
})
test('should include dependency schema only once', () => {
const refResolver = new RefResolver()
const schema1Id = 'schemaId1'
const schema1 = {
$id: schema1Id,
type: 'object',
properties: {
foo: { type: 'string' }
}
}
const schema2Id = 'schemaId2'
const schema2 = {
$id: schema2Id,
$ref: schema1Id
}
const schema3Id = 'schemaId3'
const schema3 = {
$id: schema3Id,
allOf: [
{ $ref: schema1Id },
{ $ref: schema2Id }
]
}
refResolver.addSchema(schema1)
refResolver.addSchema(schema2)
refResolver.addSchema(schema3)
const schema1Deps = refResolver.getSchemaDependencies(schema1Id)
assert.deepStrictEqual(schema1Deps, {})
const schema2Deps = refResolver.getSchemaDependencies(schema2Id)
assert.deepStrictEqual(schema2Deps, { [schema1Id]: schema1 })
const schema3Deps = refResolver.getSchemaDependencies(schema3Id)
assert.deepStrictEqual(schema3Deps, {
[schema1Id]: schema1,
[schema2Id]: schema2
})
})

View File

@@ -0,0 +1,87 @@
'use strict'
const assert = require('node:assert/strict')
const { test } = require('node:test')
const { RefResolver } = require('../index.js')
test('should return schema refs', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const schema = {
$id: 'schemaId',
type: 'object',
properties: {
foo: { $ref: 'schemaId2#/definitions/foo' },
bar: { $ref: 'schemaId3#/definitions/bar' },
baz: {
type: 'object',
properties: {
qux: { $ref: 'schemaId4#/definitions/qux' }
}
}
}
}
refResolver.addSchema(schema)
const schemaRefs = refResolver.getSchemaRefs(schemaId)
assert.deepStrictEqual(schemaRefs, [
{ schemaId: 'schemaId2', jsonPointer: '#/definitions/foo' },
{ schemaId: 'schemaId3', jsonPointer: '#/definitions/bar' },
{ schemaId: 'schemaId4', jsonPointer: '#/definitions/qux' }
])
})
test('should return nested schema refs', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const subSchemaId = 'subSchemaId'
const schema = {
$id: 'schemaId',
$ref: 'schemaId2#/definitions/subschema',
definitions: {
subschema: {
$id: subSchemaId,
type: 'object',
properties: {
foo: { $ref: 'schemaId2#/definitions/foo' },
bar: { $ref: 'schemaId3#/definitions/bar' },
baz: {
type: 'object',
properties: {
qux: { $ref: 'schemaId4#/definitions/qux' }
}
}
}
}
}
}
refResolver.addSchema(schema)
const schemaRefs = refResolver.getSchemaRefs(schemaId)
assert.deepStrictEqual(schemaRefs, [
{ schemaId: 'schemaId2', jsonPointer: '#/definitions/subschema' }
])
const subSchemaRefs = refResolver.getSchemaRefs(subSchemaId)
assert.deepStrictEqual(subSchemaRefs, [
{ schemaId: 'schemaId2', jsonPointer: '#/definitions/foo' },
{ schemaId: 'schemaId3', jsonPointer: '#/definitions/bar' },
{ schemaId: 'schemaId4', jsonPointer: '#/definitions/qux' }
])
})
test('should throw is schema does not exist', () => {
const refResolver = new RefResolver()
try {
refResolver.getSchemaRefs('schemaId')
assert.fail('should throw error')
} catch (error) {
assert.equal(error.message, 'Schema with id "schemaId" is not found.')
}
})

View File

@@ -0,0 +1,222 @@
'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)
})

View File

@@ -0,0 +1,28 @@
'use strict'
const assert = require('node:assert/strict')
const { test } = require('node:test')
const { RefResolver } = require('../index.js')
test('should return true if schema exists', () => {
const refResolver = new RefResolver()
const schemaId = 'schemaId'
const schema = {
$id: 'schemaId',
type: 'object',
properties: {
foo: { type: 'string' }
}
}
refResolver.addSchema(schema)
const hasSchema = refResolver.hasSchema(schemaId)
assert.strictEqual(hasSchema, true)
})
test('should return false if schema does not exist', () => {
const refResolver = new RefResolver()
const hasSchema = refResolver.hasSchema('schemaId')
assert.strictEqual(hasSchema, false)
})