z-schema 7.0.0 → 7.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/README.md +7 -11
  2. package/bin/z-schema +0 -0
  3. package/cjs/index.d.ts +192 -117
  4. package/cjs/index.js +949 -998
  5. package/{src/FormatValidators.ts → dist/format-validators.js} +97 -65
  6. package/dist/index.js +1 -1
  7. package/dist/json-schema.js +40 -0
  8. package/dist/{JsonValidation.js → json-validation.js} +75 -69
  9. package/dist/{Report.js → report.js} +35 -45
  10. package/dist/schema-cache.js +109 -0
  11. package/dist/schema-compiler.js +255 -0
  12. package/dist/{SchemaValidation.js → schema-validator.js} +153 -149
  13. package/dist/types/{Errors.d.ts → errors.d.ts} +2 -0
  14. package/dist/types/format-validators.d.ts +10 -0
  15. package/dist/types/index.d.ts +10 -1
  16. package/dist/types/json-schema.d.ts +50 -0
  17. package/dist/types/json-validation.d.ts +7 -0
  18. package/dist/types/{Report.d.ts → report.d.ts} +22 -23
  19. package/dist/types/schema-cache.d.ts +17 -0
  20. package/dist/types/schema-compiler.d.ts +16 -0
  21. package/dist/types/schema-validator.d.ts +10 -0
  22. package/dist/types/utils/array.d.ts +2 -0
  23. package/dist/types/utils/clone.d.ts +2 -0
  24. package/dist/types/utils/json.d.ts +7 -0
  25. package/dist/types/utils/symbols.d.ts +2 -0
  26. package/dist/types/utils/unicode.d.ts +14 -0
  27. package/dist/types/utils/uri.d.ts +4 -0
  28. package/dist/types/utils/what-is.d.ts +3 -0
  29. package/dist/types/z-schema.d.ts +75 -0
  30. package/dist/utils/array.js +27 -0
  31. package/dist/utils/clone.js +61 -0
  32. package/dist/utils/json.js +59 -0
  33. package/dist/utils/symbols.js +2 -0
  34. package/dist/utils/unicode.js +45 -0
  35. package/dist/utils/uri.js +15 -0
  36. package/dist/utils/what-is.js +29 -0
  37. package/dist/{ZSchema.js → z-schema.js} +66 -77
  38. package/package.json +8 -4
  39. package/src/{Errors.ts → errors.ts} +4 -0
  40. package/src/format-validators.ts +191 -0
  41. package/src/index.ts +12 -1
  42. package/src/json-schema.ts +97 -0
  43. package/src/{JsonValidation.ts → json-validation.ts} +137 -127
  44. package/src/{Report.ts → report.ts} +60 -70
  45. package/src/schema-cache.ts +122 -0
  46. package/src/schema-compiler.ts +300 -0
  47. package/src/{SchemaValidation.ts → schema-validator.ts} +213 -215
  48. package/src/utils/array.ts +29 -0
  49. package/src/utils/clone.ts +63 -0
  50. package/src/utils/json.ts +74 -0
  51. package/src/utils/symbols.ts +3 -0
  52. package/src/utils/unicode.ts +43 -0
  53. package/src/utils/uri.ts +18 -0
  54. package/src/utils/what-is.ts +46 -0
  55. package/src/{ZSchema.ts → z-schema.ts} +108 -113
  56. package/umd/ZSchema.js +949 -998
  57. package/umd/ZSchema.min.js +1 -1
  58. package/dist/FormatValidators.js +0 -136
  59. package/dist/SchemaCache.js +0 -173
  60. package/dist/SchemaCompilation.js +0 -259
  61. package/dist/Utils.js +0 -266
  62. package/dist/types/FormatValidators.d.ts +0 -12
  63. package/dist/types/JsonValidation.d.ts +0 -37
  64. package/dist/types/SchemaCache.d.ts +0 -26
  65. package/dist/types/SchemaCompilation.d.ts +0 -1
  66. package/dist/types/SchemaValidation.d.ts +0 -6
  67. package/dist/types/Utils.d.ts +0 -64
  68. package/dist/types/ZSchema.d.ts +0 -97
  69. package/src/SchemaCache.ts +0 -189
  70. package/src/SchemaCompilation.ts +0 -293
  71. package/src/Utils.ts +0 -286
  72. /package/dist/{Errors.js → errors.js} +0 -0
  73. /package/dist/schemas/{hyper-schema.json → draft-04-hyper-schema.json} +0 -0
  74. /package/dist/schemas/{schema.json → draft-04-schema.json} +0 -0
  75. /package/src/schemas/{hyper-schema.json → draft-04-hyper-schema.json} +0 -0
  76. /package/src/schemas/{schema.json → draft-04-schema.json} +0 -0
