UNPKG

17.7 kBTypeScriptView Raw
1import { JSONSchema4, JSONSchema4Type, JSONSchema6, JSONSchema6Type } from 'json-schema';
2
3export = $RefParser
4
5/**
6 * 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.
7 *
8 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/ref-parser.md
9 */
10declare class $RefParser {
11
12 /**
13 * The `schema` property is the parsed/bundled/dereferenced JSON Schema object. This is the same value that is passed to the callback function (or Promise) when calling the parse, bundle, or dereference methods.
14 *
15 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/ref-parser.md#schema
16 */
17 schema: JSONSchema4 | JSONSchema6
18
19 /**
20 * The $refs property is a `$Refs` object, which lets you access all of the externally-referenced files in the schema, as well as easily get and set specific values in the schema using JSON pointers.
21 *
22 * This is the same value that is passed to the callback function (or Promise) when calling the `resolve` method.
23 *
24 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/ref-parser.md#refs
25 */
26 $refs: $RefParser.$Refs
27
28 /**
29 * Dereferences all `$ref` pointers in the JSON Schema, replacing each reference with its resolved value. This results in a schema object that does not contain any `$ref` pointers. Instead, it's a normal JavaScript object tree that can easily be crawled and used just like any other JavaScript object. This is great for programmatic usage, especially when using tools that don't understand JSON references.
30 *
31 * The dereference method maintains object reference equality, meaning that all `$ref` pointers that point to the same object will be replaced with references to the same object. Again, this is great for programmatic usage, but it does introduce the risk of circular references, so be careful if you intend to serialize the schema using `JSON.stringify()`. Consider using the bundle method instead, which does not create circular references.
32 *
33 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/ref-parser.md#dereferenceschema-options-callback
34 *
35 * @param schema A JSON Schema object, or the file path or URL of a JSON Schema file. See the `parse` method for more info.
36 * @param options (optional)
37 * @param callback (optional) A callback that will receive the dereferenced schema object
38 */
39 dereference(path: string, schema: string | JSONSchema4 | JSONSchema6, options?: $RefParser.Options, callback?: (err: Error | null, schema: JSONSchema4 | JSONSchema6 | null) => any): Promise<JSONSchema4 | JSONSchema6>
40 dereference(path: string, options?: $RefParser.Options, callback?: (err: Error | null, schema: JSONSchema4 | JSONSchema6 | null) => any): Promise<JSONSchema4 | JSONSchema6>
41 dereference(schema: JSONSchema4 | JSONSchema6, options?: $RefParser.Options, callback?: (err: Error | null, schema: JSONSchema4 | JSONSchema6 | null) => any): Promise<JSONSchema4 | JSONSchema6>
42
43 /**
44 * Bundles all referenced files/URLs into a single schema that only has internal `$ref` pointers. This lets you split-up your schema however you want while you're building it, but easily combine all those files together when it's time to package or distribute the schema to other people. The resulting schema size will be small, since it will still contain internal JSON references rather than being fully-dereferenced.
45 *
46 * This also eliminates the risk of circular references, so the schema can be safely serialized using `JSON.stringify()`.
47 *
48 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/ref-parser.md#bundleschema-options-callback
49 *
50 * @param schema A JSON Schema object, or the file path or URL of a JSON Schema file. See the `parse` method for more info.
51 * @param options (optional)
52 * @param callback (optional) A callback that will receive the bundled schema object
53 */
54 bundle(
55 schema: string | JSONSchema4 | JSONSchema6,
56 options?: $RefParser.Options,
57 callback?: (err: Error | null, schema: JSONSchema4 | JSONSchema6 | null) => any
58 ): Promise<JSONSchema4 | JSONSchema6>
59
60 /**
61 * *This method is used internally by other methods, such as `bundle` and `dereference`. You probably won't need to call this method yourself.*
62 *
63 * Parses the given JSON Schema file (in JSON or YAML format), and returns it as a JavaScript object. This method `does not` resolve `$ref` pointers or dereference anything. It simply parses one file and returns it.
64 *
65 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/ref-parser.md#parseschema-options-callback
66 *
67 * @param schema A JSON Schema object, or the file path or URL of a JSON Schema file. The path can be absolute or relative. In Node, the path is relative to `process.cwd()`. In the browser, it's relative to the URL of the page.
68 * @param options (optional)
69 * @param callback (optional) A callback that will receive the parsed schema object, or an error
70 */
71 parse(
72 schema: string | JSONSchema4 | JSONSchema6,
73 options?: $RefParser.Options,
74 callback?: (err: Error | null, schema: JSONSchema4 | JSONSchema6 | null) => any
75 ): Promise<JSONSchema4 | JSONSchema6>
76
77 /**
78 * *This method is used internally by other methods, such as `bundle` and `dereference`. You probably won't need to call this method yourself.*
79 *
80 * Resolves all JSON references (`$ref` pointers) in the given JSON Schema file. If it references any other files/URLs, then they will be downloaded and resolved as well. This method **does not** dereference anything. It simply gives you a `$Refs` object, which is a map of all the resolved references and their values.
81 *
82 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/ref-parser.md#resolveschema-options-callback
83 *
84 * @param schema A JSON Schema object, or the file path or URL of a JSON Schema file. See the `parse` method for more info.
85 * @param options (optional)
86 * @param callback (optional) A callback that will receive a `$Refs` object
87 */
88 resolve(
89 schema: string | JSONSchema4 | JSONSchema6,
90 options?: $RefParser.Options,
91 callback?: (err: Error | null, $refs: $RefParser.$Refs | null) => any
92 ): Promise<$RefParser.$Refs>
93}
94
95declare namespace $RefParser {
96
97 /**
98 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/options.md
99 */
100 export type Options = object & {
101
102 /**
103 * The `parse` options determine how different types of files will be parsed.
104 *
105 * 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.
106 */
107 parse?: {
108 json?: ParserOptions | boolean
109 yaml?: ParserOptions | boolean
110 text?: (ParserOptions & { encoding?: string }) | boolean
111 }
112
113 /**
114 * The `resolve` options control how JSON Schema $Ref Parser will resolve file paths and URLs, and how those files will be read/downloaded.
115 *
116 * 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.
117 */
118 resolve?: {
119
120 /**
121 * Determines whether external $ref pointers will be resolved. If this option is disabled, then external `$ref` pointers will simply be ignored.
122 */
123 external?: boolean
124 file?: Partial<ResolverOptions> | boolean
125 http?: HTTPResolverOptions | boolean
126 }
127
128 /**
129 * The `dereference` options control how JSON Schema `$Ref` Parser will dereference `$ref` pointers within the JSON schema.
130 */
131 dereference?: {
132
133 /**
134 * Determines whether circular `$ref` pointers are handled.
135 *
136 * If set to `false`, then a `ReferenceError` will be thrown if the schema contains any circular references.
137 *
138 * 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`.
139 */
140 circular?: boolean | 'ignore'
141 }
142 }
143
144 export interface HTTPResolverOptions extends Partial<ResolverOptions> {
145
146 /**
147 * You can specify any HTTP headers that should be sent when downloading files. For example, some servers may require you to set the `Accept` or `Referrer` header.
148 */
149 headers?: object
150
151 /**
152 * The amount of time (in milliseconds) to wait for a response from the server when downloading files. The default is 5 seconds.
153 */
154 timeout?: number
155
156 /**
157 * The maximum number of HTTP redirects to follow per file. The default is 5. To disable automatic following of redirects, set this to zero.
158 */
159 redirects?: number
160
161 /**
162 * Set this to `true` if you're downloading files from a CORS-enabled server that requires authentication
163 */
164 withCredentials?: boolean
165 }
166
167 /**
168 * JSON Schema `$Ref` Parser comes with built-in resolvers for HTTP and HTTPS URLs, as well as local filesystem paths (when running in Node.js). You can add your own custom resolvers to support additional protocols, or even replace any of the built-in resolvers with your own custom implementation.
169 *
170 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/plugins/resolvers.md
171 */
172 export interface ResolverOptions {
173
174 /**
175 * All resolvers have an order property, even the built-in resolvers. If you don't specify an order property, then your resolver will run last. Specifying `order: 1`, like we did in this example, will make your resolver run first. Or you can squeeze your resolver in-between some of the built-in resolvers. For example, `order: 101` would make it run after the file resolver, but before the HTTP resolver. You can see the order of all the built-in resolvers by looking at their source code.
176 *
177 * The order property and canRead property are related to each other. For each file that JSON Schema $Ref Parser needs to resolve, it first determines which resolvers can read that file by checking their canRead property. If only one resolver matches a file, then only that one resolver is called, regardless of its order. If multiple resolvers match a file, then those resolvers are tried in order until one of them successfully reads the file. Once a resolver successfully reads the file, the rest of the resolvers are skipped.
178 */
179 order?: number
180
181 /**
182 * The `canRead` property tells JSON Schema `$Ref` Parser what kind of files your resolver can read. In this example, we've simply specified a regular expression that matches "mogodb://" URLs, but we could have used a simple boolean, or even a function with custom logic to determine which files to resolve. Here are examples of each approach:
183 */
184 canRead: boolean | RegExp | string | string[] | ((file: FileInfo) => boolean)
185
186 /**
187 * This is where the real work of a resolver happens. The `read` method accepts the same file info object as the `canRead` function, but rather than returning a boolean value, the `read` method should return the contents of the file. The file contents should be returned in as raw a form as possible, such as a string or a byte array. Any further parsing or processing should be done by parsers.
188 *
189 * Unlike the `canRead` function, the `read` method can also be asynchronous. This might be important if your resolver needs to read data from a database or some other external source. You can return your asynchronous value using either an ES6 Promise or a Node.js-style error-first callback. Of course, if your resolver has the ability to return its data synchronously, then that's fine too. Here are examples of all three approaches:
190 */
191 read(
192 file: FileInfo,
193 callback?: (error: Error | null, data: string | null) => any
194 ): string | Buffer | Promise<string | Buffer>
195 }
196
197 export interface ParserOptions {
198
199 /**
200 * Parsers run in a specific order, relative to other parsers. For example, a parser with `order: 5` will run before a parser with `order: 10`. If a parser is unable to successfully parse a file, then the next parser is tried, until one succeeds or they all fail.
201 *
202 * You can change the order in which parsers run, which is useful if you know that most of your referenced files will be a certain type, or if you add your own custom parser that you want to run first.
203 */
204 order?: number
205
206 /**
207 * All of the built-in parsers allow empty files by default. The JSON and YAML parsers will parse empty files as `undefined`. The text parser will parse empty files as an empty string. The binary parser will parse empty files as an empty byte array.
208 *
209 * You can set `allowEmpty: false` on any parser, which will cause an error to be thrown if a file empty.
210 */
211 allowEmpty?: boolean
212
213 /**
214 * Determines which parsers will be used for which files.
215 *
216 * A regular expression can be used to match files by their full path. A string (or array of strings) can be used to match files by their file extension. Or a function can be used to perform more complex matching logic. See the custom parser docs for details.
217 */
218 canParse?: boolean | RegExp | string | string[] | ((file: FileInfo) => boolean)
219 }
220
221 /**
222 * JSON Schema `$Ref` Parser supports plug-ins, such as resolvers and parsers. These plug-ins can have methods such as `canRead()`, `read()`, `canParse()`, and `parse()`. All of these methods accept the same object as their parameter: an object containing information about the file being read or parsed.
223 *
224 * The file info object currently only consists of a few properties, but it may grow in the future if plug-ins end up needing more information.
225 *
226 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/plugins/file-info-object.md
227 */
228 export interface FileInfo {
229
230 /**
231 * The full URL of the file. This could be any type of URL, including "http://", "https://", "file://", "ftp://", "mongodb://", or even a local filesystem path (when running in Node.js).
232 */
233 url: string
234
235 /**
236 * The lowercase file extension, such as ".json", ".yaml", ".txt", etc.
237 */
238 extension: string
239
240 /**
241 * The raw file contents, in whatever form they were returned by the resolver that read the file.
242 */
243 data: string | Buffer
244 }
245
246 /**
247 * When you call the resolve method, the value that gets passed to the callback function (or Promise) is a $Refs object. This same object is accessible via the parser.$refs property of $RefParser objects.
248 *
249 * This object is a map of JSON References and their resolved values. It also has several convenient helper methods that make it easy for you to navigate and manipulate the JSON References.
250 *
251 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/refs.md
252 */
253 export class $Refs {
254 /**
255 * This property is true if the schema contains any circular references. You may want to check this property before serializing the dereferenced schema as JSON, since JSON.stringify() does not support circular references by default.
256 *
257 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/refs.md#circular
258 */
259 circular: boolean
260
261 /**
262 * Returns the paths/URLs of all the files in your schema (including the main schema file).
263 *
264 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/refs.md#pathstypes
265 *
266 * @param types (optional) Optionally only return certain types of paths ("file", "http", etc.)
267 */
268 paths(...types: string[]): string[]
269
270 /**
271 * Returns a map of paths/URLs and their correspond values.
272 *
273 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/refs.md#valuestypes
274 *
275 * @param types (optional) Optionally only return values from certain locations ("file", "http", etc.)
276 */
277 values(...types: string[]): { [url: string]: JSONSchema4 | JSONSchema6 }
278
279 /**
280 * Returns `true` if the given path exists in the schema; otherwise, returns `false`
281 *
282 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/refs.md#existsref
283 *
284 * @param $ref The JSON Reference path, optionally with a JSON Pointer in the hash
285 */
286 exists($ref: string): boolean
287
288 /**
289 * Gets the value at the given path in the schema. Throws an error if the path does not exist.
290 *
291 * See https://github.com/APIDevTools/json-schema-ref-parser/blob/master/docs/refs.md#getref
292 *
293 * @param $ref The JSON Reference path, optionally with a JSON Pointer in the hash
294 */
295 get($ref: string): JSONSchema4Type | JSONSchema6Type
296
297 /**
298 * Sets the value at the given path in the schema. If the property, or any of its parents, don't exist, they will be created.
299 *
300 * @param $ref The JSON Reference path, optionally with a JSON Pointer in the hash
301 * @param value The value to assign. Can be anything (object, string, number, etc.)
302 */
303 set($ref: string, value: JSONSchema4Type | JSONSchema6Type): void
304 }
305
306}
307
\No newline at end of file