Skip to content

Commit 8599262

Browse files
committed
Pass options to $ref-resolver
1 parent 60e534c commit 8599262

File tree

4 files changed

+49
-38
lines changed

4 files changed

+49
-38
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
8989
| enableConstEnums | boolean | `true` | Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)? |
9090
| style | object | `{ bracketSpacing: false, printWidth: 120, semi: true, singleQuote: false, tabWidth: 2, trailingComma: 'none', useTabs: false }` | A [Prettier](https://prettier.io/docs/en/options.html) configuration |
9191
| unreachableDefinitions | boolean | `false` | Generates code for `definitions` that aren't referenced by the schema. |
92+
| $refOptions | object | `{}` | [$RefParser](https://github.com/BigstickCarpet/json-schema-ref-parser) Options, used when resolving `$ref`s |
9293
## CLI
9394

9495
A simple CLI utility is provided with this package.

src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { parse } from './parser'
1111
import { dereference } from './resolver'
1212
import { error, stripExtension, Try } from './utils'
1313
import { validate } from './validator'
14+
import { Options as $RefOptions } from 'json-schema-ref-parser'
1415

1516
export { EnumJSONSchema, JSONSchema, NamedEnumJSONSchema } from './types/JSONSchema'
1617

@@ -39,6 +40,10 @@ export interface Options {
3940
* Generate code for `definitions` that aren't referenced by the schema?
4041
*/
4142
unreachableDefinitions: boolean
43+
/**
44+
* [$RefParser](https://github.com/BigstickCarpet/json-schema-ref-parser) Options, used when resolving `$ref`s
45+
*/
46+
$refOptions: $RefOptions
4247
}
4348

4449
export const DEFAULT_OPTIONS: Options = {
@@ -59,7 +64,8 @@ export const DEFAULT_OPTIONS: Options = {
5964
trailingComma: 'none',
6065
useTabs: false
6166
},
62-
unreachableDefinitions: false
67+
unreachableDefinitions: false,
68+
$refOptions: {}
6369
}
6470

6571
export function compileFromFile(
@@ -102,7 +108,7 @@ export async function compile(
102108

103109
return format(generate(
104110
optimize(
105-
parse(await dereference(normalize(schema, name), _options.cwd), _options)
111+
parse(await dereference(normalize(schema, name), _options), _options)
106112
),
107113
_options
108114
), _options)

src/resolver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { whiteBright } from 'cli-color'
33
import { JSONSchema } from './types/JSONSchema'
44
import { log } from './utils'
55

6-
export async function dereference(schema: JSONSchema, cwd: string): Promise<JSONSchema> {
6+
export async function dereference(schema: JSONSchema, {cwd, $refOptions}: {cwd: string, $refOptions: $RefParser.Options}): Promise<JSONSchema> {
77
log(whiteBright.bgGreen('resolver'), schema, cwd)
88
const parser = new $RefParser
9-
return parser.dereference(cwd, schema, {})
9+
return parser.dereference(cwd, schema, $refOptions)
1010
}

types/json-schema-ref-parser.d.ts

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ declare module 'json-schema-ref-parser' {
1010

1111
export = $RefParser
1212

13+
type Options = $RefParser.Options
14+
1315
/**
1416
* This is the default export of JSON Schema $Ref Parser. You can creates instances of this class using new $RefParser(), or you can just call its static methods.
1517
*
@@ -100,50 +102,52 @@ declare module 'json-schema-ref-parser' {
100102
): Promise<$Refs>
101103
}
102104

103-
/**
104-
* See https://github.com/BigstickCarpet/json-schema-ref-parser/blob/master/docs/options.md
105-
*/
106-
type Options = object & {
107-
105+
namespace $RefParser{
108106
/**
109-
* The `parse` options determine how different types of files will be parsed.
110-
*
111-
* JSON Schema `$Ref` Parser comes with built-in JSON, YAML, plain-text, and binary parsers, any of which you can configure or disable. You can also add your own custom parsers if you want.
107+
* See https://github.com/BigstickCarpet/json-schema-ref-parser/blob/master/docs/options.md
112108
*/
113-
parse?: {
114-
json?: ParserOptions | boolean
115-
yaml?: ParserOptions | boolean
116-
text?: (ParserOptions & { encoding?: string }) | boolean
117-
}
109+
export type Options = object & {
118110

119-
/**
120-
* The `resolve` options control how JSON Schema $Ref Parser will resolve file paths and URLs, and how those files will be read/downloaded.
121-
*
122-
* JSON Schema `$Ref` Parser comes with built-in support for HTTP and HTTPS, as well as support for local files (when running in Node.js). You can configure or disable either of these built-in resolvers. You can also add your own custom resolvers if you want.
123-
*/
124-
resolve?: {
111+
/**
112+
* The `parse` options determine how different types of files will be parsed.
113+
*
114+
* JSON Schema `$Ref` Parser comes with built-in JSON, YAML, plain-text, and binary parsers, any of which you can configure or disable. You can also add your own custom parsers if you want.
115+
*/
116+
parse?: {
117+
json?: ParserOptions | boolean
118+
yaml?: ParserOptions | boolean
119+
text?: (ParserOptions & { encoding?: string }) | boolean
120+
}
125121

126122
/**
127-
* Determines whether external $ref pointers will be resolved. If this option is disabled, then external `$ref` pointers will simply be ignored.
123+
* The `resolve` options control how JSON Schema $Ref Parser will resolve file paths and URLs, and how those files will be read/downloaded.
124+
*
125+
* JSON Schema `$Ref` Parser comes with built-in support for HTTP and HTTPS, as well as support for local files (when running in Node.js). You can configure or disable either of these built-in resolvers. You can also add your own custom resolvers if you want.
128126
*/
129-
external?: boolean
130-
file?: ResolverOptions | boolean
131-
http?: HTTPResolverOptions | boolean
132-
}
127+
resolve?: {
133128

134-
/**
135-
* The `dereference` options control how JSON Schema `$Ref` Parser will dereference `$ref` pointers within the JSON schema.
136-
*/
137-
dereference?: {
129+
/**
130+
* Determines whether external $ref pointers will be resolved. If this option is disabled, then external `$ref` pointers will simply be ignored.
131+
*/
132+
external?: boolean
133+
file?: ResolverOptions | boolean
134+
http?: HTTPResolverOptions | boolean
135+
}
138136

139137
/**
140-
* Determines whether circular `$ref` pointers are handled.
141-
*
142-
* If set to `false`, then a `ReferenceError` will be thrown if the schema contains any circular references.
143-
*
144-
* If set to `"ignore"`, then circular references will simply be ignored. No error will be thrown, but the `$Refs.circular` property will still be set to `true`.
138+
* The `dereference` options control how JSON Schema `$Ref` Parser will dereference `$ref` pointers within the JSON schema.
145139
*/
146-
circular?: boolean | 'ignore'
140+
dereference?: {
141+
142+
/**
143+
* Determines whether circular `$ref` pointers are handled.
144+
*
145+
* If set to `false`, then a `ReferenceError` will be thrown if the schema contains any circular references.
146+
*
147+
* If set to `"ignore"`, then circular references will simply be ignored. No error will be thrown, but the `$Refs.circular` property will still be set to `true`.
148+
*/
149+
circular?: boolean | 'ignore'
150+
}
147151
}
148152
}
149153

0 commit comments

Comments
 (0)