1 | "use strict";
|
---|
2 |
|
---|
3 | var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
---|
4 | var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard").default;
|
---|
5 | exports.__esModule = true;
|
---|
6 | exports.evaluate = exports.JsonSchemaUriError = void 0;
|
---|
7 | var _ramdaAdjunct = require("ramda-adjunct");
|
---|
8 | var _apidomCore = require("@swagger-api/apidom-core");
|
---|
9 | var _apidomNsOpenapi = require("@swagger-api/apidom-ns-openapi-3-1");
|
---|
10 | var _apidomJsonPointer = require("@swagger-api/apidom-json-pointer");
|
---|
11 | var url = _interopRequireWildcard(require("../../../../util/url.cjs"));
|
---|
12 | var _EvaluationJsonSchemaUriError = _interopRequireDefault(require("../../../../errors/EvaluationJsonSchemaUriError.cjs"));
|
---|
13 | exports.EvaluationJsonSchemaUriError = _EvaluationJsonSchemaUriError.default;
|
---|
14 | var _$anchor = require("./$anchor.cjs");
|
---|
15 | var _util = require("../../../../resolve/strategies/openapi-3-1/util.cjs");
|
---|
16 | var _JsonSchemaUriError = _interopRequireDefault(require("../../../../errors/JsonSchemaUriError.cjs"));
|
---|
17 | exports.JsonSchemaUriError = _JsonSchemaUriError.default;
|
---|
18 | /**
|
---|
19 | * Evaluates JSON Schema $ref containing unknown URI against ApiDOM fragment.
|
---|
20 | */
|
---|
21 | const evaluate = (uri, element) => {
|
---|
22 | const {
|
---|
23 | cache
|
---|
24 | } = evaluate;
|
---|
25 | const uriStrippedHash = url.stripHash(uri);
|
---|
26 | const isSchemaElementWith$id = e => (0, _apidomNsOpenapi.isSchemaElement)(e) && typeof e.$id !== 'undefined';
|
---|
27 |
|
---|
28 | // warm the cache
|
---|
29 | if (!cache.has(element)) {
|
---|
30 | const schemaObjectElements = (0, _apidomCore.filter)(isSchemaElementWith$id, element);
|
---|
31 | cache.set(element, Array.from(schemaObjectElements));
|
---|
32 | }
|
---|
33 |
|
---|
34 | // search for the matching schema
|
---|
35 | const result = cache.get(element).find(e => {
|
---|
36 | const $idBaseURI = (0, _util.resolveSchema$idField)(uriStrippedHash, e);
|
---|
37 | return $idBaseURI === uriStrippedHash;
|
---|
38 | });
|
---|
39 | if ((0, _ramdaAdjunct.isUndefined)(result)) {
|
---|
40 | throw new _EvaluationJsonSchemaUriError.default(`Evaluation failed on URI: "${uri}"`);
|
---|
41 | }
|
---|
42 | let fragmentEvaluate;
|
---|
43 | let selector;
|
---|
44 | if ((0, _$anchor.isAnchor)((0, _$anchor.uriToAnchor)(uri))) {
|
---|
45 | // we're dealing with JSON Schema $anchor here
|
---|
46 | fragmentEvaluate = _$anchor.evaluate;
|
---|
47 | selector = (0, _$anchor.uriToAnchor)(uri);
|
---|
48 | } else {
|
---|
49 | // we're assuming here that we're dealing with JSON Pointer here
|
---|
50 | fragmentEvaluate = _apidomJsonPointer.evaluate;
|
---|
51 | selector = (0, _apidomJsonPointer.uriToPointer)(uri);
|
---|
52 | }
|
---|
53 |
|
---|
54 | // @ts-ignore
|
---|
55 | return fragmentEvaluate(selector, result);
|
---|
56 | };
|
---|
57 | exports.evaluate = evaluate;
|
---|
58 | evaluate.cache = new WeakMap(); |
---|