Skip to content

Commit 19c1c26

Browse files
deps: use json-schema-ref-resolver
1 parent 9548178 commit 19c1c26

File tree

4 files changed

+36
-115
lines changed

4 files changed

+36
-115
lines changed

index.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
const merge = require('@fastify/deepmerge')()
66
const clone = require('rfdc')({ proto: true })
77
const { randomUUID } = require('node:crypto')
8+
const { RefResolver } = require('json-schema-ref-resolver')
89

910
const validate = require('./lib/schema-validator')
1011
const Serializer = require('./lib/serializer')
1112
const Validator = require('./lib/validator')
12-
const RefResolver = require('./lib/ref-resolver')
1313
const Location = require('./lib/location')
1414

1515
let largeArraySize = 2e4
@@ -53,8 +53,7 @@ function resolveRef (context, location, ref) {
5353
const jsonPointer = ref.slice(hashIndex) || '#'
5454

5555
const schema = context.refResolver.getSchema(schemaId, jsonPointer)
56-
57-
if (schema === undefined) {
56+
if (schema === null) {
5857
throw new Error(`Cannot find reference "${ref}"`)
5958
}
6059

@@ -66,6 +65,13 @@ function resolveRef (context, location, ref) {
6665
return newLocation
6766
}
6867

68+
function getSchemaId (schema, rootSchemaId) {
69+
if (schema.$id && schema.$id.charAt(0) !== '#') {
70+
return schema.$id
71+
}
72+
return rootSchemaId
73+
}
74+
6975
function build (schema, options) {
7076
isValidSchema(schema)
7177

@@ -82,12 +88,19 @@ function build (schema, options) {
8288
validatorSchemasIds: new Set()
8389
}
8490

85-
context.refResolver.addSchema(schema, context.rootSchemaId)
91+
const schemaId = getSchemaId(schema, context.rootSchemaId)
92+
if (!context.refResolver.hasSchema(schemaId)) {
93+
context.refResolver.addSchema(schema, context.rootSchemaId)
94+
}
8695

8796
if (options.schema) {
88-
for (const key of Object.keys(options.schema)) {
89-
isValidSchema(options.schema[key], key)
90-
context.refResolver.addSchema(options.schema[key], key)
97+
for (const key in options.schema) {
98+
const schema = options.schema[key]
99+
const schemaId = getSchemaId(schema, key)
100+
if (!context.refResolver.hasSchema(schemaId)) {
101+
isValidSchema(schema, key)
102+
context.refResolver.addSchema(schema, key)
103+
}
91104
}
92105
}
93106

lib/ref-resolver.js

Lines changed: 0 additions & 104 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
"ajv-formats": "^2.1.1",
5353
"fast-deep-equal": "^3.1.3",
5454
"fast-uri": "^2.1.0",
55-
"rfdc": "^1.2.0"
55+
"rfdc": "^1.2.0",
56+
"json-schema-ref-resolver": "^1.0.1"
5657
},
5758
"standard": {
5859
"ignore": [

test/ref.test.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,10 @@ test('Bad key', t => {
13771377
})
13781378
t.fail('Should throw')
13791379
} catch (err) {
1380-
t.equal(err.message, 'Cannot find reference "extrenal#/definitions/projectId"')
1380+
t.equal(
1381+
err.message,
1382+
'Cannot resolve ref "extrenal#/definitions/projectId". Schema with id "extrenal" is not found.'
1383+
)
13811384
}
13821385
})
13831386

@@ -2076,7 +2079,11 @@ test('should throw an Error if two non-identical schemas with same id are provid
20762079
]
20772080
}
20782081

2079-
t.throws(() => build(schema), new Error('There is already another schema with id inner_schema'))
2082+
try {
2083+
build(schema)
2084+
} catch (err) {
2085+
t.equal(err.message, 'There is already another schema with id "inner_schema".')
2086+
}
20802087
})
20812088

20822089
test('ref internal - throw if schema has definition twice with different shape', (t) => {
@@ -2115,5 +2122,9 @@ test('ref internal - throw if schema has definition twice with different shape',
21152122
}
21162123
}
21172124

2118-
t.throws(() => build(schema), Error('There is already another schema with id test##uri'))
2125+
try {
2126+
build(schema)
2127+
} catch (err) {
2128+
t.equal(err.message, 'There is already another anchor "#uri" in a schema "test".')
2129+
}
21192130
})

0 commit comments

Comments
 (0)