-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
587 changed files
with
115,305 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { JSONSchema, JSONSchemaWithPath } from "./utils"; | ||
interface JSONSchemasGeneratorConfig { | ||
schemasDir: string; | ||
examplesDir?: string; | ||
propertiesManifestDir?: string; | ||
} | ||
export default class JSONSchemasGenerator implements JSONSchemasGeneratorConfig { | ||
readonly schemas: JSONSchema[]; | ||
readonly examples?: JSONSchema[]; | ||
readonly wrappedExamples: JSONSchemaWithPath[]; | ||
readonly propertiesManifest: object; | ||
readonly results: object; | ||
readonly schemasDir: string; | ||
readonly examplesDir?: string; | ||
readonly propertiesManifestDir?: string; | ||
constructor(config?: JSONSchemasGeneratorConfig); | ||
writeResolvedSchemas(subfolder: string, skipMergeAllOff?: boolean): void; | ||
writeResolvedExamples(subfolder: string): void; | ||
} | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"use strict"; | ||
var __importDefault = | ||
(this && this.__importDefault) || | ||
function (mod) { | ||
return mod && mod.__esModule ? mod : { default: mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* eslint-disable class-methods-use-this */ | ||
const fs_1 = __importDefault(require("fs")); | ||
const js_yaml_1 = __importDefault(require("js-yaml")); | ||
const json_schema_merge_allof_1 = __importDefault(require("json-schema-merge-allof")); | ||
const path_1 = __importDefault(require("path")); | ||
const settings_1 = require("./settings"); | ||
const utils_1 = require("./utils"); | ||
const DEFAULT_CONFIG = { | ||
schemasDir: settings_1.SCHEMAS_DIR, | ||
examplesDir: settings_1.EXAMPLES_DIR, | ||
propertiesManifestDir: settings_1.PROPERTIES_MANIFEST_PATH, | ||
}; | ||
class JSONSchemasGenerator { | ||
constructor(config = DEFAULT_CONFIG) { | ||
this.schemasDir = config.schemasDir; | ||
this.examplesDir = config.examplesDir; | ||
this.propertiesManifestDir = config.propertiesManifestDir; | ||
this.schemas = (0, utils_1.parseIncludeReferenceStatementsByDir)(this.schemasDir); | ||
this.wrappedExamples = this.examplesDir | ||
? (0, utils_1.parseIncludeReferenceStatementsByDir)(this.examplesDir, true) | ||
: []; | ||
this.examples = this.wrappedExamples.map((example) => example.data); | ||
this.propertiesManifest = this.propertiesManifestDir | ||
? js_yaml_1.default.load(fs_1.default.readFileSync(this.propertiesManifestDir, "utf-8")) | ||
: {}; | ||
this.results = Object.entries(this.propertiesManifest) | ||
.map((k) => (k[1].isResult ? k[0] : null)) | ||
.filter((x) => x); | ||
} | ||
writeResolvedSchemas(subfolder, skipMergeAllOff = false) { | ||
const schemasFolder = `${subfolder}/schema`; | ||
fs_1.default.rmSync(schemasFolder, { recursive: true, force: true }); | ||
const mergeAllOfConfig = { | ||
resolvers: { | ||
defaultResolver: json_schema_merge_allof_1.default.options.resolvers.title, | ||
}, | ||
}; | ||
const schemas = this.schemas.map((schema) => { | ||
var _a; | ||
console.log(`Resolving schema: ${schema.$id}`); | ||
const mergedSchema = skipMergeAllOff | ||
? schema | ||
: (0, json_schema_merge_allof_1.default)(schema, mergeAllOfConfig); | ||
const idAsPath = | ||
(_a = mergedSchema.$id) === null || _a === void 0 ? void 0 : _a.replace(/-/g, "_"); | ||
const fullPath = `${schemasFolder}/${idAsPath}.json`; | ||
fs_1.default.mkdirSync(path_1.default.dirname(fullPath), { recursive: true }); | ||
fs_1.default.writeFileSync(fullPath, JSON.stringify(mergedSchema, null, 4), "utf8"); | ||
return mergedSchema; | ||
}); | ||
fs_1.default.writeFileSync(`${subfolder}/schemas.json`, `${JSON.stringify(schemas)}`); | ||
} | ||
writeResolvedExamples(subfolder) { | ||
const examplesFolder = `${subfolder}/example`; | ||
fs_1.default.rmSync(`${examplesFolder}`, { recursive: true, force: true }); | ||
this.wrappedExamples.forEach((e) => { | ||
const idAsPath = e.path.replace(/-/g, "_"); | ||
const fullPath = `${examplesFolder}/${idAsPath}.json`; | ||
fs_1.default.mkdirSync(path_1.default.dirname(fullPath), { recursive: true }); | ||
fs_1.default.writeFileSync(fullPath, JSON.stringify(e.data, null, 4), "utf8"); | ||
}); | ||
} | ||
} | ||
exports.default = JSONSchemasGenerator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { JSONSchema } from "./utils"; | ||
export type JSONSchemasInterfaceQuery = { | ||
[key in keyof JSONSchema]: { | ||
$regex: string; | ||
}; | ||
}; | ||
export default class JSONSchemasInterface { | ||
static schemasCache: Map<string, import("json-schema").JSONSchema7>; | ||
static setSchemas(schema: JSONSchema[]): void; | ||
static addSchema(schema: JSONSchema): void; | ||
static getSchemaById(schemaId: string): import("json-schema").JSONSchema7 | undefined; | ||
/** | ||
* @example <caption>Search by $id regex</caption> | ||
* JSONSchemasInterface.matchSchema({ | ||
* $id: { | ||
* $regex: 'software-application' | ||
* } | ||
* }) | ||
* | ||
* @example <caption>Search by $id and title regex</caption> | ||
* JSONSchemasInterface.matchSchema({ | ||
* $id: { | ||
* $regex: 'software-application' | ||
* }, | ||
* title: { | ||
* $regex: 'application' | ||
* } | ||
* }) | ||
*/ | ||
static matchSchema(query: JSONSchemasInterfaceQuery): import("json-schema").JSONSchema7 | undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class JSONSchemasInterface { | ||
static setSchemas(schema) { | ||
schema.forEach((schema) => this.addSchema(schema)); | ||
} | ||
static addSchema(schema) { | ||
if (schema.$id) { | ||
this.schemasCache.set(schema.$id, schema); | ||
} | ||
} | ||
static getSchemaById(schemaId) { | ||
return this.schemasCache.get(schemaId); | ||
} | ||
/** | ||
* @example <caption>Search by $id regex</caption> | ||
* JSONSchemasInterface.matchSchema({ | ||
* $id: { | ||
* $regex: 'software-application' | ||
* } | ||
* }) | ||
* | ||
* @example <caption>Search by $id and title regex</caption> | ||
* JSONSchemasInterface.matchSchema({ | ||
* $id: { | ||
* $regex: 'software-application' | ||
* }, | ||
* title: { | ||
* $regex: 'application' | ||
* } | ||
* }) | ||
*/ | ||
static matchSchema(query) { | ||
const searchFields = Object.keys(query); | ||
return Array.from(this.schemasCache.values()).find((schema) => { | ||
return searchFields.every((field) => { | ||
const queryField = query[field]; | ||
const schemaField = schema[field]; | ||
if (!queryField || typeof schemaField !== "string") { | ||
return; | ||
} | ||
return new RegExp(queryField.$regex).test(schemaField); | ||
}); | ||
}); | ||
} | ||
} | ||
exports.default = JSONSchemasInterface; | ||
JSONSchemasInterface.schemasCache = new Map(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { SchemaObject } from "ajv"; | ||
import JSONSchemasInterface from "./JSONSchemasInterface"; | ||
export declare function readSchemaFolderSync(folderPath: string): SchemaObject[]; | ||
export default class JSONSchemasInterfaceServer extends JSONSchemasInterface { | ||
static schemaFolder: string; | ||
static setSchemaFolder(schemaFolder: string): void; | ||
static readSchemaFolder(): void; | ||
static getSchemaById(schemaId: string): import("json-schema").JSONSchema7 | undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"use strict"; | ||
var __importDefault = | ||
(this && this.__importDefault) || | ||
function (mod) { | ||
return mod && mod.__esModule ? mod : { default: mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.readSchemaFolderSync = void 0; | ||
const fs_1 = __importDefault(require("fs")); | ||
const path_1 = __importDefault(require("path")); | ||
const filesystem_1 = require("../utils/filesystem"); | ||
const JSONSchemasInterface_1 = __importDefault(require("./JSONSchemasInterface")); | ||
function readSchemaFolderSync(folderPath) { | ||
const schemas = []; | ||
(0, filesystem_1.walkDirSync)(folderPath, (filePath) => { | ||
if (path_1.default.extname(filePath) !== ".json") { | ||
return; | ||
} | ||
const schema = JSON.parse(fs_1.default.readFileSync(filePath).toString()); | ||
schemas.push(schema); | ||
}); | ||
return schemas; | ||
} | ||
exports.readSchemaFolderSync = readSchemaFolderSync; | ||
class JSONSchemasInterfaceServer extends JSONSchemasInterface_1.default { | ||
static setSchemaFolder(schemaFolder) { | ||
if (this.schemaFolder !== schemaFolder) { | ||
this.schemaFolder = schemaFolder; | ||
this.readSchemaFolder(); | ||
} | ||
} | ||
static readSchemaFolder() { | ||
const schemas = readSchemaFolderSync(this.schemaFolder); | ||
schemas.forEach((schema) => { | ||
if (schema.$id) { | ||
this.schemasCache.set(schema.$id, schema); | ||
} | ||
}); | ||
} | ||
static getSchemaById(schemaId) { | ||
if (this.schemasCache.size === 0) { | ||
this.readSchemaFolder(); | ||
} | ||
return super.getSchemaById(schemaId); | ||
} | ||
} | ||
exports.default = JSONSchemasInterfaceServer; | ||
JSONSchemasInterfaceServer.schemaFolder = path_1.default.resolve(__dirname, "./../schema"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { JSONSchema, JSONSchemaDefinition } from "./utils"; | ||
export type MapSchema = (prop: JSONSchemaDefinition) => JSONSchemaDefinition | undefined; | ||
export declare function mapObjectDeep(object: JSONSchemaDefinition, mapValue: MapSchema): JSONSchema; | ||
export declare function mapObjectDeep(object: JSONSchemaDefinition[], mapValue: MapSchema): JSONSchema[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mapObjectDeep = void 0; | ||
function mapObjectDeep(object, mapValue) { | ||
if (typeof object !== "object" || object === null) { | ||
return object; | ||
} | ||
if (Array.isArray(object)) { | ||
return object.map((innerValue) => mapObjectDeep(innerValue, mapValue)); | ||
} | ||
const mappedObject = mapValue(object) || object; | ||
const entries = Object.entries(mappedObject).map(([key, value]) => { | ||
return [key, mapObjectDeep(value, mapValue)]; | ||
}); | ||
return Object.fromEntries(entries); | ||
} | ||
exports.mapObjectDeep = mapObjectDeep; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export declare const SCHEMAS_DIR: string; | ||
export declare const EXAMPLES_DIR: string; | ||
export declare const PROPERTIES_MANIFEST_PATH: string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"use strict"; | ||
var __importDefault = | ||
(this && this.__importDefault) || | ||
function (mod) { | ||
return mod && mod.__esModule ? mod : { default: mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PROPERTIES_MANIFEST_PATH = exports.EXAMPLES_DIR = exports.SCHEMAS_DIR = void 0; | ||
const path_1 = __importDefault(require("path")); | ||
exports.SCHEMAS_DIR = path_1.default.resolve(__dirname, "../../../schema"); | ||
exports.EXAMPLES_DIR = path_1.default.resolve(__dirname, "../../../example"); | ||
exports.PROPERTIES_MANIFEST_PATH = path_1.default.resolve( | ||
__dirname, | ||
"../../../manifest/properties.yaml", | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface AnyObject { | ||
[key: string]: unknown; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { JSONSchema7, JSONSchema7Definition } from "json-schema"; | ||
export type JSONSchema = JSONSchema7; | ||
export type JSONSchemaDefinition = JSONSchema7Definition; | ||
/** | ||
* Resolves `include` and `$ref` statements. | ||
* @param filePath {String} file to parse. | ||
*/ | ||
export declare function parseIncludeReferenceStatements(filePath: string): JSONSchema; | ||
export interface JSONSchemaWithPath { | ||
data: JSONSchema; | ||
path: string; | ||
} | ||
export declare function parseIncludeReferenceStatementsByDir(dirPath: string, wrapInDataAndPath: true): JSONSchemaWithPath[]; | ||
export declare function parseIncludeReferenceStatementsByDir(dirPath: string, wrapInDataAndPath?: false): JSONSchema[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"use strict"; | ||
var __importDefault = | ||
(this && this.__importDefault) || | ||
function (mod) { | ||
return mod && mod.__esModule ? mod : { default: mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parseIncludeReferenceStatementsByDir = exports.parseIncludeReferenceStatements = void 0; | ||
// @ts-ignore | ||
const json_schema_deref_sync_1 = __importDefault(require("json-schema-deref-sync")); | ||
const path_1 = __importDefault(require("path")); | ||
const json_include_1 = require("../json_include"); | ||
const filesystem_1 = require("../utils/filesystem"); | ||
/** | ||
* Resolves `include` and `$ref` statements. | ||
* @param filePath {String} file to parse. | ||
*/ | ||
function parseIncludeReferenceStatements(filePath) { | ||
const jsonResolver = new json_include_1.JSONInclude(); | ||
const parsed = jsonResolver.parseIncludeStatements(filePath); | ||
const dirPath = path_1.default.dirname(filePath); | ||
let dereferenced = (0, json_schema_deref_sync_1.default)(parsed, { | ||
baseFolder: dirPath, | ||
removeIds: true, | ||
}); | ||
// handle circular references and use non-dereferenced source | ||
if (dereferenced instanceof Error && dereferenced.message === "Circular self reference") { | ||
dereferenced = parsed; | ||
} | ||
return dereferenced; | ||
} | ||
exports.parseIncludeReferenceStatements = parseIncludeReferenceStatements; | ||
/** | ||
* Resolves `include` and `$ref` statements for all the JSON files inside a given directory. | ||
* @param dirPath directory to parse. | ||
*/ | ||
function parseIncludeReferenceStatementsByDir(dirPath, wrapInDataAndPath = false) { | ||
const schemas = []; | ||
const schemasWithPath = []; | ||
const topDir = path_1.default.resolve(__dirname, "../../../"); | ||
(0, filesystem_1.walkDirSync)(dirPath, (filePath) => { | ||
if (filePath.endsWith(".json")) { | ||
const config = parseIncludeReferenceStatements(filePath); | ||
if (wrapInDataAndPath) { | ||
const _path = path_1.default.join( | ||
// remove leading slashes and "example" from path | ||
path_1.default | ||
.dirname(filePath) | ||
.replace(path_1.default.join(topDir, "example"), "") | ||
.replace(/^\/+/, ""), | ||
path_1.default.basename(filePath).replace(".json", ""), | ||
); | ||
schemasWithPath.push({ data: config, path: _path }); | ||
} else { | ||
schemas.push(config); | ||
} | ||
} | ||
}); | ||
return wrapInDataAndPath ? schemasWithPath : schemas; | ||
} | ||
exports.parseIncludeReferenceStatementsByDir = parseIncludeReferenceStatementsByDir; |
Oops, something went wrong.