5
5
const merge = require ( '@fastify/deepmerge' ) ( )
6
6
const clone = require ( 'rfdc' ) ( { proto : true } )
7
7
const { randomUUID } = require ( 'node:crypto' )
8
+ const { RefResolver } = require ( 'json-schema-ref-resolver' )
8
9
9
10
const validate = require ( './lib/schema-validator' )
10
11
const Serializer = require ( './lib/serializer' )
11
12
const Validator = require ( './lib/validator' )
12
- const RefResolver = require ( './lib/ref-resolver' )
13
13
const Location = require ( './lib/location' )
14
14
15
15
let largeArraySize = 2e4
@@ -53,8 +53,7 @@ function resolveRef (context, location, ref) {
53
53
const jsonPointer = ref . slice ( hashIndex ) || '#'
54
54
55
55
const schema = context . refResolver . getSchema ( schemaId , jsonPointer )
56
-
57
- if ( schema === undefined ) {
56
+ if ( schema === null ) {
58
57
throw new Error ( `Cannot find reference "${ ref } "` )
59
58
}
60
59
@@ -66,6 +65,13 @@ function resolveRef (context, location, ref) {
66
65
return newLocation
67
66
}
68
67
68
+ function getSchemaId ( schema , rootSchemaId ) {
69
+ if ( schema . $id && schema . $id . charAt ( 0 ) !== '#' ) {
70
+ return schema . $id
71
+ }
72
+ return rootSchemaId
73
+ }
74
+
69
75
function build ( schema , options ) {
70
76
isValidSchema ( schema )
71
77
@@ -82,12 +88,19 @@ function build (schema, options) {
82
88
validatorSchemasIds : new Set ( )
83
89
}
84
90
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
+ }
86
95
87
96
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
+ }
91
104
}
92
105
}
93
106
0 commit comments