-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference.ts
101 lines (86 loc) · 2.51 KB
/
reference.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { File } from "./file";
import { JsonSchema } from "./json-schema";
import { Pointer } from "./pointer";
import { Resolution } from "./resolution";
import { Resource } from "./resource";
import { createURL } from "./url";
import { createError } from "./utils";
import { JsonSchemaVersionNumber } from "./versions";
const code = "ERR_INVALID_REF";
/**
* The arguments that can be passed to the `Reference` constructor
*/
export interface ReferenceArgs {
resource: Resource;
locationInFile: string[];
value: string | unknown;
data: object;
}
/**
* A JSON reference
*
* @see https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03
* @see http://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.8.2.4
*/
export class Reference {
/**
* The JSON Schema that this reference is part of
*/
public schema: JsonSchema;
/**
* The file that contains this reference
*/
public file: File;
/**
* The JSON Schema resource that contains this reference
*/
public resource: Resource;
/**
* A pointer to the reference's location in the file
*/
public locationInFile: Pointer;
/**
* The `$ref` value (e.g. "some-file.json#/Foo/properties/bar")
*/
public value: string;
/**
* The reference object in the JSON Schema
*/
public data: object;
/**
* The absolute URI that this reference points to
*/
public targetURI: URL;
public constructor(args: ReferenceArgs) {
let input = args.value;
if (typeof input !== "string") {
let type = typeof input;
throw createError(TypeError, `$ref must be a string, not ${type}.`, { code, input, type });
}
if (input.length === 0) {
throw createError(SyntaxError, "$ref cannot be empty.", { code, input });
}
this.schema = args.resource.schema;
this.file = args.resource.file;
this.resource = args.resource;
this.locationInFile = new Pointer(args.locationInFile);
this.value = input;
this.data = args.data;
this.targetURI = createURL(input, this.resource.uri);
}
/**
* Resolve the reference to a value in the schema.
*
* @param versionNumber - The JSON Schema version to use. Defaults to auto-detecting.
* @returns The resolved value, along with information about how it was resolved
*/
public resolve(versionNumber?: JsonSchemaVersionNumber): Resolution {
return this.schema.resolve(this.targetURI, versionNumber);
}
/**
* Returns the reference's target URI
*/
public toString(): string {
return `$ref: ${this.targetURI}`;
}
}