schema-manager
Advanced tools
Comparing version 0.0.3 to 0.0.4
import { Schema } from './model/schema'; | ||
export * from './model/schema'; | ||
export * from './manager'; | ||
export declare const manager: import("./model/schema").ISchemaManager; | ||
export declare const schemas: import("./model/schema").ISchemaManager; | ||
export declare const add: (value: Schema) => Schema; | ||
export declare const load: (value: string | Schema) => Promise<Schema>; | ||
export declare const load: (value: string | Schema) => Promise<Schema[]>; | ||
export declare const get: (key: string) => Schema; | ||
export declare const list: () => Schema[]; | ||
export declare const solve: (value: string | Schema) => Schema; | ||
export declare const externalRefs: (schema: Schema) => string[]; | ||
export declare const normalize: (source: Schema) => Schema; |
22
index.js
@@ -26,31 +26,31 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.normalize = exports.externalRefs = exports.solve = exports.get = exports.load = exports.add = exports.manager = void 0; | ||
exports.normalize = exports.solve = exports.list = exports.get = exports.load = exports.add = exports.schemas = void 0; | ||
const schemaManager_1 = require("./manager/schemaManager"); | ||
__exportStar(require("./model/schema"), exports); | ||
__exportStar(require("./manager"), exports); | ||
exports.manager = schemaManager_1.SchemaManager.instance; | ||
exports.schemas = schemaManager_1.SchemaManager.instance; | ||
const add = (value) => { | ||
return exports.manager.add(value); | ||
return exports.schemas.add(value); | ||
}; | ||
exports.add = add; | ||
const load = (value) => __awaiter(void 0, void 0, void 0, function* () { | ||
return exports.manager.load(value); | ||
return exports.schemas.load(value); | ||
}); | ||
exports.load = load; | ||
const get = (key) => { | ||
return exports.manager.get(key); | ||
return exports.schemas.get(key); | ||
}; | ||
exports.get = get; | ||
const list = () => { | ||
return exports.schemas.list(); | ||
}; | ||
exports.list = list; | ||
const solve = (value) => { | ||
return exports.manager.solve(value); | ||
return exports.schemas.solve(value); | ||
}; | ||
exports.solve = solve; | ||
const externalRefs = (schema) => { | ||
return exports.manager.externalRefs(schema); | ||
}; | ||
exports.externalRefs = externalRefs; | ||
const normalize = (source) => { | ||
return exports.manager.normalize(source); | ||
return exports.schemas.normalize(source); | ||
}; | ||
exports.normalize = normalize; | ||
//# sourceMappingURL=index.js.map |
@@ -1,5 +0,6 @@ | ||
import { ISchemaTransform, Schema } from '../model/schema'; | ||
export declare class SchemaCompleter implements ISchemaTransform { | ||
execute(schema: Schema): Schema; | ||
import { ISchemaCompleter, Schema } from '../model/schema'; | ||
export declare class SchemaCompleter implements ISchemaCompleter { | ||
complete(schema: Schema): void; | ||
private completeId; | ||
private solveId; | ||
private completeRef; | ||
@@ -10,4 +11,3 @@ private absoluteRef; | ||
private findSchemaByPath; | ||
private solveId; | ||
private removeProperties; | ||
} |
@@ -6,3 +6,3 @@ "use strict"; | ||
class SchemaCompleter { | ||
execute(schema) { | ||
complete(schema) { | ||
if (schema === undefined || schema === null) { | ||
@@ -12,3 +12,3 @@ throw new Error('source is empty'); | ||
if (typeof schema !== 'object') { | ||
return schema; | ||
return; | ||
} | ||
@@ -18,3 +18,2 @@ this.completeId(schema, schema, schema.$id); | ||
this.removeProperties(schema); | ||
return schema; | ||
} | ||
@@ -30,6 +29,20 @@ completeId(root, data, parentId) { | ||
else if (data && typeof data === 'object') { | ||
if (data.$anchor || data.$id) { | ||
data.$id = this.solveId(data, _parentId); | ||
if (data.$id && typeof data.$id === 'string') { | ||
data.$id = this.solveId(data.$id, data.$anchor, _parentId); | ||
_parentId = data.$id; | ||
if (data.$anchor && typeof data.$anchor === 'string') { | ||
delete data.$anchor; | ||
} | ||
} | ||
else if (data.$anchor && typeof data.$anchor === 'string') { | ||
if (parentId === undefined) { | ||
throw new Error(`Could not resolve anchor: ${data.$anchor} without parent`); | ||
} | ||
if (data.$id !== undefined && typeof data.$id !== 'string') { | ||
throw new Error(`Could not resolve anchor: ${data.$anchor} if $id is not string`); | ||
} | ||
data.$id = `${parentId}#${data.$anchor}`; | ||
_parentId = data.$id; | ||
delete data.$anchor; | ||
} | ||
for (const entry of Object.entries(data)) { | ||
@@ -40,2 +53,27 @@ this.completeId(root, entry[1], _parentId); | ||
} | ||
solveId(id, anchor, parentId) { | ||
try { | ||
let _id; | ||
if (parentId === undefined || id.startsWith('http:')) { | ||
_id = id; | ||
} | ||
else if (id === parentId) { | ||
_id = id; | ||
} | ||
else if (id && parentId !== undefined) { | ||
_id = _1.Helper.urlJoin(parentId, id); | ||
} | ||
else { | ||
_id = id; | ||
} | ||
if (anchor) { | ||
return `${_id}#${anchor}`; | ||
} | ||
return _id; | ||
} | ||
catch (error) { | ||
console.error(error); | ||
throw error; | ||
} | ||
} | ||
completeRef(root, current, path, parentId) { | ||
@@ -111,2 +149,3 @@ if (Array.isArray(current)) { | ||
const ref = _1.Helper.urlJoin(parentId, current.$ref); | ||
// busca si el uri es un id dentro del current schema | ||
let found = this.schemaPathById(current, ref, '#'); | ||
@@ -116,2 +155,3 @@ if (found) { | ||
} | ||
// busca si el uri es un id dentro del schema | ||
found = this.schemaPathById(root, ref, '#'); | ||
@@ -121,2 +161,6 @@ if (found) { | ||
} | ||
// si comienza con http es un uri externo | ||
if (ref.startsWith('http')) { | ||
return ref; | ||
} | ||
} | ||
@@ -224,29 +268,2 @@ throw new Error(`Ref ${current.$ref} is invalid`); | ||
} | ||
solveId(data, parentId) { | ||
let id; | ||
if (data.$id && (parentId === undefined || data.$id.startsWith('http:'))) { | ||
id = data.$id; | ||
} | ||
else if (data.$id === parentId) { | ||
id = data.$id; | ||
} | ||
else if (data.$id && parentId !== undefined) { | ||
id = _1.Helper.urlJoin(parentId, data.$id); | ||
} | ||
else { | ||
id = undefined; | ||
} | ||
if (data.$anchor && id !== undefined) { | ||
return `${id}#${data.$anchor}`; | ||
// return `${id}/#${data.$anchor}` | ||
} | ||
if (data.$anchor && parentId) { | ||
return `${parentId}#${data.$anchor}`; | ||
// return parentId.endsWith('/') ? `${parentId}#${data.$anchor}` : `${parentId}/#${data.$anchor}` | ||
} | ||
if (id !== undefined) { | ||
return id; | ||
} | ||
throw new Error(`Could not resolve id for id: ${data.$id}, parent:${parentId}, anchor: ${data.$anchor}`); | ||
} | ||
removeProperties(data) { | ||
@@ -261,3 +278,2 @@ if (Array.isArray(data)) { | ||
delete data.$comment; | ||
delete data.$anchor; | ||
delete data.$schema; | ||
@@ -264,0 +280,0 @@ delete data.$extends; |
@@ -1,5 +0,5 @@ | ||
import { ISchemaTransform, Schema } from '../model/schema'; | ||
export declare class SchemaExtender implements ISchemaTransform { | ||
execute(schema: Schema): Schema; | ||
import { ISchemaExtender, Schema } from '../model/schema'; | ||
export declare class SchemaExtender implements ISchemaExtender { | ||
extend(schema: Schema): void; | ||
private extendDef; | ||
} |
@@ -6,3 +6,3 @@ "use strict"; | ||
class SchemaExtender { | ||
execute(schema) { | ||
extend(schema) { | ||
if (schema === undefined || schema === null) { | ||
@@ -15,3 +15,3 @@ throw new Error('source is empty'); | ||
if (schema.$defs === undefined || schema.$defs === null) { | ||
return schema; | ||
return; | ||
} | ||
@@ -21,3 +21,2 @@ for (const def of Object.values(schema.$defs)) { | ||
} | ||
return schema; | ||
} | ||
@@ -24,0 +23,0 @@ extendDef(def, defs) { |
@@ -1,2 +0,2 @@ | ||
import { Schema, ISchemaTransform, ISchemaManager } from '../model/schema'; | ||
import { Schema, ISchemaCompleter, ISchemaExtender, ISchemaManager, ISchemaLoader } from '../model/schema'; | ||
export declare class SchemaManagerBuilder { | ||
@@ -7,12 +7,16 @@ build(): ISchemaManager; | ||
private schemas; | ||
private transformers; | ||
constructor(transformers: ISchemaTransform[]); | ||
completer: ISchemaCompleter; | ||
extender: ISchemaExtender; | ||
loader: ISchemaLoader; | ||
constructor(completer: ISchemaCompleter, extender: ISchemaExtender, loader: ISchemaLoader); | ||
private static _instance; | ||
static get instance(): ISchemaManager; | ||
load(value: string | Schema): Promise<Schema>; | ||
load(value: string | Schema): Promise<Schema[]>; | ||
add(value: Schema): Schema; | ||
private getKey; | ||
get(key: string): Schema; | ||
list(): Schema[]; | ||
solve(value: string | Schema): Schema; | ||
externalRefs(schema: Schema): string[]; | ||
normalize(source: Schema): Schema; | ||
private externalRefs; | ||
} |
@@ -16,5 +16,6 @@ "use strict"; | ||
const schemaExtender_1 = require("./schemaExtender"); | ||
const schemaLoader_1 = require("./schemaLoader"); | ||
class SchemaManagerBuilder { | ||
build() { | ||
return new SchemaManager([new schemaExtender_1.SchemaExtender(), new schemaCompleter_1.SchemaCompleter()]); | ||
return new SchemaManager(new schemaCompleter_1.SchemaCompleter(), new schemaExtender_1.SchemaExtender(), new schemaLoader_1.SchemaLoader()); | ||
} | ||
@@ -24,6 +25,7 @@ } | ||
class SchemaManager { | ||
constructor(transformers) { | ||
constructor(completer, extender, loader) { | ||
this.schemas = {}; | ||
this.transformers = []; | ||
this.transformers = transformers; | ||
this.completer = completer; | ||
this.extender = extender; | ||
this.loader = loader; | ||
} | ||
@@ -38,4 +40,5 @@ static get instance() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const list = []; | ||
if (value === null || value === undefined || typeof value === 'boolean') { | ||
return value; | ||
return [value]; | ||
} | ||
@@ -46,12 +49,11 @@ let schema; | ||
schema = this.schemas[value]; | ||
if (schema) { | ||
return schema; | ||
if (!schema) { | ||
const loaded = yield this.loader.load(value); | ||
if (!loaded) { | ||
throw Error(`The schema in ${value} not found`); | ||
} | ||
schema = this.normalize(loaded); | ||
key = schema.$id && typeof schema.$id === 'string' ? schema.$id : value; | ||
this.schemas[key] = schema; | ||
} | ||
const content = yield _1.Helper.get(value); | ||
const downloaded = _1.Helper.tryParse(content); | ||
if (!downloaded) { | ||
throw Error(`The schema in ${value} not found`); | ||
} | ||
schema = this.normalize(downloaded); | ||
key = schema.$id || value; | ||
} | ||
@@ -63,10 +65,12 @@ else { | ||
schema = this.normalize(value); | ||
key = schema.$id || _1.Helper.createKey(schema); | ||
key = this.getKey(schema); | ||
this.schemas[key] = schema; | ||
} | ||
this.schemas[key] = schema; | ||
list.push(schema); | ||
const externalsRefs = this.externalRefs(schema); | ||
for (const externalsRef of externalsRefs) { | ||
this.load(externalsRef); | ||
const children = yield this.load(externalsRef); | ||
list.push(...children); | ||
} | ||
return schema; | ||
return list; | ||
}); | ||
@@ -85,6 +89,9 @@ } | ||
} | ||
const key = schema.$id || _1.Helper.createKey(schema); | ||
const key = this.getKey(schema); | ||
this.schemas[key] = schema; | ||
return schema; | ||
} | ||
getKey(schema) { | ||
return schema.$id && typeof schema.$id === 'string' ? schema.$id : _1.Helper.createKey(schema); | ||
} | ||
get(key) { | ||
@@ -97,2 +104,5 @@ const schema = this.schemas[key]; | ||
} | ||
list() { | ||
return Object.values(this.schemas); | ||
} | ||
solve(value) { | ||
@@ -110,11 +120,2 @@ if (value === null || value === undefined || typeof value === 'boolean') { | ||
} | ||
externalRefs(schema) { | ||
const ids = _1.Helper.findAllInObject(schema, (value) => { | ||
return value.$id !== undefined && value.$id.startsWith('http'); | ||
}).map(p => p.$id); | ||
const refs = _1.Helper.findAllInObject(schema, (value) => { | ||
return value.$ref !== undefined && value.$ref.startsWith('http'); | ||
}).map(p => p.$ref); | ||
return refs.filter(p => !ids.includes(p)); | ||
} | ||
normalize(source) { | ||
@@ -127,10 +128,18 @@ if (source === undefined || source === null) { | ||
} | ||
let schema = _1.Helper.clone(source); | ||
for (const transformer of this.transformers) { | ||
schema = transformer.execute(schema); | ||
} | ||
const schema = _1.Helper.clone(source); | ||
this.extender.extend(schema); | ||
this.completer.complete(schema); | ||
return schema; | ||
} | ||
externalRefs(schema) { | ||
const ids = _1.Helper.findAllInObject(schema, (value) => { | ||
return value.$id !== undefined && typeof value.$id === 'string' && value.$id.startsWith('http'); | ||
}).map(p => p.$id); | ||
const refs = _1.Helper.findAllInObject(schema, (value) => { | ||
return value.$ref !== undefined && typeof value.$ref === 'string' && value.$ref.startsWith('http'); | ||
}).map(p => p.$ref); | ||
return refs.filter(p => !ids.includes(p)); | ||
} | ||
} | ||
exports.SchemaManager = SchemaManager; | ||
//# sourceMappingURL=schemaManager.js.map |
@@ -9,12 +9,18 @@ export interface Schema { | ||
} | ||
export interface ISchemaTransform { | ||
execute(schema: Schema): Schema; | ||
export interface ISchemaCompleter { | ||
complete(schema: Schema): void; | ||
} | ||
export interface ISchemaExtender { | ||
extend(schema: Schema): void; | ||
} | ||
export interface ISchemaLoader { | ||
load(uri: string): Promise<Schema>; | ||
} | ||
export interface ISchemaManager { | ||
add(value: Schema): Schema; | ||
load(value: string | Schema): Promise<Schema>; | ||
load(value: string | Schema): Promise<Schema[]>; | ||
get(key: string): Schema; | ||
list(): Schema[]; | ||
solve(value: string | Schema): Schema; | ||
externalRefs(schema: Schema): string[]; | ||
normalize(source: Schema): Schema; | ||
} |
{ | ||
"name": "schema-manager", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Schema Manager", | ||
@@ -5,0 +5,0 @@ "author": "Flavio Lionel Rita <flaviolrita@hotmail.com>", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
53445
30
640