@scalar/openapi-parser 0.8.5 → 0.8.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +1 -1
- package/dist/configuration/index.js +27 -1
- package/dist/index.js +26 -1
- package/dist/lib/Validator/Validator.js +127 -1
- package/dist/{utils/load/plugins → plugins/fetch-urls}/fetchUrls.d.ts +1 -1
- package/dist/plugins/fetch-urls/fetchUrls.d.ts.map +1 -0
- package/dist/plugins/fetch-urls/fetchUrls.js +45 -0
- package/dist/plugins/fetch-urls/index.d.ts +2 -0
- package/dist/plugins/fetch-urls/index.d.ts.map +1 -0
- package/dist/plugins/fetch-urls/index.js +1 -0
- package/dist/plugins/read-files/index.d.ts +2 -0
- package/dist/plugins/read-files/index.d.ts.map +1 -0
- package/dist/plugins/read-files/index.js +1 -0
- package/dist/plugins/read-files/readFiles.d.ts +3 -0
- package/dist/plugins/read-files/readFiles.d.ts.map +1 -0
- package/dist/plugins/read-files/readFiles.js +56 -0
- package/dist/polyfills/path.js +120 -1
- package/dist/schemas/v2.0/schema.json.js +1623 -1
- package/dist/schemas/v3.0/schema.json.js +1673 -1
- package/dist/schemas/v3.1/schema.json.js +1451 -1
- package/dist/utils/betterAjvErrors/helpers.js +108 -1
- package/dist/utils/betterAjvErrors/index.js +15 -1
- package/dist/utils/betterAjvErrors/utils.js +31 -1
- package/dist/utils/betterAjvErrors/validation-errors/additional-prop.js +20 -1
- package/dist/utils/betterAjvErrors/validation-errors/base.js +34 -1
- package/dist/utils/betterAjvErrors/validation-errors/default.js +20 -1
- package/dist/utils/betterAjvErrors/validation-errors/enum.js +58 -1
- package/dist/utils/betterAjvErrors/validation-errors/pattern.js +20 -1
- package/dist/utils/betterAjvErrors/validation-errors/required.js +19 -1
- package/dist/utils/betterAjvErrors/validation-errors/unevaluated-prop.js +20 -1
- package/dist/utils/dereference.js +21 -1
- package/dist/utils/details.js +25 -1
- package/dist/utils/escapeJsonPointer.js +10 -1
- package/dist/utils/filter.js +17 -1
- package/dist/utils/getEntrypoint.js +8 -1
- package/dist/utils/getListOfReferences.js +27 -1
- package/dist/utils/getSegmentsFromPath.js +18 -1
- package/dist/utils/isFilesystem.js +11 -1
- package/dist/utils/isJson.js +11 -1
- package/dist/utils/isObject.js +6 -1
- package/dist/utils/isYaml.js +19 -1
- package/dist/utils/load/load.js +121 -1
- package/dist/utils/makeFilesystem.js +25 -1
- package/dist/utils/normalize.js +25 -1
- package/dist/utils/openapi/actions/details.js +13 -1
- package/dist/utils/openapi/actions/files.js +11 -1
- package/dist/utils/openapi/actions/get.js +13 -1
- package/dist/utils/openapi/actions/toJson.js +13 -1
- package/dist/utils/openapi/actions/toYaml.js +13 -1
- package/dist/utils/openapi/commands/dereferenceCommand.js +29 -1
- package/dist/utils/openapi/commands/filterCommand.js +28 -1
- package/dist/utils/openapi/commands/loadCommand.js +44 -1
- package/dist/utils/openapi/commands/upgradeCommand.js +31 -1
- package/dist/utils/openapi/commands/validateCommand.js +35 -1
- package/dist/utils/openapi/openapi.js +18 -1
- package/dist/utils/openapi/utils/queueTask.js +11 -1
- package/dist/utils/openapi/utils/workThroughQueue.js +67 -1
- package/dist/utils/resolveReferences.js +153 -1
- package/dist/utils/toJson.js +3 -1
- package/dist/utils/toYaml.js +5 -1
- package/dist/utils/transformErrors.js +23 -1
- package/dist/utils/traverse.d.ts +1 -1
- package/dist/utils/traverse.d.ts.map +1 -1
- package/dist/utils/traverse.js +26 -1
- package/dist/utils/unescapeJsonPointer.js +11 -1
- package/dist/utils/upgrade.js +20 -1
- package/dist/utils/upgradeFromThreeToThreeOne.d.ts +3 -1
- package/dist/utils/upgradeFromThreeToThreeOne.d.ts.map +1 -1
- package/dist/utils/upgradeFromThreeToThreeOne.js +122 -1
- package/dist/utils/upgradeFromTwoToThree.d.ts.map +1 -1
- package/dist/utils/upgradeFromTwoToThree.js +174 -1
- package/dist/utils/validate.js +18 -1
- package/package.json +15 -13
- package/dist/utils/load/plugins/fetchUrls.d.ts.map +0 -1
- package/dist/utils/load/plugins/fetchUrls.js +0 -1
- package/dist/utils/load/plugins/readFiles.d.ts +0 -3
- package/dist/utils/load/plugins/readFiles.d.ts.map +0 -1
- package/dist/utils/load/plugins/readFiles.js +0 -1
|
@@ -1 +1,108 @@
|
|
|
1
|
-
import{getErrors
|
|
1
|
+
import { getErrors, isRequiredError, isAnyOfError, isEnumError, getSiblings, notUndefined, concatAll, getChildren } from './utils.js';
|
|
2
|
+
import EnumValidationError from './validation-errors/enum.js';
|
|
3
|
+
import DefaultValidationError from './validation-errors/default.js';
|
|
4
|
+
import UnevaluatedPropValidationError from './validation-errors/unevaluated-prop.js';
|
|
5
|
+
import RequiredValidationError from './validation-errors/required.js';
|
|
6
|
+
import PatternValidationError from './validation-errors/pattern.js';
|
|
7
|
+
import AdditionalPropValidationError from './validation-errors/additional-prop.js';
|
|
8
|
+
|
|
9
|
+
/* eslint-disable no-param-reassign */
|
|
10
|
+
const JSON_POINTERS_REGEX = /\/[\w_-]+(\/\d+)?/g;
|
|
11
|
+
// Make a tree of errors from ajv errors array
|
|
12
|
+
function makeTree(ajvErrors = []) {
|
|
13
|
+
const root = { children: {} };
|
|
14
|
+
ajvErrors.forEach((ajvError) => {
|
|
15
|
+
const instancePath = typeof ajvError.instancePath !== 'undefined'
|
|
16
|
+
? ajvError.instancePath
|
|
17
|
+
: ajvError.dataPath;
|
|
18
|
+
// `dataPath === ''` is root
|
|
19
|
+
const paths = instancePath === '' ? [''] : instancePath.match(JSON_POINTERS_REGEX);
|
|
20
|
+
if (paths) {
|
|
21
|
+
paths.reduce((obj, path, i) => {
|
|
22
|
+
obj.children[path] = obj.children[path] || { children: {}, errors: [] };
|
|
23
|
+
if (i === paths.length - 1) {
|
|
24
|
+
obj.children[path].errors.push(ajvError);
|
|
25
|
+
}
|
|
26
|
+
return obj.children[path];
|
|
27
|
+
}, root);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
return root;
|
|
31
|
+
}
|
|
32
|
+
function filterRedundantErrors(root, parent, key) {
|
|
33
|
+
/**
|
|
34
|
+
* If there is a `required` error then we can just skip everythig else.
|
|
35
|
+
* And, also `required` should have more priority than `anyOf`. @see #8
|
|
36
|
+
*/
|
|
37
|
+
getErrors(root).forEach((error) => {
|
|
38
|
+
if (isRequiredError(error)) {
|
|
39
|
+
root.errors = [error];
|
|
40
|
+
root.children = {};
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
/**
|
|
44
|
+
* If there is an `anyOf` error that means we have more meaningful errors
|
|
45
|
+
* inside children. So we will just remove all errors from this level.
|
|
46
|
+
*
|
|
47
|
+
* If there are no children, then we don't delete the errors since we should
|
|
48
|
+
* have at least one error to report.
|
|
49
|
+
*/
|
|
50
|
+
if (getErrors(root).some(isAnyOfError)) {
|
|
51
|
+
if (Object.keys(root.children).length > 0) {
|
|
52
|
+
delete root.errors;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* If all errors are `enum` and siblings have any error then we can safely
|
|
57
|
+
* ignore the node.
|
|
58
|
+
*
|
|
59
|
+
* **CAUTION**
|
|
60
|
+
* Need explicit `root.errors` check because `[].every(fn) === true`
|
|
61
|
+
* https://en.wikipedia.org/wiki/Vacuous_truth#Vacuous_truths_in_mathematics
|
|
62
|
+
*/
|
|
63
|
+
if (root.errors?.length && getErrors(root).every(isEnumError)) {
|
|
64
|
+
if (getSiblings(parent)(root)
|
|
65
|
+
// Remove any reference which becomes `undefined` later
|
|
66
|
+
.filter(notUndefined)
|
|
67
|
+
.some(getErrors)) {
|
|
68
|
+
delete parent.children[key];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
Object.entries(root.children).forEach(([k, child]) => filterRedundantErrors(child, root, k));
|
|
72
|
+
}
|
|
73
|
+
function createErrorInstances(root, options) {
|
|
74
|
+
const errors = getErrors(root);
|
|
75
|
+
if (errors.length && errors.every(isEnumError)) {
|
|
76
|
+
const uniqueValues = new Set(concatAll([])(errors.map((e) => e.params.allowedValues)));
|
|
77
|
+
const allowedValues = [...uniqueValues];
|
|
78
|
+
const error = errors[0];
|
|
79
|
+
return [
|
|
80
|
+
new EnumValidationError({
|
|
81
|
+
...error,
|
|
82
|
+
params: { allowedValues },
|
|
83
|
+
}, options),
|
|
84
|
+
];
|
|
85
|
+
}
|
|
86
|
+
return concatAll(errors.reduce((ret, error) => {
|
|
87
|
+
switch (error.keyword) {
|
|
88
|
+
case 'additionalProperties':
|
|
89
|
+
return ret.concat(new AdditionalPropValidationError(error, options));
|
|
90
|
+
case 'pattern':
|
|
91
|
+
return ret.concat(new PatternValidationError(error, options));
|
|
92
|
+
case 'required':
|
|
93
|
+
return ret.concat(new RequiredValidationError(error, options));
|
|
94
|
+
case 'unevaluatedProperties':
|
|
95
|
+
return ret.concat(new UnevaluatedPropValidationError(error, options));
|
|
96
|
+
default:
|
|
97
|
+
return ret.concat(new DefaultValidationError(error, options));
|
|
98
|
+
}
|
|
99
|
+
}, []))(getChildren(root).map((child) => createErrorInstances(child, options)));
|
|
100
|
+
}
|
|
101
|
+
function prettify(ajvErrors, options) {
|
|
102
|
+
const tree = makeTree(ajvErrors || []);
|
|
103
|
+
// @ts-expect-error TODO
|
|
104
|
+
filterRedundantErrors(tree);
|
|
105
|
+
return createErrorInstances(tree, options);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export { createErrorInstances, prettify as default, filterRedundantErrors, makeTree };
|
|
@@ -1 +1,15 @@
|
|
|
1
|
-
import
|
|
1
|
+
import prettify from './helpers.js';
|
|
2
|
+
|
|
3
|
+
function betterAjvErrors(schema, data, errors, options = {}) {
|
|
4
|
+
const { indent = null, json = null } = options;
|
|
5
|
+
const jsonRaw = json || JSON.stringify(data, null, indent);
|
|
6
|
+
const customErrorToStructure = (error) => error.getError();
|
|
7
|
+
const customErrors = prettify(errors, {
|
|
8
|
+
data,
|
|
9
|
+
schema,
|
|
10
|
+
jsonRaw,
|
|
11
|
+
});
|
|
12
|
+
return customErrors.map(customErrorToStructure);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { betterAjvErrors };
|
|
@@ -1 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
// Basic
|
|
2
|
+
const eq = (x) => (y) => x === y;
|
|
3
|
+
const not = (fn) => (x) => !fn(x);
|
|
4
|
+
|
|
5
|
+
const getValues = (o) => Object.values(o);
|
|
6
|
+
|
|
7
|
+
const notUndefined = (x) => x !== undefined;
|
|
8
|
+
|
|
9
|
+
// Error
|
|
10
|
+
const isXError = (x) => (error) => error.keyword === x;
|
|
11
|
+
const isRequiredError = isXError('required');
|
|
12
|
+
const isAnyOfError = isXError('anyOf');
|
|
13
|
+
const isEnumError = isXError('enum');
|
|
14
|
+
const getErrors = (node) => (node && node.errors) || [];
|
|
15
|
+
|
|
16
|
+
// Node
|
|
17
|
+
const getChildren = (node) => (node && getValues(node.children)) || [];
|
|
18
|
+
|
|
19
|
+
const getSiblings =
|
|
20
|
+
(parent /*: Node */) => (node /*: Node */) /*: $ReadOnlyArray<Node> */ =>
|
|
21
|
+
getChildren(parent).filter(not(eq(node)));
|
|
22
|
+
|
|
23
|
+
const concatAll =
|
|
24
|
+
/* ::<T> */
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
(xs /*: $ReadOnlyArray<T> */) =>
|
|
28
|
+
(ys /* : $ReadOnlyArray<T> */) /* : $ReadOnlyArray<T> */ =>
|
|
29
|
+
ys.reduce((zs, z) => zs.concat(z), xs);
|
|
30
|
+
|
|
31
|
+
export { concatAll, getChildren, getErrors, getSiblings, isAnyOfError, isEnumError, isRequiredError, notUndefined };
|
|
@@ -1 +1,20 @@
|
|
|
1
|
-
import
|
|
1
|
+
import BaseValidationError from './base.js';
|
|
2
|
+
|
|
3
|
+
class AdditionalPropValidationError extends BaseValidationError {
|
|
4
|
+
constructor(...args) {
|
|
5
|
+
super(...args);
|
|
6
|
+
this.name = 'AdditionalPropValidationError';
|
|
7
|
+
this.options.isIdentifierLocation = true;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
getError() {
|
|
11
|
+
const { params } = this.options;
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
message: `Property ${params.additionalProperty} is not expected to be here`,
|
|
15
|
+
path: this.instancePath,
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { AdditionalPropValidationError as default };
|
|
@@ -1 +1,34 @@
|
|
|
1
|
-
|
|
1
|
+
// import { codeFrameColumns } from '@babel/code-frame';
|
|
2
|
+
// import chalk from 'chalk';
|
|
3
|
+
// import { getMetaFromPath } from '../json/index'
|
|
4
|
+
|
|
5
|
+
class BaseValidationError {
|
|
6
|
+
// eslint-disable-next-line default-param-last
|
|
7
|
+
constructor(
|
|
8
|
+
options = { isIdentifierLocation: false },
|
|
9
|
+
{ data, schema, jsonAst, jsonRaw },
|
|
10
|
+
) {
|
|
11
|
+
this.options = options;
|
|
12
|
+
this.data = data;
|
|
13
|
+
this.schema = schema;
|
|
14
|
+
this.jsonAst = jsonAst;
|
|
15
|
+
this.jsonRaw = jsonRaw;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @return {string}
|
|
20
|
+
*/
|
|
21
|
+
get instancePath() {
|
|
22
|
+
return typeof this.options.instancePath !== 'undefined'
|
|
23
|
+
? this.options.instancePath
|
|
24
|
+
: this.options.dataPath
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
getError() {
|
|
28
|
+
throw new Error(
|
|
29
|
+
`Implement the 'getError' method inside ${this.constructor.name}!`,
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { BaseValidationError as default };
|
|
@@ -1 +1,20 @@
|
|
|
1
|
-
import
|
|
1
|
+
import BaseValidationError from './base.js';
|
|
2
|
+
|
|
3
|
+
class DefaultValidationError extends BaseValidationError {
|
|
4
|
+
constructor(...args) {
|
|
5
|
+
super(...args);
|
|
6
|
+
this.name = 'DefaultValidationError';
|
|
7
|
+
this.options.isSkipEndLocation = true;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
getError() {
|
|
11
|
+
const { keyword, message } = this.options;
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
message: `${keyword} ${message}`,
|
|
15
|
+
path: this.instancePath,
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { DefaultValidationError as default };
|
|
@@ -1 +1,58 @@
|
|
|
1
|
-
import
|
|
1
|
+
import pointer from 'jsonpointer';
|
|
2
|
+
import leven from 'leven';
|
|
3
|
+
import BaseValidationError from './base.js';
|
|
4
|
+
|
|
5
|
+
class EnumValidationError extends BaseValidationError {
|
|
6
|
+
constructor(...args) {
|
|
7
|
+
super(...args);
|
|
8
|
+
this.name = 'EnumValidationError';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
getError() {
|
|
12
|
+
const { message, params } = this.options;
|
|
13
|
+
const bestMatch = this.findBestMatch();
|
|
14
|
+
const allowedValues = params.allowedValues.join(', ');
|
|
15
|
+
|
|
16
|
+
const output = {
|
|
17
|
+
message: `${message}: ${allowedValues}`,
|
|
18
|
+
path: this.instancePath,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
if (bestMatch !== null) {
|
|
22
|
+
output.suggestion = `Did you mean ${bestMatch}?`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return output
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
findBestMatch() {
|
|
29
|
+
const {
|
|
30
|
+
params: { allowedValues },
|
|
31
|
+
} = this.options;
|
|
32
|
+
|
|
33
|
+
const currentValue =
|
|
34
|
+
this.instancePath === ''
|
|
35
|
+
? this.data
|
|
36
|
+
: pointer.get(this.data, this.instancePath);
|
|
37
|
+
|
|
38
|
+
if (!currentValue) {
|
|
39
|
+
return null
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const bestMatch = allowedValues
|
|
43
|
+
.map((value) => ({
|
|
44
|
+
value,
|
|
45
|
+
weight: leven(value, currentValue.toString()),
|
|
46
|
+
}))
|
|
47
|
+
.sort((x, y) =>
|
|
48
|
+
x.weight > y.weight ? 1 : x.weight < y.weight ? -1 : 0,
|
|
49
|
+
)[0];
|
|
50
|
+
|
|
51
|
+
return allowedValues.length === 1 ||
|
|
52
|
+
bestMatch.weight < bestMatch.value.length
|
|
53
|
+
? bestMatch.value
|
|
54
|
+
: null
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { EnumValidationError as default };
|
|
@@ -1 +1,20 @@
|
|
|
1
|
-
import
|
|
1
|
+
import BaseValidationError from './base.js';
|
|
2
|
+
|
|
3
|
+
class PatternValidationError extends BaseValidationError {
|
|
4
|
+
constructor(...args) {
|
|
5
|
+
super(...args);
|
|
6
|
+
this.name = 'PatternValidationError';
|
|
7
|
+
this.options.isIdentifierLocation = true;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
getError() {
|
|
11
|
+
const { params, propertyName } = this.options;
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
message: `Property "${propertyName}" must match pattern ${params.pattern}`,
|
|
15
|
+
path: this.instancePath,
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { PatternValidationError as default };
|
|
@@ -1 +1,19 @@
|
|
|
1
|
-
import
|
|
1
|
+
import BaseValidationError from './base.js';
|
|
2
|
+
|
|
3
|
+
class RequiredValidationError extends BaseValidationError {
|
|
4
|
+
constructor(...args) {
|
|
5
|
+
super(...args);
|
|
6
|
+
this.name = 'RequiredValidationError';
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
getError() {
|
|
10
|
+
const { message } = this.options;
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
message: `${message}`,
|
|
14
|
+
path: this.instancePath,
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { RequiredValidationError as default };
|
|
@@ -1 +1,20 @@
|
|
|
1
|
-
import
|
|
1
|
+
import BaseValidationError from './base.js';
|
|
2
|
+
|
|
3
|
+
class UnevaluatedPropValidationError extends BaseValidationError {
|
|
4
|
+
constructor(...args) {
|
|
5
|
+
super(...args);
|
|
6
|
+
this.name = 'UnevaluatedPropValidationError';
|
|
7
|
+
this.options.isIdentifierLocation = true;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
getError() {
|
|
11
|
+
const { params } = this.options;
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
message: `Property ${params.unevaluatedProperty} is not expected to be here`,
|
|
15
|
+
path: this.instancePath,
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { UnevaluatedPropValidationError as default };
|
|
@@ -1 +1,21 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import { details } from './details.js';
|
|
2
|
+
import { getEntrypoint } from './getEntrypoint.js';
|
|
3
|
+
import { makeFilesystem } from './makeFilesystem.js';
|
|
4
|
+
import { resolveReferences } from './resolveReferences.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Validates an OpenAPI schema and resolves all references.
|
|
8
|
+
*/
|
|
9
|
+
async function dereference(value, options) {
|
|
10
|
+
const filesystem = makeFilesystem(value);
|
|
11
|
+
const entrypoint = getEntrypoint(filesystem);
|
|
12
|
+
const result = resolveReferences(filesystem, options);
|
|
13
|
+
return {
|
|
14
|
+
specification: entrypoint.specification,
|
|
15
|
+
errors: result.errors,
|
|
16
|
+
schema: result.schema,
|
|
17
|
+
...details(entrypoint.specification),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { dereference };
|
package/dist/utils/details.js
CHANGED
|
@@ -1 +1,25 @@
|
|
|
1
|
-
import{OpenApiVersions
|
|
1
|
+
import { OpenApiVersions } from '../configuration/index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get versions of the OpenAPI specification.
|
|
5
|
+
*/
|
|
6
|
+
function details(specification) {
|
|
7
|
+
for (const version of new Set(OpenApiVersions)) {
|
|
8
|
+
const specificationType = version === '2.0' ? 'swagger' : 'openapi';
|
|
9
|
+
const value = specification[specificationType];
|
|
10
|
+
if (typeof value === 'string' && value.startsWith(version)) {
|
|
11
|
+
return {
|
|
12
|
+
version: version,
|
|
13
|
+
specificationType,
|
|
14
|
+
specificationVersion: value,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
version: undefined,
|
|
20
|
+
specificationType: undefined,
|
|
21
|
+
specificationVersion: undefined,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { details };
|
|
@@ -1 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Escapes a JSON pointer string.
|
|
3
|
+
*
|
|
4
|
+
* Example: `/foo/bar~baz` -> `/foo~1bar~0baz`
|
|
5
|
+
*/
|
|
6
|
+
function escapeJsonPointer(str) {
|
|
7
|
+
return str.replace(/~/g, '~0').replace(/\//g, '~1');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { escapeJsonPointer };
|
package/dist/utils/filter.js
CHANGED
|
@@ -1 +1,17 @@
|
|
|
1
|
-
import{getEntrypoint
|
|
1
|
+
import { getEntrypoint } from './getEntrypoint.js';
|
|
2
|
+
import { makeFilesystem } from './makeFilesystem.js';
|
|
3
|
+
import { traverse } from './traverse.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Filter the specification based on the callback
|
|
7
|
+
*/
|
|
8
|
+
function filter(specification, callback) {
|
|
9
|
+
const filesystem = makeFilesystem(specification);
|
|
10
|
+
return {
|
|
11
|
+
specification: traverse(getEntrypoint(filesystem).specification, (schema) => {
|
|
12
|
+
return callback(schema) ? schema : undefined;
|
|
13
|
+
}),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { filter };
|
|
@@ -1 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Return just the entrypoint of the filesystem.
|
|
3
|
+
*/
|
|
4
|
+
function getEntrypoint(filesystem) {
|
|
5
|
+
return filesystem?.find((file) => file.isEntrypoint);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export { getEntrypoint };
|
|
@@ -1 +1,27 @@
|
|
|
1
|
-
import{traverse
|
|
1
|
+
import { traverse } from './traverse.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Walks through the specification and returns all references as an array.
|
|
5
|
+
*
|
|
6
|
+
* Warning: Doesn’t return internal references.
|
|
7
|
+
*/
|
|
8
|
+
function getListOfReferences(specification) {
|
|
9
|
+
const references = [];
|
|
10
|
+
// Make sure we’re dealing with an object
|
|
11
|
+
if (!specification || typeof specification !== 'object') {
|
|
12
|
+
return references;
|
|
13
|
+
}
|
|
14
|
+
// Traverse the specification and collect all references
|
|
15
|
+
traverse(specification, (value) => {
|
|
16
|
+
if (value.$ref &&
|
|
17
|
+
typeof value.$ref === 'string' &&
|
|
18
|
+
!value.$ref.startsWith('#')) {
|
|
19
|
+
references.push(value.$ref.split('#')[0]);
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
});
|
|
23
|
+
// Remove duplicates
|
|
24
|
+
return [...new Set(references)];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { getListOfReferences };
|
|
@@ -1 +1,18 @@
|
|
|
1
|
-
import{unescapeJsonPointer
|
|
1
|
+
import { unescapeJsonPointer } from './unescapeJsonPointer.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Translate `/paths/~1test` to `['paths', '/test']`
|
|
5
|
+
*/
|
|
6
|
+
function getSegmentsFromPath(path) {
|
|
7
|
+
return (
|
|
8
|
+
// /paths/~1test
|
|
9
|
+
path
|
|
10
|
+
// ['', 'paths', '~1test']
|
|
11
|
+
.split('/')
|
|
12
|
+
// ['paths', '~test']
|
|
13
|
+
.slice(1)
|
|
14
|
+
// ['paths', '/test']
|
|
15
|
+
.map(unescapeJsonPointer));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { getSegmentsFromPath };
|
|
@@ -1 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Check if the value is a filesystem
|
|
3
|
+
*/
|
|
4
|
+
function isFilesystem(value) {
|
|
5
|
+
return (typeof value !== 'undefined' &&
|
|
6
|
+
Array.isArray(value) &&
|
|
7
|
+
value.length > 0 &&
|
|
8
|
+
value.some((file) => file.isEntrypoint === true));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { isFilesystem };
|
package/dist/utils/isJson.js
CHANGED
package/dist/utils/isObject.js
CHANGED
package/dist/utils/isYaml.js
CHANGED
|
@@ -1 +1,19 @@
|
|
|
1
|
-
import{parse
|
|
1
|
+
import { parse } from 'yaml';
|
|
2
|
+
|
|
3
|
+
function isYaml(value) {
|
|
4
|
+
// Line breaks
|
|
5
|
+
if (!value.includes('\n')) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
try {
|
|
9
|
+
parse(value, {
|
|
10
|
+
maxAliasCount: 10000,
|
|
11
|
+
});
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { isYaml };
|