package/dist/Utils.js DELETED
@@ -1,266 +0,0 @@
1
- export const jsonSymbol = Symbol.for('z-schema/json');
2
- export const schemaSymbol = Symbol.for('z-schema/schema');
3
- /**
4
- * @param {object} obj
5
- *
6
- * @returns {string[]}
7
- */
8
- export function sortedKeys(obj) {
9
- return Object.keys(obj).sort();
10
- }
11
- /**
12
- *
13
- * @param {string} uri
14
- *
15
- * @returns {boolean}
16
- */
17
- export function isAbsoluteUri(uri) {
18
- return /^https?:\/\//.test(uri);
19
- }
20
- /**
21
- *
22
- * @param {string} uri
23
- *
24
- * @returns {boolean}
25
- */
26
- export function isRelativeUri(uri) {
27
- // relative URIs that end with a hash sign, issue #56
28
- return /.+#/.test(uri);
29
- }
30
- export function whatIs(what) {
31
- const to = typeof what;
32
- if (to === 'object') {
33
- if (what === null) {
34
- return 'null';
35
- }
36
- if (Array.isArray(what)) {
37
- return 'array';
38
- }
39
- return 'object'; // typeof what === 'object' && what === Object(what) && !Array.isArray(what);
40
- }
41
- if (to === 'number') {
42
- if (Number.isFinite(what)) {
43
- if (what % 1 === 0) {
44
- return 'integer';
45
- }
46
- else {
47
- return 'number';
48
- }
49
- }
50
- if (Number.isNaN(what)) {
51
- return 'not-a-number';
52
- }
53
- return 'unknown-number';
54
- }
55
- return to; // undefined, boolean, string, function
56
- }
57
- /**
58
- *
59
- * @param {*} json1
60
- * @param {*} json2
61
- * @param {*} [options]
62
- *
63
- * @returns {boolean}
64
- */
65
- export function areEqual(json1, json2, options) {
66
- options = options || {};
67
- const caseInsensitiveComparison = options.caseInsensitiveComparison || false;
68
- // http://json-schema.org/latest/json-schema-core.html#rfc.section.3.6
69
- // Two JSON values are said to be equal if and only if:
70
- // both are nulls; or
71
- // both are booleans, and have the same value; or
72
- // both are strings, and have the same value; or
73
- // both are numbers, and have the same mathematical value; or
74
- if (json1 === json2) {
75
- return true;
76
- }
77
- if (caseInsensitiveComparison === true &&
78
- typeof json1 === 'string' &&
79
- typeof json2 === 'string' &&
80
- json1.toUpperCase() === json2.toUpperCase()) {
81
- return true;
82
- }
83
- let i, len;
84
- // both are arrays, and:
85
- if (Array.isArray(json1) && Array.isArray(json2)) {
86
- // have the same number of items; and
87
- if (json1.length !== json2.length) {
88
- return false;
89
- }
90
- // items at the same index are equal according to this definition; or
91
- len = json1.length;
92
- for (i = 0; i < len; i++) {
93
- if (!areEqual(json1[i], json2[i], { caseInsensitiveComparison: caseInsensitiveComparison })) {
94
- return false;
95
- }
96
- }
97
- return true;
98
- }
99
- // both are objects, and:
100
- if (whatIs(json1) === 'object' && whatIs(json2) === 'object') {
101
- // have the same set of property names; and
102
- const keys1 = sortedKeys(json1);
103
- const keys2 = sortedKeys(json2);
104
- if (!areEqual(keys1, keys2, { caseInsensitiveComparison: caseInsensitiveComparison })) {
105
- return false;
106
- }
107
- // values for a same property name are equal according to this definition.
108
- len = keys1.length;
109
- for (i = 0; i < len; i++) {
110
- if (!areEqual(json1[keys1[i]], json2[keys1[i]], { caseInsensitiveComparison: caseInsensitiveComparison })) {
111
- return false;
112
- }
113
- }
114
- return true;
115
- }
116
- return false;
117
- }
118
- /**
119
- *
120
- * @param {*[]} arr
121
- * @param {number[]} [indexes]
122
- *
123
- * @returns {boolean}
124
- */
125
- export function isUniqueArray(arr, indexes) {
126
- let i;
127
- let j;
128
- const l = arr.length;
129
- for (i = 0; i < l; i++) {
130
- for (j = i + 1; j < l; j++) {
131
- if (areEqual(arr[i], arr[j])) {
132
- if (indexes) {
133
- indexes.push(i, j);
134
- }
135
- return false;
136
- }
137
- }
138
- }
139
- return true;
140
- }
141
- /**
142
- *
143
- * @param {*} bigSet
144
- * @param {*} subSet
145
- *
146
- * @returns {*[]}
147
- */
148
- export function difference(bigSet, subSet) {
149
- const arr = [];
150
- let idx = bigSet.length;
151
- while (idx--) {
152
- if (subSet.indexOf(bigSet[idx]) === -1) {
153
- arr.push(bigSet[idx]);
154
- }
155
- }
156
- return arr;
157
- }
158
- // NOT a deep version of clone
159
- export function clone(src) {
160
- if (typeof src === 'undefined') {
161
- return void 0;
162
- }
163
- if (typeof src !== 'object' || src === null) {
164
- return src;
165
- }
166
- let res, idx;
167
- if (Array.isArray(src)) {
168
- res = [];
169
- idx = src.length;
170
- while (idx--) {
171
- res[idx] = src[idx];
172
- }
173
- }
174
- else {
175
- res = {};
176
- const keys = Object.keys(src);
177
- idx = keys.length;
178
- while (idx--) {
179
- const key = keys[idx];
180
- res[key] = src[key];
181
- }
182
- }
183
- return res;
184
- }
185
- export function cloneDeep(src) {
186
- let vidx = 0;
187
- const visited = new Map();
188
- const cloned = [];
189
- function cloneDeepInner(src) {
190
- if (typeof src !== 'object' || src === null) {
191
- return src;
192
- }
193
- let res;
194
- let idx;
195
- const cidx = visited.get(src);
196
- if (cidx !== undefined) {
197
- return cloned[cidx];
198
- }
199
- visited.set(src, vidx++);
200
- if (Array.isArray(src)) {
201
- res = [];
202
- cloned.push(res);
203
- idx = src.length;
204
- while (idx--) {
205
- res[idx] = cloneDeepInner(src[idx]);
206
- }
207
- }
208
- else {
209
- res = {};
210
- cloned.push(res);
211
- const keys = Object.keys(src);
212
- idx = keys.length;
213
- while (idx--) {
214
- const key = keys[idx];
215
- res[key] = cloneDeepInner(src[key]);
216
- }
217
- }
218
- return res;
219
- }
220
- return cloneDeepInner(src);
221
- }
222
- /*
223
- following function comes from punycode.js library
224
- see: https://github.com/bestiejs/punycode.js
225
- */
226
- /**
227
- * Creates an array containing the numeric code points of each Unicode
228
- * character in the string. While JavaScript uses UCS-2 internally,
229
- * this function will convert a pair of surrogate halves (each of which
230
- * UCS-2 exposes as separate characters) into a single code point,
231
- * matching UTF-16.
232
- * @see `punycode.ucs2.encode`
233
- * @see <https://mathiasbynens.be/notes/javascript-encoding>
234
- * @memberOf punycode.ucs2
235
- * @name decode
236
- * @param {String} string The Unicode input string (UCS-2).
237
- * @returns {Array} The new array of code points.
238
- */
239
- export function ucs2decode(string) {
240
- const output = [];
241
- let counter = 0;
242
- const length = string.length;
243
- let value;
244
- let extra;
245
- while (counter < length) {
246
- value = string.charCodeAt(counter++);
247
- if (value >= 0xd800 && value <= 0xdbff && counter < length) {
248
- // high surrogate, and there is a next character
249
- extra = string.charCodeAt(counter++);
250
- if ((extra & 0xfc00) == 0xdc00) {
251
- // low surrogate
252
- output.push(((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000);
253
- }
254
- else {
255
- // unmatched surrogate; only append this code unit, in case the next
256
- // code unit is the high surrogate of a surrogate pair
257
- output.push(value);
258
- counter--;
259
- }
260
- }
261
- else {
262
- output.push(value);
263
- }
264
- }
265
- return output;
266
- }
@@ -1,12 +0,0 @@
1
- export declare const FormatValidators: {
2
- date: (date: any) => boolean;
3
- 'date-time': (dateTime: any) => boolean;
4
- email: (email: any) => any;
5
- hostname: (hostname: any) => boolean;
6
- 'host-name': (hostname: any) => any;
7
- ipv4: (ipv4: any) => any;
8
- ipv6: (ipv6: any) => any;
9
- regex: (str: any) => boolean;
10
- uri: (args_0: string) => any;
11
- 'strict-uri': (uri: any) => any;
12
- };
@@ -1,37 +0,0 @@
1
- export declare const JsonValidators: {
2
- multipleOf: (report: any, schema: any, json: any) => void;
3
- maximum: (report: any, schema: any, json: any) => void;
4
- exclusiveMaximum: () => void;
5
- minimum: (report: any, schema: any, json: any) => void;
6
- exclusiveMinimum: () => void;
7
- maxLength: (report: any, schema: any, json: any) => void;
8
- minLength: (report: any, schema: any, json: any) => void;
9
- pattern: (report: any, schema: any, json: any) => void;
10
- additionalItems: (report: any, schema: any, json: any) => void;
11
- items: () => void;
12
- maxItems: (report: any, schema: any, json: any) => void;
13
- minItems: (report: any, schema: any, json: any) => void;
14
- uniqueItems: (report: any, schema: any, json: any) => void;
15
- maxProperties: (report: any, schema: any, json: any) => void;
16
- minProperties: (report: any, schema: any, json: any) => void;
17
- required: (report: any, schema: any, json: any) => void;
18
- additionalProperties: (report: any, schema: any, json: any) => any;
19
- patternProperties: (report: any, schema: any, json: any) => any;
20
- properties: (report: any, schema: any, json: any) => void;
21
- dependencies: (report: any, schema: any, json: any) => void;
22
- enum: (report: any, schema: any, json: any) => void;
23
- type: (report: any, schema: any, json: any) => void;
24
- allOf: (report: any, schema: any, json: any) => void;
25
- anyOf: (report: any, schema: any, json: any) => void;
26
- oneOf: (report: any, schema: any, json: any) => void;
27
- not: (report: any, schema: any, json: any) => void;
28
- definitions: () => void;
29
- format: (report: any, schema: any, json: any) => void;
30
- };
31
- /**
32
- *
33
- * @param {Report} report
34
- * @param {*} schema
35
- * @param {*} json
36
- */
37
- export declare function validate(report: any, schema: any, json: any): boolean;
@@ -1,26 +0,0 @@
1
- export declare function getRemotePath(uri: any): any;
2
- /**
3
- *
4
- * @param {*} uri
5
- * @param {*} schema
6
- *
7
- * @returns {void}
8
- */
9
- export declare function cacheSchemaByUri(uri: any, schema: any): void;
10
- /**
11
- *
12
- * @param {*} uri
13
- *
14
- * @returns {void}
15
- */
16
- export declare function removeFromCacheByUri(uri: any): void;
17
- /**
18
- *
19
- * @param {*} uri
20
- *
21
- * @returns {boolean}
22
- */
23
- export declare function checkCacheForUri(uri: any): boolean;
24
- export declare function getSchema(report: any, schema: any): any;
25
- export declare function getSchemaByReference(report: any, key: any): any;
26
- export declare function getSchemaByUri(report: any, uri: any, root: any): any;
@@ -1 +0,0 @@
1
- export declare function compileSchema(report: any, schema: any): any;
@@ -1,6 +0,0 @@
1
- /**
2
- *
3
- * @param {Report} report
4
- * @param {*} schema
5
- */
6
- export declare function validateSchema(report: any, schema: any): any;
@@ -1,64 +0,0 @@
1
- export declare const jsonSymbol: unique symbol;
2
- export declare const schemaSymbol: unique symbol;
3
- /**
4
- * @param {object} obj
5
- *
6
- * @returns {string[]}
7
- */
8
- export declare function sortedKeys(obj: any): string[];
9
- /**
10
- *
11
- * @param {string} uri
12
- *
13
- * @returns {boolean}
14
- */
15
- export declare function isAbsoluteUri(uri: any): boolean;
16
- /**
17
- *
18
- * @param {string} uri
19
- *
20
- * @returns {boolean}
21
- */
22
- export declare function isRelativeUri(uri: any): boolean;
23
- export declare function whatIs(what: any): "string" | "bigint" | "boolean" | "symbol" | "undefined" | "function" | "object" | "null" | "array" | "integer" | "number" | "not-a-number" | "unknown-number";
24
- /**
25
- *
26
- * @param {*} json1
27
- * @param {*} json2
28
- * @param {*} [options]
29
- *
30
- * @returns {boolean}
31
- */
32
- export declare function areEqual(json1: any, json2: any, options?: any): boolean;
33
- /**
34
- *
35
- * @param {*[]} arr
36
- * @param {number[]} [indexes]
37
- *
38
- * @returns {boolean}
39
- */
40
- export declare function isUniqueArray(arr: any, indexes?: any): boolean;
41
- /**
42
- *
43
- * @param {*} bigSet
44
- * @param {*} subSet
45
- *
46
- * @returns {*[]}
47
- */
48
- export declare function difference(bigSet: any, subSet: any): any[];
49
- export declare function clone(src: any): any;
50
- export declare function cloneDeep(src: any): any;
51
- /**
52
- * Creates an array containing the numeric code points of each Unicode
53
- * character in the string. While JavaScript uses UCS-2 internally,
54
- * this function will convert a pair of surrogate halves (each of which
55
- * UCS-2 exposes as separate characters) into a single code point,
56
- * matching UTF-16.
57
- * @see `punycode.ucs2.encode`
58
- * @see <https://mathiasbynens.be/notes/javascript-encoding>
59
- * @memberOf punycode.ucs2
60
- * @name decode
61
- * @param {String} string The Unicode input string (UCS-2).
62
- * @returns {Array} The new array of code points.
63
- */
64
- export declare function ucs2decode(string: any): any[];
@@ -1,97 +0,0 @@
1
- import { Report, SchemaError, SchemaErrorDetail } from './Report.js';
2
- import type { Errors } from './Errors.js';
3
- export interface ZSchemaOptions {
4
- asyncTimeout?: number;
5
- forceAdditional?: boolean;
6
- assumeAdditional?: boolean;
7
- forceItems?: boolean;
8
- forceMinItems?: boolean;
9
- forceMaxItems?: boolean;
10
- forceMinLength?: boolean;
11
- forceMaxLength?: boolean;
12
- forceProperties?: boolean;
13
- ignoreUnresolvableReferences?: boolean;
14
- noExtraKeywords?: boolean;
15
- noTypeless?: boolean;
16
- noEmptyStrings?: boolean;
17
- noEmptyArrays?: boolean;
18
- strictUris?: boolean;
19
- strictMode?: boolean;
20
- reportPathAsArray?: boolean;
21
- breakOnFirstError?: boolean;
22
- pedanticCheck?: boolean;
23
- ignoreUnknownFormats?: boolean;
24
- customValidator?: (report: Report, schema: unknown, json: unknown) => void;
25
- }
26
- export interface ValidateOptions {
27
- schemaPath?: string;
28
- includeErrors?: Array<keyof typeof Errors>;
29
- }
30
- type ValidateCallback = (e: Error, valid: boolean) => void;
31
- type SchemaReader = (uri: string) => unknown;
32
- export declare class ZSchema {
33
- lastReport: Report | undefined;
34
- /**
35
- * Register a custom format.
36
- *
37
- * @param name - name of the custom format
38
- * @param validatorFunction - custom format validator function.
39
- * Returns `true` if `value` matches the custom format.
40
- */
41
- static registerFormat(formatName: string, validatorFunction: (value: unknown) => boolean): void;
42
- /**
43
- * Unregister a format.
44
- *
45
- * @param name - name of the custom format
46
- */
47
- static unregisterFormat(name: string): void;
48
- /**
49
- * Get the list of all registered formats.
50
- *
51
- * Both the names of the burned-in formats and the custom format names are
52
- * returned by this function.
53
- *
54
- * @returns {string[]} the list of all registered format names.
55
- */
56
- static getRegisteredFormats(): string[];
57
- static getDefaultOptions(): ZSchemaOptions;
58
- private cache;
59
- private referenceCache;
60
- private validateOptions;
61
- options: ZSchemaOptions;
62
- constructor(options?: ZSchemaOptions);
63
- /**
64
- * @param schema - JSON object representing schema
65
- * @returns {boolean} true if schema is valid.
66
- */
67
- validateSchema(schema: unknown): boolean;
68
- /**
69
- * @param json - either a JSON string or a parsed JSON object
70
- * @param schema - the JSON object representing the schema
71
- * @returns true if json matches schema
72
- */
73
- validate(json: any, schema: any, options?: ValidateOptions, callback?: ValidateCallback): boolean;
74
- validate(json: any, schema: any, callback?: any): boolean;
75
- validate(json: any, schema: any): boolean;
76
- /**
77
- * Returns an Error object for the most recent failed validation, or null if the validation was successful.
78
- */
79
- getLastError(): SchemaError;
80
- /**
81
- * Returns the error details for the most recent validation, or undefined if the validation was successful.
82
- * This is the same list as the SchemaError.details property.
83
- */
84
- getLastErrors(): SchemaErrorDetail[];
85
- setRemoteReference(uri: any, schema: any, validationOptions: any): void;
86
- compileSchema(schema: any): boolean;
87
- getMissingReferences(arr?: any): any[];
88
- getMissingRemoteReferences(): any[];
89
- getResolvedSchema(schema: any): any;
90
- static schemaReader: SchemaReader;
91
- setSchemaReader(schemaReader: any): void;
92
- getSchemaReader(): SchemaReader;
93
- static setSchemaReader(schemaReader: any): void;
94
- static schemaSymbol: symbol;
95
- static jsonSymbol: symbol;
96
- }
97
- export {};
@@ -1,189 +0,0 @@
1
- import isequal from 'lodash.isequal';
2
- import { Report } from './Report.js';
3
- import { compileSchema } from './SchemaCompilation.js';
4
- import * as SchemaValidation from './SchemaValidation.js';
5
- import * as Utils from './Utils.js';
6
-
7
- function decodeJSONPointer(str) {
8
- // http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-07#section-3
9
- return decodeURIComponent(str).replace(/~[0-1]/g, function (x) {
10
- return x === '~1' ? '/' : '~';
11
- });
12
- }
13
-
14
- export function getRemotePath(uri) {
15
- const io = uri.indexOf('#');
16
- return io === -1 ? uri : uri.slice(0, io);
17
- }
18
-
19
- function getQueryPath(uri) {
20
- const io = uri.indexOf('#');
21
- const res = io === -1 ? undefined : uri.slice(io + 1);
22
- // WARN: do not slice slash, #/ means take root and go down from it
23
- // if (res && res[0] === "/") { res = res.slice(1); }
24
- return res;
25
- }
26
-
27
- function findId(schema, id) {
28
- // process only arrays and objects
29
- if (typeof schema !== 'object' || schema === null) {
30
- return;
31
- }
32
-
33
- // no id means root so return itself
34
- if (!id) {
35
- return schema;
36
- }
37
-
38
- if (schema.id) {
39
- if (schema.id === id || (schema.id[0] === '#' && schema.id.substring(1) === id)) {
40
- return schema;
41
- }
42
- }
43
-
44
- let idx, result;
45
- if (Array.isArray(schema)) {
46
- idx = schema.length;
47
- while (idx--) {
48
- result = findId(schema[idx], id);
49
- if (result) {
50
- return result;
51
- }
52
- }
53
- } else {
54
- const keys = Object.keys(schema);
55
- idx = keys.length;
56
- while (idx--) {
57
- const k = keys[idx];
58
- if (k.indexOf('__$') === 0) {
59
- continue;
60
- }
61
- result = findId(schema[k], id);
62
- if (result) {
63
- return result;
64
- }
65
- }
66
- }
67
- }
68
-
69
- /**
70
- *
71
- * @param {*} uri
72
- * @param {*} schema
73
- *
74
- * @returns {void}
75
- */
76
- export function cacheSchemaByUri(uri, schema) {
77
- const remotePath = getRemotePath(uri);
78
- if (remotePath) {
79
- this.cache[remotePath] = schema;
80
- }
81
- }
82
-
83
- /**
84
- *
85
- * @param {*} uri
86
- *
87
- * @returns {void}
88
- */
89
- export function removeFromCacheByUri(uri) {
90
- const remotePath = getRemotePath(uri);
91
- if (remotePath) {
92
- delete this.cache[remotePath];
93
- }
94
- }
95
-
96
- /**
97
- *
98
- * @param {*} uri
99
- *
100
- * @returns {boolean}
101
- */
102
- export function checkCacheForUri(uri) {
103
- const remotePath = getRemotePath(uri);
104
- return remotePath ? this.cache[remotePath] != null : false;
105
- }
106
-
107
- export function getSchema(report, schema) {
108
- if (typeof schema === 'object') {
109
- schema = getSchemaByReference.call(this, report, schema);
110
- }
111
- if (typeof schema === 'string') {
112
- schema = getSchemaByUri.call(this, report, schema);
113
- }
114
- return schema;
115
- }
116
-
117
- export function getSchemaByReference(report, key) {
118
- let i = this.referenceCache.length;
119
- while (i--) {
120
- if (isequal(this.referenceCache[i][0], key)) {
121
- return this.referenceCache[i][1];
122
- }
123
- }
124
- // not found
125
- const schema = Utils.cloneDeep(key);
126
- this.referenceCache.push([key, schema]);
127
- return schema;
128
- }
129
-
130
- export function getSchemaByUri(report, uri, root) {
131
- const remotePath = getRemotePath(uri);
132
- const queryPath = getQueryPath(uri);
133
- let result = remotePath ? this.cache[remotePath] : root;
134
-
135
- if (result && remotePath) {
136
- // we need to avoid compiling schemas in a recursive loop
137
- const compileRemote = result !== root;
138
- // now we need to compile and validate resolved schema (in case it's not already)
139
- if (compileRemote) {
140
- report.path.push(remotePath);
141
-
142
- let remoteReport;
143
-
144
- const anscestorReport = report.getAncestor(result.id);
145
- if (anscestorReport) {
146
- remoteReport = anscestorReport;
147
- } else {
148
- remoteReport = new Report(report);
149
- if (compileSchema.call(this, remoteReport, result)) {
150
- const savedOptions = this.options;
151
- try {
152
- // If custom validationOptions were provided to setRemoteReference(),
153
- // use them instead of the default options
154
- this.options = result.__$validationOptions || this.options;
155
- SchemaValidation.validateSchema.call(this, remoteReport, result);
156
- } finally {
157
- this.options = savedOptions;
158
- }
159
- }
160
- }
161
- const remoteReportIsValid = remoteReport.isValid();
162
- if (!remoteReportIsValid) {
163
- report.addError('REMOTE_NOT_VALID', [uri], remoteReport);
164
- }
165
-
166
- report.path.pop();
167
-
168
- if (!remoteReportIsValid) {
169
- return undefined;
170
- }
171
- }
172
- }
173
-
174
- if (result && queryPath) {
175
- const parts = queryPath.split('/');
176
- for (let idx = 0, lim = parts.length; result && idx < lim; idx++) {
177
- const key = decodeJSONPointer(parts[idx]);
178
- if (idx === 0) {
179
- // it's an id
180
- result = findId(result, key);
181
- } else {
182
- // it's a path behind id
183
- result = result[key];
184
- }
185
- }
186
- }
187
-
188
- return result;
189
- }