2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.getSchemaRefs = exports.resolveUrl = exports.normalizeId = exports._getFullPath = exports.getFullPath = exports.inlineRef = void 0;
4 const util_1 = require("./util");
5 const equal = require("fast-deep-equal");
6 const traverse = require("json-schema-traverse");
7 const URI = require("uri-js");
8 // TODO refactor to use keyword definitions
9 const SIMPLE_INLINED = new Set([
27 function inlineRef(schema, limit = true) {
28 if (typeof schema == "boolean")
31 return !hasRef(schema);
34 return countKeys(schema) <= limit;
36 exports.inlineRef = inlineRef;
37 const REF_KEYWORDS = new Set([
44 function hasRef(schema) {
45 for (const key in schema) {
46 if (REF_KEYWORDS.has(key))
48 const sch = schema[key];
49 if (Array.isArray(sch) && sch.some(hasRef))
51 if (typeof sch == "object" && hasRef(sch))
56 function countKeys(schema) {
58 for (const key in schema) {
62 if (SIMPLE_INLINED.has(key))
64 if (typeof schema[key] == "object") {
65 (0, util_1.eachItem)(schema[key], (sch) => (count += countKeys(sch)));
67 if (count === Infinity)
72 function getFullPath(id = "", normalize) {
73 if (normalize !== false)
75 const p = URI.parse(id);
76 return _getFullPath(p);
78 exports.getFullPath = getFullPath;
79 function _getFullPath(p) {
80 return URI.serialize(p).split("#")[0] + "#";
82 exports._getFullPath = _getFullPath;
83 const TRAILING_SLASH_HASH = /#\/?$/;
84 function normalizeId(id) {
85 return id ? id.replace(TRAILING_SLASH_HASH, "") : "";
87 exports.normalizeId = normalizeId;
88 function resolveUrl(baseId, id) {
90 return URI.resolve(baseId, id);
92 exports.resolveUrl = resolveUrl;
93 const ANCHOR = /^[a-z_][-a-z0-9._]*$/i;
94 function getSchemaRefs(schema, baseId) {
95 if (typeof schema == "boolean")
97 const { schemaId } = this.opts;
98 const schId = normalizeId(schema[schemaId] || baseId);
99 const baseIds = { "": schId };
100 const pathPrefix = getFullPath(schId, false);
101 const localRefs = {};
102 const schemaRefs = new Set();
103 traverse(schema, { allKeys: true }, (sch, jsonPtr, _, parentJsonPtr) => {
104 if (parentJsonPtr === undefined)
106 const fullPath = pathPrefix + jsonPtr;
107 let baseId = baseIds[parentJsonPtr];
108 if (typeof sch[schemaId] == "string")
109 baseId = addRef.call(this, sch[schemaId]);
110 addAnchor.call(this, sch.$anchor);
111 addAnchor.call(this, sch.$dynamicAnchor);
112 baseIds[jsonPtr] = baseId;
113 function addRef(ref) {
114 ref = normalizeId(baseId ? URI.resolve(baseId, ref) : ref);
115 if (schemaRefs.has(ref))
118 let schOrRef = this.refs[ref];
119 if (typeof schOrRef == "string")
120 schOrRef = this.refs[schOrRef];
121 if (typeof schOrRef == "object") {
122 checkAmbiguosRef(sch, schOrRef.schema, ref);
124 else if (ref !== normalizeId(fullPath)) {
125 if (ref[0] === "#") {
126 checkAmbiguosRef(sch, localRefs[ref], ref);
127 localRefs[ref] = sch;
130 this.refs[ref] = fullPath;
135 function addAnchor(anchor) {
136 if (typeof anchor == "string") {
137 if (!ANCHOR.test(anchor))
138 throw new Error(`invalid anchor "${anchor}"`);
139 addRef.call(this, `#${anchor}`);
144 function checkAmbiguosRef(sch1, sch2, ref) {
145 if (sch2 !== undefined && !equal(sch1, sch2))
148 function ambiguos(ref) {
149 return new Error(`reference "${ref}" resolves to more than one schema`);
152 exports.getSchemaRefs = getSchemaRefs;
153 //# sourceMappingURL=resolve.js.map