@microsoft.azure/openapi-validator-core
Version:
Azure OpenAPI Validator
128 lines • 4.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertJsonPath = exports.getRange = exports.defaultFileSystem = exports.getOpenapiType = exports.isExample = exports.traverse = exports.parseJsonRef = exports.normalizePath = exports.isUriAbsolute = exports.followReference = void 0;
const uri_1 = require("@azure-tools/uri");
const jsonpath_1 = require("./jsonpath");
const types_1 = require("./types");
function followReference(doc, schema, inventory) {
const getRefModel = (docToSearch, refValue, visited) => {
if (visited.includes(refValue)) {
throw new Error("Found circle reference: " + visited.join("->"));
}
visited.push(refValue);
const refSlices = (0, exports.parseJsonRef)(refValue);
const pathExpression = refSlices[1].split("/").slice(1);
try {
const result = (0, jsonpath_1.nodes)(docToSearch, (0, jsonpath_1.stringify)(pathExpression));
return result.length !== 0 ? result[0].value : undefined;
}
catch (err) {
return undefined;
}
};
if (schema && doc) {
if (schema.$ref) {
const refSlices = (0, exports.parseJsonRef)(schema.$ref);
if (inventory && refSlices[0]) {
doc = inventory.getDocuments(refSlices[0]);
}
schema = getRefModel(doc, `#${refSlices[1]}`, []);
return followReference(doc, schema, inventory);
}
return schema;
}
return undefined;
}
exports.followReference = followReference;
function isUriAbsolute(url) {
return /^[a-z]+:\/\//.test(url);
}
exports.isUriAbsolute = isUriAbsolute;
const normalizePath = (path) => {
if (isUriAbsolute(path)) {
return path;
}
return (0, uri_1.createFileOrFolderUri)(path);
};
exports.normalizePath = normalizePath;
const parseJsonRef = (ref) => {
return ref.split("#");
};
exports.parseJsonRef = parseJsonRef;
function traverse(obj, path, visited, options, visitor) {
if (!obj) {
return;
}
if (visited.has(obj)) {
return;
}
visited.add(obj);
if (visitor(obj, path, options) === false) {
return;
}
if (Array.isArray(obj)) {
for (const [index, item] of obj.entries()) {
traverse(item, [...path, index.toString()], visited, options, visitor);
}
}
else if (typeof obj === "object") {
for (const [key, item] of Object.entries(obj)) {
traverse(item, [...path, key], visited, options, visitor);
}
}
return;
}
exports.traverse = traverse;
function isExample(path) {
return path.split(/\\|\//g).includes("examples");
}
exports.isExample = isExample;
function getOpenapiType(type) {
switch (type) {
case "arm": {
return types_1.OpenApiTypes.arm;
}
case "data-plane": {
return types_1.OpenApiTypes.dataplane;
}
case "rpaas": {
return types_1.OpenApiTypes.rpaas;
}
case "providerHub": {
return types_1.OpenApiTypes.rpaas;
}
default:
return types_1.OpenApiTypes.default;
}
}
exports.getOpenapiType = getOpenapiType;
exports.defaultFileSystem = {
read: async (uri) => {
return await (0, uri_1.readUri)(uri);
}
};
function getRange(inventory, specPath, path) {
const document = inventory.getInternalDocument(specPath);
if (path && path[0] === "$") {
path = path.slice(1);
}
return document === null || document === void 0 ? void 0 : document.getPositionFromJsonPath(path);
}
exports.getRange = getRange;
function convertJsonPath(doc, paths) {
if (paths && doc) {
const convertedPaths = [];
paths = paths[0] === "$" ? paths.slice(1) : paths;
for (const path of paths) {
if (!doc || typeof doc !== "object") {
return convertedPaths;
}
convertedPaths.push(Array.isArray(doc) ? Number.parseInt(path) : path);
doc = doc[path];
}
return convertedPaths;
}
return [];
}
exports.convertJsonPath = convertJsonPath;
//# sourceMappingURL=utils.js.map