@json-schema-tools/dereferencer
Advanced tools
Comparing version 1.3.1 to 1.4.0
import { JSONSchema } from "@json-schema-tools/meta-schema"; | ||
export interface RefCache { | ||
[k: string]: JSONSchema; | ||
} | ||
/** | ||
@@ -10,4 +13,8 @@ * Options that can be passed to the derefencer constructor. | ||
recursive?: boolean; | ||
/** | ||
* Preseed the dereferencer with resolved refs | ||
*/ | ||
refCache?: RefCache; | ||
rootSchema?: JSONSchema; | ||
} | ||
export declare const defaultDereferencerOptions: DereferencerOptions; | ||
/** | ||
@@ -34,5 +41,2 @@ * Error thrown by the constructor when given a ref that isn't a string | ||
} | ||
export interface RefCache { | ||
[k: string]: JSONSchema; | ||
} | ||
/** | ||
@@ -46,3 +50,3 @@ * When instantiated, represents a fully configured dereferencer. When constructed, references are pulled out. | ||
private schema; | ||
private refCache; | ||
refCache: RefCache; | ||
constructor(schema: JSONSchema, options?: DereferencerOptions); | ||
@@ -49,0 +53,0 @@ /** |
@@ -53,9 +53,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.NonStringRefError = exports.defaultDereferencerOptions = void 0; | ||
exports.NonStringRefError = void 0; | ||
var traverse_1 = __importDefault(require("@json-schema-tools/traverse")); | ||
var reference_resolver_1 = __importDefault(require("@json-schema-tools/reference-resolver")); | ||
var fast_safe_stringify_1 = __importDefault(require("fast-safe-stringify")); | ||
exports.defaultDereferencerOptions = { | ||
recursive: true, | ||
}; | ||
/** | ||
@@ -89,13 +86,13 @@ * Error thrown by the constructor when given a ref that isn't a string | ||
exports.NonStringRefError = NonStringRefError; | ||
var copyProps = function (s1, s2) { | ||
Object | ||
.entries(s2) | ||
.filter(function (_a) { | ||
var k = _a[0]; | ||
return k !== "$ref"; | ||
}) | ||
.forEach(function (_a) { | ||
var k = _a[0], v = _a[1]; | ||
return s1[k] = v; | ||
}); | ||
var copyOrNot = function (s1, s2) { | ||
if (s1.$ref !== undefined && | ||
Object.keys(s1).length > 1 && | ||
(s2 !== true && s2 !== false)) { | ||
var reflessCopy = __assign(__assign({}, s2), s1); | ||
delete reflessCopy.$ref; | ||
return reflessCopy; | ||
} | ||
else { | ||
return s2; | ||
} | ||
}; | ||
@@ -108,5 +105,17 @@ /** | ||
function Dereferencer(schema, options) { | ||
if (options === void 0) { options = exports.defaultDereferencerOptions; } | ||
if (options === void 0) { options = {}; } | ||
this.options = options; | ||
this.refCache = {}; | ||
if (this.options.recursive === undefined) { | ||
this.options.recursive = true; | ||
} | ||
if (this.options.rootSchema === undefined) { | ||
this.options.rootSchema = schema; | ||
} | ||
if (schema !== true && schema !== false && schema.$id) { | ||
this.options.rootSchema = schema; | ||
} | ||
if (this.options.refCache) { | ||
this.refCache = this.options.refCache; | ||
} | ||
this.schema = schema; // shallow copy breaks recursive | ||
@@ -125,6 +134,6 @@ this.refs = this.collectRefs(); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var refMap, proms, _i, _a, ref, fetched, subDereffer, _b, subFetched, _c, _d, _e, _f, rootRef, schemaCopy, recurseResolve; | ||
var refMap, unfetchedRefs, proms, _i, unfetchedRefs_1, ref, fetched, refProm, subDerefferOpts, subDereffer, subFetchedProm, subFetched; | ||
var _this = this; | ||
return __generator(this, function (_g) { | ||
switch (_g.label) { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
@@ -136,56 +145,56 @@ refMap = {}; | ||
if (this.refs.length === 0) { | ||
delete this.schema.definitions; | ||
return [2 /*return*/, Promise.resolve(this.schema)]; | ||
} | ||
unfetchedRefs = this.refs.filter(function (r) { return refMap[r] === undefined; }); | ||
proms = []; | ||
_i = 0, _a = this.refs; | ||
_g.label = 1; | ||
_i = 0, unfetchedRefs_1 = unfetchedRefs; | ||
_a.label = 1; | ||
case 1: | ||
if (!(_i < _a.length)) return [3 /*break*/, 10]; | ||
ref = _a[_i]; | ||
if (!(_i < unfetchedRefs_1.length)) return [3 /*break*/, 12]; | ||
ref = unfetchedRefs_1[_i]; | ||
fetched = void 0; | ||
if (!(this.refCache[ref] !== undefined)) return [3 /*break*/, 2]; | ||
fetched = this.refCache[ref]; | ||
return [3 /*break*/, 4]; | ||
case 2: return [4 /*yield*/, reference_resolver_1.default(ref, this.schema)]; | ||
return [3 /*break*/, 5]; | ||
case 2: | ||
if (!(ref === "#")) return [3 /*break*/, 3]; | ||
fetched = this.options.rootSchema; | ||
return [3 /*break*/, 5]; | ||
case 3: | ||
fetched = _g.sent(); | ||
this.refCache[ref] = fetched; | ||
_g.label = 4; | ||
refProm = reference_resolver_1.default(ref, this.options.rootSchema); | ||
proms.push(refProm); | ||
return [4 /*yield*/, refProm]; | ||
case 4: | ||
proms.push(fetched); | ||
if (!(this.options.recursive === true && ref.charAt(0) !== "#")) return [3 /*break*/, 7]; | ||
_b = Dereferencer.bind; | ||
return [4 /*yield*/, fetched]; | ||
fetched = _a.sent(); | ||
_a.label = 5; | ||
case 5: | ||
subDereffer = new (_b.apply(Dereferencer, [void 0, _g.sent(), this.options]))(); | ||
subFetched = subDereffer.resolve(); | ||
proms.push(subFetched); | ||
_c = refMap; | ||
_d = ref; | ||
return [4 /*yield*/, subFetched]; | ||
if (!(this.options.recursive === true && fetched !== true && fetched !== false && ref !== "#")) return [3 /*break*/, 9]; | ||
subDerefferOpts = __assign(__assign({}, this.options), { refCache: this.refCache }); | ||
subDereffer = new Dereferencer(fetched, subDerefferOpts); | ||
if (!(subDereffer.refs.length !== 0)) return [3 /*break*/, 7]; | ||
subFetchedProm = subDereffer.resolve(); | ||
proms.push(subFetchedProm); | ||
return [4 /*yield*/, subFetchedProm]; | ||
case 6: | ||
_c[_d] = _g.sent(); | ||
return [3 /*break*/, 9]; | ||
subFetched = _a.sent(); | ||
// if there are props other than $ref present on the fetched schema, | ||
// we have to break referential integrity, creating a new schema all together. | ||
refMap[ref] = copyOrNot(fetched, subFetched); | ||
return [3 /*break*/, 8]; | ||
case 7: | ||
_e = refMap; | ||
_f = ref; | ||
return [4 /*yield*/, fetched]; | ||
case 8: | ||
_e[_f] = _g.sent(); | ||
_g.label = 9; | ||
refMap[ref] = fetched; | ||
_a.label = 8; | ||
case 8: return [3 /*break*/, 10]; | ||
case 9: | ||
refMap[ref] = fetched; | ||
_a.label = 10; | ||
case 10: | ||
this.refCache[ref] = refMap[ref]; | ||
_a.label = 11; | ||
case 11: | ||
_i++; | ||
return [3 /*break*/, 1]; | ||
case 10: | ||
case 12: | ||
if (this.schema.$ref !== undefined) { | ||
rootRef = refMap[this.schema.$ref]; | ||
if (rootRef === true || rootRef === false) { | ||
this.schema = rootRef; | ||
} | ||
else { | ||
schemaCopy = __assign({}, this.schema); | ||
this.schema = rootRef; | ||
copyProps(this.schema, schemaCopy); | ||
} | ||
this.schema = copyOrNot(this.schema, refMap[this.schema.$ref]); | ||
} | ||
@@ -198,10 +207,4 @@ else { | ||
if (s.$ref !== undefined) { | ||
var reffed = refMap[s.$ref]; | ||
if (reffed === true || reffed === false) { | ||
return reffed; | ||
} | ||
else { | ||
copyProps(reffed, s); | ||
return reffed; | ||
} | ||
var reffedSchema = refMap[s.$ref]; | ||
return copyOrNot(s, reffedSchema); | ||
} | ||
@@ -211,8 +214,11 @@ return s; | ||
} | ||
if (this.options.recursive === true) { | ||
this.refs = this.collectRefs(); | ||
recurseResolve = this.resolve(); | ||
proms.push(recurseResolve); | ||
} | ||
return [2 /*return*/, Promise.all(proms).then(function () { return _this.schema; })]; | ||
return [2 /*return*/, Promise | ||
.all(proms) | ||
.then(function () { | ||
if (_this.schema !== false && _this.schema !== true) { // while not required, makes it nicer. | ||
delete _this.schema.definitions; | ||
delete _this.schema.components; | ||
} | ||
return _this.schema; | ||
})]; | ||
} | ||
@@ -219,0 +225,0 @@ }); |
import dereffer from "./dereferencer"; | ||
export { NonStringRefError, DereferencerOptions, defaultDereferencerOptions, } from "./dereferencer"; | ||
export { NonStringRefError, DereferencerOptions, } from "./dereferencer"; | ||
export default dereffer; |
@@ -6,7 +6,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.defaultDereferencerOptions = exports.NonStringRefError = void 0; | ||
exports.NonStringRefError = void 0; | ||
var dereferencer_1 = __importDefault(require("./dereferencer")); | ||
var dereferencer_2 = require("./dereferencer"); | ||
Object.defineProperty(exports, "NonStringRefError", { enumerable: true, get: function () { return dereferencer_2.NonStringRefError; } }); | ||
Object.defineProperty(exports, "defaultDereferencerOptions", { enumerable: true, get: function () { return dereferencer_2.defaultDereferencerOptions; } }); | ||
exports.default = dereferencer_1.default; |
@@ -0,1 +1,15 @@ | ||
# [1.4.0](https://github.com/json-schema-tools/dereferencer/compare/1.3.1...1.4.0) (2020-11-10) | ||
### Bug Fixes | ||
* remove console logs ([341a900](https://github.com/json-schema-tools/dereferencer/commit/341a9008e0f3d8c9176d74dd1d8397f9a2567ed5)) | ||
* run lint with --fix ([24e51e6](https://github.com/json-schema-tools/dereferencer/commit/24e51e632fff550094c449a2cf57b9ecc30626d1)) | ||
* tests and coverage all green now ([f265130](https://github.com/json-schema-tools/dereferencer/commit/f265130480d0c940618245e08693dfdaf37bb26a)) | ||
### Features | ||
* refactor into something that will work for all the cases ([5ce57c0](https://github.com/json-schema-tools/dereferencer/commit/5ce57c06b3f8fb26222c3ffc4d92f32145010a84)) | ||
## [1.3.1](https://github.com/json-schema-tools/dereferencer/compare/1.3.0...1.3.1) (2020-11-04) | ||
@@ -2,0 +16,0 @@ |
{ | ||
"name": "@json-schema-tools/dereferencer", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "Dereference (aka parse refs) from JSON Schemas", | ||
@@ -12,3 +12,3 @@ "main": "build/index.js", | ||
"build:code": "tsc", | ||
"lint": "eslint . --ext .ts", | ||
"lint": "eslint . --ext .ts --fix", | ||
"test": "npm run test:unit && npm run test:web", | ||
@@ -39,3 +39,3 @@ "test:unit": "jest --coverage", | ||
"jest": "^24.8.0", | ||
"ts-jest": "^24.1.0", | ||
"ts-jest": "^24.3.0", | ||
"typedoc": "^0.19.1", | ||
@@ -42,0 +42,0 @@ "typescript": "^4.0.5" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42132
320