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 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) {
95 if (typeof schema == "boolean")
97 const schemaId = normalizeId(schema.$id);
98 const baseIds = { "": schemaId };
99 const pathPrefix = getFullPath(schemaId, false);
100 const localRefs = {};
101 const schemaRefs = new Set();
102 traverse(schema, { allKeys: true }, (sch, jsonPtr, _, parentJsonPtr) => {
103 if (parentJsonPtr === undefined)
105 const fullPath = pathPrefix + jsonPtr;
106 let baseId = baseIds[parentJsonPtr];
107 if (typeof sch.$id == "string")
108 baseId = addRef.call(this, sch.$id);
109 addAnchor.call(this, sch.$anchor);
110 addAnchor.call(this, sch.$dynamicAnchor);
111 baseIds[jsonPtr] = baseId;
112 function addRef(ref) {
113 ref = normalizeId(baseId ? URI.resolve(baseId, ref) : ref);
114 if (schemaRefs.has(ref))
117 let schOrRef = this.refs[ref];
118 if (typeof schOrRef == "string")
119 schOrRef = this.refs[schOrRef];
120 if (typeof schOrRef == "object") {
121 checkAmbiguosRef(sch, schOrRef.schema, ref);
123 else if (ref !== normalizeId(fullPath)) {
124 if (ref[0] === "#") {
125 checkAmbiguosRef(sch, localRefs[ref], ref);
126 localRefs[ref] = sch;
129 this.refs[ref] = fullPath;
134 function addAnchor(anchor) {
135 if (typeof anchor == "string") {
136 if (!ANCHOR.test(anchor))
137 throw new Error(`invalid anchor "${anchor}"`);
138 addRef.call(this, `#${anchor}`);
143 function checkAmbiguosRef(sch1, sch2, ref) {
144 if (sch2 !== undefined && !equal(sch1, sch2))
147 function ambiguos(ref) {
148 return new Error(`reference "${ref}" resolves to more than one schema`);
151 exports.getSchemaRefs = getSchemaRefs;
152 //# sourceMappingURL=resolve.js.map