1- import { findSchemaDefinition , RJSFSchema } from '../src' ;
2- import { findSchemaDefinitionRecursive } from '../src/findSchemaDefinition' ;
1+ import { findSchemaDefinition , ID_KEY , RJSFSchema } from '../src' ;
2+ import { findSchemaDefinitionRecursive , makeAllReferencesAbsolute } from '../src/findSchemaDefinition' ;
33
44const schema : RJSFSchema = {
55 type : 'object' ,
@@ -85,29 +85,6 @@ const internalSchema: RJSFSchema = {
8585 } ,
8686} ;
8787
88- const absoluteInternalSchema : RJSFSchema = {
89- ...internalSchema ,
90- $defs : {
91- ...internalSchema . $defs ,
92- circularRef : { $ref : 'https://example.com/bundled.schema.json/#/$defs/circularRef' } ,
93- undefinedRef : { $ref : 'https://example.com/bundled.ref.json#/$defs/undefined' } ,
94- } ,
95- properties : {
96- ...internalSchema . properties ,
97- string : { $ref : 'https://example.com/bundled.ref.json#/$defs/string' } ,
98- allOf : {
99- allOf : [
100- {
101- $ref : 'https://example.com/bundled.ref.json#/$defs/string' ,
102- } ,
103- {
104- title : 'String' ,
105- } ,
106- ] ,
107- } ,
108- } ,
109- } ;
110-
11188const bundledSchema : RJSFSchema = {
11289 $schema : 'https://json-schema.org/draft/2020-12/schema' ,
11390 type : 'object' ,
@@ -209,11 +186,11 @@ describe('findSchemaDefinition()', () => {
209186 ) ;
210187 } ) ;
211188 it ( 'correctly resolves absolute bundled refs when JSON Schema Draft 2020-12' , ( ) => {
212- expect ( findSchemaDefinition ( '#/$defs/bundledAbsoluteRef' , bundledSchema ) ) . toStrictEqual ( absoluteInternalSchema ) ;
189+ expect ( findSchemaDefinition ( '#/$defs/bundledAbsoluteRef' , bundledSchema ) ) . toStrictEqual ( internalSchema ) ;
213190 } ) ;
214191 it ( 'correctly resolves absolute bundled refs with anchors within a JSON Schema Draft 2020-12' , ( ) => {
215192 expect ( findSchemaDefinition ( '#/$defs/bundledAbsoluteRefWithAnchor' , bundledSchema ) ) . toBe (
216- absoluteInternalSchema . properties ! . num ,
193+ internalSchema . properties ! . num ,
217194 ) ;
218195 } ) ;
219196 it ( 'correctly resolves absolute bundled refs in arrays within a JSON Schema Draft 2020-12' , ( ) => {
@@ -223,29 +200,27 @@ describe('findSchemaDefinition()', () => {
223200 } ) ;
224201 it ( 'correctly resolves absolute bundled refs within a JSON Schema Draft 2020-12 without an `$id`' , ( ) => {
225202 const { $id : d , ...bundledSchemaWithoutId } = bundledSchema ;
226- expect ( findSchemaDefinition ( '#/$defs/bundledAbsoluteRef' , bundledSchemaWithoutId ) ) . toStrictEqual (
227- absoluteInternalSchema ,
228- ) ;
203+ expect ( findSchemaDefinition ( '#/$defs/bundledAbsoluteRef' , bundledSchemaWithoutId ) ) . toStrictEqual ( internalSchema ) ;
229204 } ) ;
230205 it ( 'correctly resolves relative bundled refs within a JSON Schema Draft 2020-12' , ( ) => {
231- expect ( findSchemaDefinition ( '#/$defs/bundledRelativeRef' , bundledSchema ) ) . toStrictEqual ( absoluteInternalSchema ) ;
206+ expect ( findSchemaDefinition ( '#/$defs/bundledRelativeRef' , bundledSchema ) ) . toStrictEqual ( internalSchema ) ;
232207 } ) ;
233208 it ( 'correctly resolves relative bundled refs with anchors within a JSON Schema Draft 2020-12' , ( ) => {
234209 expect ( findSchemaDefinition ( '#/$defs/bundledRelativeRefWithAnchor' , bundledSchema ) ) . toBe (
235- absoluteInternalSchema . $defs ! . string ,
210+ internalSchema . $defs ! . string ,
236211 ) ;
237212 } ) ;
238213 it ( 'correctly resolves indirect bundled refs within a JSON Schema Draft 2020-12' , ( ) => {
239- expect ( findSchemaDefinition ( '#/$defs/indirectRef' , bundledSchema ) ) . toStrictEqual ( absoluteInternalSchema ) ;
214+ expect ( findSchemaDefinition ( '#/$defs/indirectRef' , bundledSchema ) ) . toStrictEqual ( internalSchema ) ;
240215 } ) ;
241216 it ( 'correctly resolves refs with explicit base URI in a bundled JSON Schema' , ( ) => {
242217 expect (
243218 findSchemaDefinition ( 'bundled.ref.json' , bundledSchema , 'https://example.com/undefined.ref.json' ) ,
244- ) . toStrictEqual ( absoluteInternalSchema ) ;
219+ ) . toStrictEqual ( internalSchema ) ;
245220 } ) ;
246221 it ( 'correctly resolves local refs with explicit base URI in a bundled JSON Schema' , ( ) => {
247222 expect ( findSchemaDefinition ( '#/properties/num' , bundledSchema , 'https://example.com/bundled.ref.json' ) ) . toBe (
248- absoluteInternalSchema . properties ! . num ,
223+ internalSchema . properties ! . num ,
249224 ) ;
250225 } ) ;
251226 it ( 'throws error when relative ref is undefined in a bundled JSON Schema' , ( ) => {
@@ -255,7 +230,7 @@ describe('findSchemaDefinition()', () => {
255230 } ) ;
256231 it ( 'throws error when relative ref with anchor is undefined in a bundled JSON Schema' , ( ) => {
257232 expect ( ( ) => findSchemaDefinition ( '#/$defs/undefinedRefWithAnchor' , bundledSchema ) ) . toThrowError (
258- 'Could not find a definition for https://example.com/bundled.ref.json #/$defs/undefined' ,
233+ 'Could not find a definition for #/$defs/undefined' ,
259234 ) ;
260235 } ) ;
261236 it ( 'throws error when local ref is undefined in a bundled JSON Schema with explicit base URI' , ( ) => {
@@ -270,7 +245,7 @@ describe('findSchemaDefinition()', () => {
270245 } ) ;
271246 it ( 'throws error when ref is a deep circular reference in a bundled JSON Schema' , ( ) => {
272247 expect ( ( ) => findSchemaDefinition ( '#/$defs/circularRef' , bundledSchema ) ) . toThrowError (
273- 'Definition for #/$defs/circularRef contains a circular reference through /bundled.ref.json/#/$defs/circularRef -> https://example.com/ bundled.schema.json/#/$defs/circularRef' ,
248+ 'Definition for #/$defs/circularRef contains a circular reference through /bundled.ref.json/#/$defs/circularRef -> / bundled.schema.json/#/$defs/circularRef -> #/$defs/circularRef' ,
274249 ) ;
275250 } ) ;
276251} ) ;
@@ -319,13 +294,11 @@ describe('findSchemaDefinitionRecursive()', () => {
319294 ) . toThrowError ( 'Could not find a definition for #/properties/num' ) ;
320295 } ) ;
321296 it ( 'correctly resolves absolute bundled refs within a JSON Schema Draft 2020-12' , ( ) => {
322- expect ( findSchemaDefinitionRecursive ( '#/$defs/bundledAbsoluteRef' , bundledSchema ) ) . toStrictEqual (
323- absoluteInternalSchema ,
324- ) ;
297+ expect ( findSchemaDefinitionRecursive ( '#/$defs/bundledAbsoluteRef' , bundledSchema ) ) . toStrictEqual ( internalSchema ) ;
325298 } ) ;
326299 it ( 'correctly resolves absolute bundled refs with anchors within a JSON Schema Draft 2020-12' , ( ) => {
327300 expect ( findSchemaDefinitionRecursive ( '#/$defs/bundledAbsoluteRefWithAnchor' , bundledSchema ) ) . toBe (
328- absoluteInternalSchema . properties ! . num ,
301+ internalSchema . properties ! . num ,
329302 ) ;
330303 } ) ;
331304 it ( 'correctly resolves absolute bundled refs in arrays within a JSON Schema Draft 2020-12' , ( ) => {
@@ -336,31 +309,29 @@ describe('findSchemaDefinitionRecursive()', () => {
336309 it ( 'correctly resolves absolute bundled refs within a JSON Schema Draft 2020-12 without an `$id`' , ( ) => {
337310 const { $id : d , ...bundledSchemaWithoutId } = bundledSchema ;
338311 expect ( findSchemaDefinitionRecursive ( '#/$defs/bundledAbsoluteRef' , bundledSchemaWithoutId ) ) . toStrictEqual (
339- absoluteInternalSchema ,
312+ internalSchema ,
340313 ) ;
341314 } ) ;
342315 it ( 'correctly resolves relative bundled refs within a JSON Schema Draft 2020-12' , ( ) => {
343- expect ( findSchemaDefinitionRecursive ( '#/$defs/bundledRelativeRef' , bundledSchema ) ) . toStrictEqual (
344- absoluteInternalSchema ,
345- ) ;
316+ expect ( findSchemaDefinitionRecursive ( '#/$defs/bundledRelativeRef' , bundledSchema ) ) . toStrictEqual ( internalSchema ) ;
346317 } ) ;
347318 it ( 'correctly resolves relative bundled refs with anchors within a JSON Schema Draft 2020-12' , ( ) => {
348319 expect ( findSchemaDefinitionRecursive ( '#/$defs/bundledRelativeRefWithAnchor' , bundledSchema ) ) . toBe (
349- absoluteInternalSchema . $defs ! . string ,
320+ internalSchema . $defs ! . string ,
350321 ) ;
351322 } ) ;
352323 it ( 'correctly resolves indirect bundled refs within a JSON Schema Draft 2020-12' , ( ) => {
353- expect ( findSchemaDefinitionRecursive ( '#/$defs/indirectRef' , bundledSchema ) ) . toStrictEqual ( absoluteInternalSchema ) ;
324+ expect ( findSchemaDefinitionRecursive ( '#/$defs/indirectRef' , bundledSchema ) ) . toStrictEqual ( internalSchema ) ;
354325 } ) ;
355326 it ( 'correctly resolves relative refs with explicit base URI in a bundled JSON Schema' , ( ) => {
356327 expect (
357328 findSchemaDefinitionRecursive ( 'bundled.ref.json' , bundledSchema , [ ] , 'https://example.com/undefined.ref.json' ) ,
358- ) . toStrictEqual ( absoluteInternalSchema ) ;
329+ ) . toStrictEqual ( internalSchema ) ;
359330 } ) ;
360331 it ( 'correctly resolves local refs with explicit base URI in a bundled JSON Schema' , ( ) => {
361332 expect (
362333 findSchemaDefinitionRecursive ( '#/properties/num' , bundledSchema , [ ] , 'https://example.com/bundled.ref.json' ) ,
363- ) . toBe ( absoluteInternalSchema . properties ! . num ) ;
334+ ) . toBe ( internalSchema . properties ! . num ) ;
364335 } ) ;
365336 it ( 'throws error when relative ref is undefined in a bundled JSON Schema' , ( ) => {
366337 expect ( ( ) => findSchemaDefinitionRecursive ( '#/$defs/undefinedRef' , bundledSchema ) ) . toThrowError (
@@ -369,7 +340,7 @@ describe('findSchemaDefinitionRecursive()', () => {
369340 } ) ;
370341 it ( 'throws error when relative ref with anchor is undefined in a bundled JSON Schema' , ( ) => {
371342 expect ( ( ) => findSchemaDefinitionRecursive ( '#/$defs/undefinedRefWithAnchor' , bundledSchema ) ) . toThrowError (
372- 'Could not find a definition for https://example.com/bundled.ref.json #/$defs/undefined' ,
343+ 'Could not find a definition for #/$defs/undefined' ,
373344 ) ;
374345 } ) ;
375346 it ( 'throws error when local ref is undefined in a bundled JSON Schema with explicit base URI' , ( ) => {
@@ -389,7 +360,34 @@ describe('findSchemaDefinitionRecursive()', () => {
389360 } ) ;
390361 it ( 'throws error when ref is a deep circular reference in a bundled JSON Schema' , ( ) => {
391362 expect ( ( ) => findSchemaDefinitionRecursive ( '#/$defs/circularRef' , bundledSchema , [ ] ) ) . toThrowError (
392- 'Definition for #/$defs/circularRef contains a circular reference through /bundled.ref.json/#/$defs/circularRef -> https://example.com/ bundled.schema.json/#/$defs/circularRef' ,
363+ 'Definition for #/$defs/circularRef contains a circular reference through /bundled.ref.json/#/$defs/circularRef -> / bundled.schema.json/#/$defs/circularRef -> #/$defs/circularRef' ,
393364 ) ;
394365 } ) ;
395366} ) ;
367+
368+ describe ( 'makeAllReferencesAbsolute()' , ( ) => {
369+ it ( 'correctly makes all references absolute in a JSON Schema' , ( ) => {
370+ expect ( makeAllReferencesAbsolute ( internalSchema , internalSchema [ ID_KEY ] ! ) ) . toStrictEqual ( {
371+ ...internalSchema ,
372+ $defs : {
373+ ...internalSchema . $defs ,
374+ circularRef : { $ref : 'https://example.com/bundled.schema.json/#/$defs/circularRef' } ,
375+ undefinedRef : { $ref : 'https://example.com/bundled.ref.json#/$defs/undefined' } ,
376+ } ,
377+ properties : {
378+ ...internalSchema . properties ,
379+ string : { $ref : 'https://example.com/bundled.ref.json#/$defs/string' } ,
380+ allOf : {
381+ allOf : [
382+ {
383+ $ref : 'https://example.com/bundled.ref.json#/$defs/string' ,
384+ } ,
385+ {
386+ title : 'String' ,
387+ } ,
388+ ] ,
389+ } ,
390+ } ,
391+ } ) ;
392+ } ) ;
393+ } ) ;
0 commit comments