@@ -12,6 +12,8 @@ interface JsonSchemaUnifierOptions {
12
12
logs ?: boolean ;
13
13
definitionsPath ?: string ;
14
14
definitionsPathSeparator ?: string ;
15
+ /** If defined, the definitions path will be relative to the value. By default, it's relative to the main schema */
16
+ baseDir ?: string ;
15
17
}
16
18
17
19
@@ -109,15 +111,19 @@ export class JsonSchemaUnifier {
109
111
this . log ( "newPath" , path ) ;
110
112
const parts = path . split ( "#" ) ;
111
113
if ( parts [ 0 ] === this . mainSchemaPath ) return `#${ parts [ 1 ] } ` ;
112
- const relativePath = relative ( this . mainSchemaPath , parts [ 0 ] ) ;
114
+ const relativePath = relative ( this . options . baseDir ? this . options . baseDir : this . mainSchemaPath , parts [ 0 ] ) ;
113
115
this . log ( "Relative path" , relativePath ) ;
114
- const defPathParts = [
115
- ...relativePath
116
- . replace ( / ^ ( \. + [ \\ / ] ) + / i, "" )
117
- . replace ( / ( \. s c h e m a ) ? \. ( j s o n | y a m l | y m l ) $ / i, "" )
118
- . split ( "/" )
119
- . filter ( p => p )
120
- ] ;
116
+ const defPathParts = relativePath
117
+ . replace ( / ( \. s c h e m a ) ? \. ( j s o n | y a m l | y m l ) $ / i, "" )
118
+ . split ( "/" )
119
+ . filter ( p => p && p !== "." ) ;
120
+ while ( defPathParts . includes ( '..' ) ) {
121
+ const index = defPathParts . indexOf ( '..' ) ;
122
+ if ( index > 0 )
123
+ defPathParts . splice ( index - 1 , 2 ) ;
124
+ else
125
+ defPathParts . splice ( index , 1 ) ;
126
+ }
121
127
if ( parts . length > 1 ) {
122
128
defPathParts . push ( ...parts [ 1 ] . split ( "/" ) . filter ( p => p ) ) ;
123
129
}
0 commit comments