@redocly/openapi-core 1.0.0-beta.49 → 1.0.0-beta.53

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.
Files changed (136) hide show
  1. package/__tests__/normalizeVisitors.test.ts +1 -1
  2. package/__tests__/utils.ts +1 -1
  3. package/__tests__/walk.test.ts +77 -4
  4. package/lib/benchmark/benches/recommended-oas3.bench.js +2 -1
  5. package/lib/benchmark/utils.d.ts +1 -1
  6. package/lib/bundle.d.ts +1 -1
  7. package/lib/bundle.js +10 -10
  8. package/lib/config/builtIn.d.ts +2 -1
  9. package/lib/config/builtIn.js +9 -1
  10. package/lib/config/config.d.ts +5 -7
  11. package/lib/config/config.js +40 -100
  12. package/lib/config/load.d.ts +2 -0
  13. package/lib/config/load.js +65 -0
  14. package/lib/config/rules.d.ts +1 -1
  15. package/lib/format/codeframes.js +1 -1
  16. package/lib/index.d.ts +4 -3
  17. package/lib/index.js +13 -11
  18. package/lib/lint.d.ts +6 -19
  19. package/lib/lint.js +13 -43
  20. package/lib/oas-types.d.ts +19 -0
  21. package/lib/oas-types.js +42 -0
  22. package/lib/resolve.d.ts +6 -2
  23. package/lib/resolve.js +18 -5
  24. package/lib/rules/ajv.d.ts +3 -3
  25. package/lib/rules/ajv.js +21 -18
  26. package/lib/rules/common/info-contact.js +2 -1
  27. package/lib/rules/common/info-description.js +2 -1
  28. package/lib/rules/common/info-license-url.js +2 -1
  29. package/lib/rules/common/license-url.js +2 -1
  30. package/lib/rules/common/no-ambiguous-paths.js +2 -1
  31. package/lib/rules/common/no-enum-type-mismatch.js +2 -1
  32. package/lib/rules/common/no-identical-paths.js +2 -1
  33. package/lib/rules/common/no-path-trailing-slash.js +2 -1
  34. package/lib/rules/common/operation-2xx-response.js +2 -1
  35. package/lib/rules/common/operation-description.js +2 -1
  36. package/lib/rules/common/operation-operationId-unique.js +2 -1
  37. package/lib/rules/common/operation-operationId-url-safe.js +2 -1
  38. package/lib/rules/common/operation-operationId.js +9 -4
  39. package/lib/rules/common/operation-parameters-unique.js +2 -1
  40. package/lib/rules/common/operation-security-defined.js +2 -1
  41. package/lib/rules/common/operation-singular-tag.js +2 -1
  42. package/lib/rules/common/operation-summary.js +2 -1
  43. package/lib/rules/common/operation-tag-defined.js +2 -1
  44. package/lib/rules/common/parameter-description.js +2 -1
  45. package/lib/rules/common/path-declaration-must-exist.js +2 -1
  46. package/lib/rules/common/path-http-verbs-order.js +2 -1
  47. package/lib/rules/common/path-not-include-query.js +2 -1
  48. package/lib/rules/common/path-params-defined.js +3 -2
  49. package/lib/rules/common/paths-kebab-case.js +2 -1
  50. package/lib/rules/common/registry-dependencies.js +2 -1
  51. package/lib/rules/common/spec.js +5 -2
  52. package/lib/rules/common/tag-description.js +2 -1
  53. package/lib/rules/common/tags-alphabetical.js +2 -1
  54. package/lib/rules/no-unresolved-refs.js +2 -1
  55. package/lib/rules/oas2/boolean-parameter-prefixes.js +2 -1
  56. package/lib/rules/oas2/index.d.ts +1 -1
  57. package/lib/rules/oas2/index.js +1 -1
  58. package/lib/rules/oas3/boolean-parameter-prefixes.js +2 -1
  59. package/lib/rules/oas3/index.d.ts +2 -1
  60. package/lib/rules/oas3/index.js +2 -2
  61. package/lib/rules/oas3/no-empty-servers.js +2 -1
  62. package/lib/rules/oas3/no-example-value-and-externalValue.js +2 -1
  63. package/lib/rules/oas3/no-invalid-media-type-examples.js +20 -10
  64. package/lib/rules/oas3/no-server-example.com.js +2 -1
  65. package/lib/rules/oas3/no-server-trailing-slash.js +2 -1
  66. package/lib/rules/oas3/no-servers-empty-enum.js +2 -1
  67. package/lib/rules/oas3/no-undefined-server-variable.js +2 -1
  68. package/lib/rules/oas3/no-unused-components.js +2 -1
  69. package/lib/rules/other/stats.js +2 -1
  70. package/lib/types/oas2.js +1 -1
  71. package/lib/types/oas3.js +1 -1
  72. package/lib/types/oas3_1.js +1 -1
  73. package/lib/utils.d.ts +1 -1
  74. package/lib/visitors.d.ts +3 -1
  75. package/lib/visitors.js +4 -4
  76. package/lib/walk.d.ts +1 -1
  77. package/lib/walk.js +11 -2
  78. package/package.json +7 -7
  79. package/src/__tests__/lint.test.ts +44 -0
  80. package/src/benchmark/benches/recommended-oas3.bench.ts +2 -1
  81. package/src/benchmark/benchmark.js +3 -1
  82. package/src/benchmark/utils.ts +1 -1
  83. package/src/bundle.ts +2 -2
  84. package/src/config/__tests__/resolve-plugins.test.ts +1 -1
  85. package/src/config/builtIn.ts +11 -1
  86. package/src/config/config.ts +30 -78
  87. package/src/config/load.ts +60 -0
  88. package/src/config/rules.ts +1 -1
  89. package/src/format/codeframes.ts +1 -1
  90. package/src/index.ts +4 -3
  91. package/src/lint.ts +19 -56
  92. package/src/oas-types.ts +58 -0
  93. package/src/resolve.ts +17 -4
  94. package/src/rules/__tests__/config.ts +10 -0
  95. package/src/rules/__tests__/no-unresolved-refs.test.ts +9 -21
  96. package/src/rules/ajv.ts +27 -23
  97. package/src/rules/common/__tests__/info-description.test.ts +8 -16
  98. package/src/rules/common/__tests__/info-license.test.ts +3 -3
  99. package/src/rules/common/__tests__/license-url.test.ts +3 -3
  100. package/src/rules/common/__tests__/no-ambiguous-paths.test.ts +2 -2
  101. package/src/rules/common/__tests__/no-enum-type-mismatch.test.ts +50 -5
  102. package/src/rules/common/__tests__/no-identical-paths.test.ts +2 -2
  103. package/src/rules/common/__tests__/no-path-trailing-slash.test.ts +4 -4
  104. package/src/rules/common/__tests__/operation-2xx-response.test.ts +4 -4
  105. package/src/rules/common/__tests__/operation-operationId-unique.test.ts +3 -3
  106. package/src/rules/common/__tests__/operation-operationId-url-safe.test.ts +2 -5
  107. package/src/rules/common/__tests__/operation-parameters-unique.test.ts +5 -17
  108. package/src/rules/common/__tests__/operation-security-defined.test.ts +3 -3
  109. package/src/rules/common/__tests__/operation-singular-tag.test.ts +3 -3
  110. package/src/rules/common/__tests__/path-http-verbs-order.test.ts +3 -3
  111. package/src/rules/common/__tests__/path-not-include-query.test.ts +3 -3
  112. package/src/rules/common/__tests__/path-params-defined.test.ts +4 -4
  113. package/src/rules/common/__tests__/paths-kebab-case.test.ts +3 -3
  114. package/src/rules/common/__tests__/tag-description.test.ts +3 -3
  115. package/src/rules/common/__tests__/tags-alphabetical.test.ts +3 -3
  116. package/src/rules/common/operation-operationId.ts +7 -3
  117. package/src/rules/common/spec.ts +4 -1
  118. package/src/rules/oas2/__tests__/boolean-parameter-prefixes.test.ts +7 -10
  119. package/src/rules/oas2/__tests__/spec/referenceableScalars.test.ts +3 -6
  120. package/src/rules/oas2/__tests__/spec/utils.ts +2 -0
  121. package/src/rules/oas2/index.ts +1 -10
  122. package/src/rules/oas3/__tests__/boolean-parameter-prefixes.test.ts +7 -10
  123. package/src/rules/oas3/__tests__/fixtures/common.yaml +11 -0
  124. package/src/rules/oas3/__tests__/no-empty-enum-servers.com.test.ts +7 -7
  125. package/src/rules/oas3/__tests__/no-example-value-and-externalValue.test.ts +3 -9
  126. package/src/rules/oas3/__tests__/no-invalid-media-type-examples.test.ts +103 -29
  127. package/src/rules/oas3/__tests__/no-server-example.com.test.ts +3 -3
  128. package/src/rules/oas3/__tests__/no-server-trailing-slash.test.ts +3 -3
  129. package/src/rules/oas3/__tests__/no-unused-components.test.ts +2 -2
  130. package/src/rules/oas3/__tests__/spec/spec.test.ts +3 -1
  131. package/src/rules/oas3/__tests__/spec/utils.ts +2 -0
  132. package/src/rules/oas3/index.ts +3 -4
  133. package/src/rules/oas3/no-invalid-media-type-examples.ts +27 -19
  134. package/src/visitors.ts +12 -8
  135. package/src/walk.ts +24 -8
  136. package/tsconfig.tsbuildinfo +1 -3084
package/lib/index.d.ts CHANGED
@@ -7,13 +7,14 @@ export { Oas2Definition } from './typings/swagger';
7
7
  export { StatsAccumulator, StatsName } from './typings/common';
8
8
  export { normalizeTypes } from './types';
9
9
  export { Stats } from './rules/other/stats';
10
- export { loadConfig, Config, LintConfig, RawConfig, IGNORE_FILE } from './config/config';
10
+ export { Config, LintConfig, RawConfig, IGNORE_FILE } from './config/config';
11
+ export { loadConfig } from './config/load';
11
12
  export { RedoclyClient } from './redocly';
12
13
  export { Source, BaseResolver, Document, resolveDocument, ResolveError, YamlParseError } from './resolve';
13
14
  export { unescapePointer } from './ref-utils';
14
- export { detectOpenAPI, OasMajorVersion, openAPIMajor } from './lint';
15
+ export { detectOpenAPI, OasMajorVersion, openAPIMajor, OasVersion } from './oas-types';
15
16
  export { normalizeVisitors } from './visitors';
16
17
  export { WalkContext, walkDocument, NormalizedProblem, ProblemSeverity, LineColLocationObject, LocationObject, Loc } from './walk';
17
18
  export { formatProblems, OutputFormat, getTotals, Totals } from './format/format';
18
- export { OasVersion, lint, lint as validate, lintDocument } from './lint';
19
+ export { lint, lint as validate, lintDocument, lintFromString } from './lint';
19
20
  export { bundle } from './bundle';
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.bundle = exports.lintDocument = exports.validate = exports.lint = exports.OasVersion = exports.getTotals = exports.formatProblems = exports.walkDocument = exports.normalizeVisitors = exports.openAPIMajor = exports.OasMajorVersion = exports.detectOpenAPI = exports.unescapePointer = exports.YamlParseError = exports.ResolveError = exports.resolveDocument = exports.BaseResolver = exports.Source = exports.RedoclyClient = exports.IGNORE_FILE = exports.LintConfig = exports.Config = exports.loadConfig = exports.Stats = exports.normalizeTypes = exports.Oas2Types = exports.Oas3Types = exports.Oas3_1Types = void 0;
3
+ exports.bundle = exports.lintFromString = exports.lintDocument = exports.validate = exports.lint = exports.getTotals = exports.formatProblems = exports.walkDocument = exports.normalizeVisitors = exports.OasVersion = exports.openAPIMajor = exports.OasMajorVersion = exports.detectOpenAPI = exports.unescapePointer = exports.YamlParseError = exports.ResolveError = exports.resolveDocument = exports.BaseResolver = exports.Source = exports.RedoclyClient = exports.loadConfig = exports.IGNORE_FILE = exports.LintConfig = exports.Config = exports.Stats = exports.normalizeTypes = exports.Oas2Types = exports.Oas3Types = exports.Oas3_1Types = void 0;
4
4
  var oas3_1_1 = require("./types/oas3_1");
5
5
  Object.defineProperty(exports, "Oas3_1Types", { enumerable: true, get: function () { return oas3_1_1.Oas3_1Types; } });
6
6
  var oas3_1 = require("./types/oas3");
@@ -12,10 +12,11 @@ Object.defineProperty(exports, "normalizeTypes", { enumerable: true, get: functi
12
12
  var stats_1 = require("./rules/other/stats");
13
13
  Object.defineProperty(exports, "Stats", { enumerable: true, get: function () { return stats_1.Stats; } });
14
14
  var config_1 = require("./config/config");
15
- Object.defineProperty(exports, "loadConfig", { enumerable: true, get: function () { return config_1.loadConfig; } });
16
15
  Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return config_1.Config; } });
17
16
  Object.defineProperty(exports, "LintConfig", { enumerable: true, get: function () { return config_1.LintConfig; } });
18
17
  Object.defineProperty(exports, "IGNORE_FILE", { enumerable: true, get: function () { return config_1.IGNORE_FILE; } });
18
+ var load_1 = require("./config/load");
19
+ Object.defineProperty(exports, "loadConfig", { enumerable: true, get: function () { return load_1.loadConfig; } });
19
20
  var redocly_1 = require("./redocly");
20
21
  Object.defineProperty(exports, "RedoclyClient", { enumerable: true, get: function () { return redocly_1.RedoclyClient; } });
21
22
  var resolve_1 = require("./resolve");
@@ -26,10 +27,11 @@ Object.defineProperty(exports, "ResolveError", { enumerable: true, get: function
26
27
  Object.defineProperty(exports, "YamlParseError", { enumerable: true, get: function () { return resolve_1.YamlParseError; } });
27
28
  var ref_utils_1 = require("./ref-utils");
28
29
  Object.defineProperty(exports, "unescapePointer", { enumerable: true, get: function () { return ref_utils_1.unescapePointer; } });
29
- var lint_1 = require("./lint");
30
- Object.defineProperty(exports, "detectOpenAPI", { enumerable: true, get: function () { return lint_1.detectOpenAPI; } });
31
- Object.defineProperty(exports, "OasMajorVersion", { enumerable: true, get: function () { return lint_1.OasMajorVersion; } });
32
- Object.defineProperty(exports, "openAPIMajor", { enumerable: true, get: function () { return lint_1.openAPIMajor; } });
30
+ var oas_types_1 = require("./oas-types");
31
+ Object.defineProperty(exports, "detectOpenAPI", { enumerable: true, get: function () { return oas_types_1.detectOpenAPI; } });
32
+ Object.defineProperty(exports, "OasMajorVersion", { enumerable: true, get: function () { return oas_types_1.OasMajorVersion; } });
33
+ Object.defineProperty(exports, "openAPIMajor", { enumerable: true, get: function () { return oas_types_1.openAPIMajor; } });
34
+ Object.defineProperty(exports, "OasVersion", { enumerable: true, get: function () { return oas_types_1.OasVersion; } });
33
35
  var visitors_1 = require("./visitors");
34
36
  Object.defineProperty(exports, "normalizeVisitors", { enumerable: true, get: function () { return visitors_1.normalizeVisitors; } });
35
37
  var walk_1 = require("./walk");
@@ -37,10 +39,10 @@ Object.defineProperty(exports, "walkDocument", { enumerable: true, get: function
37
39
  var format_1 = require("./format/format");
38
40
  Object.defineProperty(exports, "formatProblems", { enumerable: true, get: function () { return format_1.formatProblems; } });
39
41
  Object.defineProperty(exports, "getTotals", { enumerable: true, get: function () { return format_1.getTotals; } });
40
- var lint_2 = require("./lint");
41
- Object.defineProperty(exports, "OasVersion", { enumerable: true, get: function () { return lint_2.OasVersion; } });
42
- Object.defineProperty(exports, "lint", { enumerable: true, get: function () { return lint_2.lint; } });
43
- Object.defineProperty(exports, "validate", { enumerable: true, get: function () { return lint_2.lint; } });
44
- Object.defineProperty(exports, "lintDocument", { enumerable: true, get: function () { return lint_2.lintDocument; } });
42
+ var lint_1 = require("./lint");
43
+ Object.defineProperty(exports, "lint", { enumerable: true, get: function () { return lint_1.lint; } });
44
+ Object.defineProperty(exports, "validate", { enumerable: true, get: function () { return lint_1.lint; } });
45
+ Object.defineProperty(exports, "lintDocument", { enumerable: true, get: function () { return lint_1.lintDocument; } });
46
+ Object.defineProperty(exports, "lintFromString", { enumerable: true, get: function () { return lint_1.lintFromString; } });
45
47
  var bundle_1 = require("./bundle");
46
48
  Object.defineProperty(exports, "bundle", { enumerable: true, get: function () { return bundle_1.bundle; } });
package/lib/lint.d.ts CHANGED
@@ -1,33 +1,20 @@
1
1
  import { BaseResolver, Document } from './resolve';
2
- import { Oas3Rule, Oas3Preprocessor, Oas2Rule, Oas2Preprocessor } from './visitors';
3
2
  import { NodeType } from './types';
4
3
  import { LintConfig, Config } from './config/config';
5
- export declare enum OasVersion {
6
- Version2 = "oas2",
7
- Version3_0 = "oas3_0",
8
- Version3_1 = "oas3_1"
9
- }
10
- export declare enum OasMajorVersion {
11
- Version2 = "oas2",
12
- Version3 = "oas3"
13
- }
14
- export declare type RuleSet<T> = Record<string, T>;
15
- export declare type Oas3RuleSet = Record<string, Oas3Rule>;
16
- export declare type Oas2RuleSet = Record<string, Oas2Rule>;
17
- export declare type Oas3PreprocessorsSet = Record<string, Oas3Preprocessor>;
18
- export declare type Oas2PreprocessorsSet = Record<string, Oas2Preprocessor>;
19
- export declare type Oas3DecoratorsSet = Record<string, Oas3Preprocessor>;
20
- export declare type Oas2DecoratorsSet = Record<string, Oas2Preprocessor>;
21
4
  export declare function lint(opts: {
22
5
  ref: string;
23
6
  config: Config;
24
7
  externalRefResolver?: BaseResolver;
25
8
  }): Promise<import("./walk").NormalizedProblem[]>;
9
+ export declare function lintFromString(opts: {
10
+ source: string;
11
+ absoluteRef?: string;
12
+ config: Config;
13
+ externalRefResolver?: BaseResolver;
14
+ }): Promise<import("./walk").NormalizedProblem[]>;
26
15
  export declare function lintDocument(opts: {
27
16
  document: Document;
28
17
  config: LintConfig;
29
18
  customTypes?: Record<string, NodeType>;
30
19
  externalRefResolver: BaseResolver;
31
20
  }): Promise<import("./walk").NormalizedProblem[]>;
32
- export declare function detectOpenAPI(root: any): OasVersion;
33
- export declare function openAPIMajor(version: OasVersion): OasMajorVersion;
package/lib/lint.js CHANGED
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.openAPIMajor = exports.detectOpenAPI = exports.lintDocument = exports.lint = exports.OasMajorVersion = exports.OasVersion = void 0;
12
+ exports.lintDocument = exports.lintFromString = exports.lint = void 0;
13
13
  const resolve_1 = require("./resolve");
14
14
  const visitors_1 = require("./visitors");
15
15
  const oas3_1_1 = require("./types/oas3_1");
@@ -19,17 +19,7 @@ const walk_1 = require("./walk");
19
19
  const types_1 = require("./types");
20
20
  const rules_1 = require("./config/rules");
21
21
  const ajv_1 = require("./rules/ajv");
22
- var OasVersion;
23
- (function (OasVersion) {
24
- OasVersion["Version2"] = "oas2";
25
- OasVersion["Version3_0"] = "oas3_0";
26
- OasVersion["Version3_1"] = "oas3_1";
27
- })(OasVersion = exports.OasVersion || (exports.OasVersion = {}));
28
- var OasMajorVersion;
29
- (function (OasMajorVersion) {
30
- OasMajorVersion["Version2"] = "oas2";
31
- OasMajorVersion["Version3"] = "oas3";
32
- })(OasMajorVersion = exports.OasMajorVersion || (exports.OasMajorVersion = {}));
22
+ const oas_types_1 = require("./oas-types");
33
23
  function lint(opts) {
34
24
  return __awaiter(this, void 0, void 0, function* () {
35
25
  const { ref, externalRefResolver = new resolve_1.BaseResolver(opts.config.resolve) } = opts;
@@ -38,14 +28,22 @@ function lint(opts) {
38
28
  });
39
29
  }
40
30
  exports.lint = lint;
31
+ function lintFromString(opts) {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ const { source, absoluteRef, externalRefResolver = new resolve_1.BaseResolver(opts.config.resolve) } = opts;
34
+ const document = resolve_1.makeDocumentFromString(source, absoluteRef || '/');
35
+ return lintDocument(Object.assign(Object.assign({ document }, opts), { externalRefResolver, config: opts.config.lint }));
36
+ });
37
+ }
38
+ exports.lintFromString = lintFromString;
41
39
  function lintDocument(opts) {
42
40
  return __awaiter(this, void 0, void 0, function* () {
43
41
  ajv_1.releaseAjvInstance(); // FIXME: preprocessors can modify nodes which are then cached to ajv-instance by absolute path
44
42
  const { document, customTypes, externalRefResolver, config } = opts;
45
- const oasVersion = detectOpenAPI(document.parsed);
46
- const oasMajorVersion = openAPIMajor(oasVersion);
43
+ const oasVersion = oas_types_1.detectOpenAPI(document.parsed);
44
+ const oasMajorVersion = oas_types_1.openAPIMajor(oasVersion);
47
45
  const rules = config.getRulesForOasVersion(oasMajorVersion);
48
- const types = types_1.normalizeTypes(config.extendTypes((customTypes !== null && customTypes !== void 0 ? customTypes : oasMajorVersion === OasMajorVersion.Version3) ? (oasVersion === OasVersion.Version3_1 ? oas3_1_1.Oas3_1Types : oas3_1.Oas3Types) : oas2_1.Oas2Types, oasVersion), config);
46
+ const types = types_1.normalizeTypes(config.extendTypes((customTypes !== null && customTypes !== void 0 ? customTypes : oasMajorVersion === oas_types_1.OasMajorVersion.Version3) ? (oasVersion === oas_types_1.OasVersion.Version3_1 ? oas3_1_1.Oas3_1Types : oas3_1.Oas3Types) : oas2_1.Oas2Types, oasVersion), config);
49
47
  const ctx = {
50
48
  problems: [],
51
49
  oasVersion: oasVersion,
@@ -69,31 +67,3 @@ function lintDocument(opts) {
69
67
  });
70
68
  }
71
69
  exports.lintDocument = lintDocument;
72
- function detectOpenAPI(root) {
73
- if (typeof root !== 'object') {
74
- throw new Error(`Document must be JSON object, got ${typeof root}`);
75
- }
76
- if (!(root.openapi || root.swagger)) {
77
- throw new Error('This doesn’t look like an OpenAPI document.\n');
78
- }
79
- if (root.openapi && root.openapi.startsWith('3.0')) {
80
- return OasVersion.Version3_0;
81
- }
82
- if (root.openapi && root.openapi.startsWith('3.1')) {
83
- return OasVersion.Version3_1;
84
- }
85
- if (root.swagger && root.swagger === '2.0') {
86
- return OasVersion.Version2;
87
- }
88
- throw new Error(`Unsupported OpenAPI Version: ${root.openapi || root.swagger}`);
89
- }
90
- exports.detectOpenAPI = detectOpenAPI;
91
- function openAPIMajor(version) {
92
- if (version === OasVersion.Version2) {
93
- return OasMajorVersion.Version2;
94
- }
95
- else {
96
- return OasMajorVersion.Version3;
97
- }
98
- }
99
- exports.openAPIMajor = openAPIMajor;
@@ -0,0 +1,19 @@
1
+ import { Oas3Rule, Oas3Preprocessor, Oas2Rule, Oas2Preprocessor } from './visitors';
2
+ export declare type RuleSet<T> = Record<string, T>;
3
+ export declare enum OasVersion {
4
+ Version2 = "oas2",
5
+ Version3_0 = "oas3_0",
6
+ Version3_1 = "oas3_1"
7
+ }
8
+ export declare enum OasMajorVersion {
9
+ Version2 = "oas2",
10
+ Version3 = "oas3"
11
+ }
12
+ export declare type Oas3RuleSet = Record<string, Oas3Rule>;
13
+ export declare type Oas2RuleSet = Record<string, Oas2Rule>;
14
+ export declare type Oas3PreprocessorsSet = Record<string, Oas3Preprocessor>;
15
+ export declare type Oas2PreprocessorsSet = Record<string, Oas2Preprocessor>;
16
+ export declare type Oas3DecoratorsSet = Record<string, Oas3Preprocessor>;
17
+ export declare type Oas2DecoratorsSet = Record<string, Oas2Preprocessor>;
18
+ export declare function detectOpenAPI(root: any): OasVersion;
19
+ export declare function openAPIMajor(version: OasVersion): OasMajorVersion;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.openAPIMajor = exports.detectOpenAPI = exports.OasMajorVersion = exports.OasVersion = void 0;
4
+ var OasVersion;
5
+ (function (OasVersion) {
6
+ OasVersion["Version2"] = "oas2";
7
+ OasVersion["Version3_0"] = "oas3_0";
8
+ OasVersion["Version3_1"] = "oas3_1";
9
+ })(OasVersion = exports.OasVersion || (exports.OasVersion = {}));
10
+ var OasMajorVersion;
11
+ (function (OasMajorVersion) {
12
+ OasMajorVersion["Version2"] = "oas2";
13
+ OasMajorVersion["Version3"] = "oas3";
14
+ })(OasMajorVersion = exports.OasMajorVersion || (exports.OasMajorVersion = {}));
15
+ function detectOpenAPI(root) {
16
+ if (typeof root !== 'object') {
17
+ throw new Error(`Document must be JSON object, got ${typeof root}`);
18
+ }
19
+ if (!(root.openapi || root.swagger)) {
20
+ throw new Error('This doesn’t look like an OpenAPI document.\n');
21
+ }
22
+ if (root.openapi && root.openapi.startsWith('3.0')) {
23
+ return OasVersion.Version3_0;
24
+ }
25
+ if (root.openapi && root.openapi.startsWith('3.1')) {
26
+ return OasVersion.Version3_1;
27
+ }
28
+ if (root.swagger && root.swagger === '2.0') {
29
+ return OasVersion.Version2;
30
+ }
31
+ throw new Error(`Unsupported OpenAPI Version: ${root.openapi || root.swagger}`);
32
+ }
33
+ exports.detectOpenAPI = detectOpenAPI;
34
+ function openAPIMajor(version) {
35
+ if (version === OasVersion.Version2) {
36
+ return OasMajorVersion.Version2;
37
+ }
38
+ else {
39
+ return OasMajorVersion.Version3;
40
+ }
41
+ }
42
+ exports.openAPIMajor = openAPIMajor;
package/lib/resolve.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { YAMLNode } from 'yaml-ast-parser';
1
+ import type { YAMLNode, LoadOptions } from 'yaml-ast-parser';
2
2
  import { NormalizedNodeType } from './types';
3
3
  import { ResolveConfig } from './config/config';
4
4
  export declare type CollectedRefs = Map<string, Document>;
@@ -9,7 +9,7 @@ export declare class Source {
9
9
  constructor(absoluteRef: string, body: string, mimeType?: string | undefined);
10
10
  private _ast;
11
11
  private _lines;
12
- getAst(): YAMLNode;
12
+ getAst(safeLoad: (input: string, options?: LoadOptions | undefined) => YAMLNode): YAMLNode;
13
13
  getLines(): string[];
14
14
  }
15
15
  export declare class ResolveError extends Error {
@@ -27,6 +27,10 @@ export declare type Document = {
27
27
  source: Source;
28
28
  parsed: any;
29
29
  };
30
+ export declare function makeDocumentFromString(sourceString: string, absoluteRef: string): {
31
+ source: Source;
32
+ parsed: string | object | undefined;
33
+ };
30
34
  export declare class BaseResolver {
31
35
  private config;
32
36
  cache: Map<string, Promise<Document | ResolveError>>;
package/lib/resolve.js CHANGED
@@ -9,13 +9,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.resolveDocument = exports.BaseResolver = exports.YamlParseError = exports.ResolveError = exports.Source = void 0;
12
+ exports.resolveDocument = exports.BaseResolver = exports.makeDocumentFromString = exports.YamlParseError = exports.ResolveError = exports.Source = void 0;
13
13
  const fs = require("fs");
14
14
  const path = require("path");
15
15
  const url = require("url");
16
16
  const yaml = require("js-yaml");
17
17
  const ref_utils_1 = require("./ref-utils");
18
- const yaml_ast_parser_1 = require("yaml-ast-parser");
19
18
  const types_1 = require("./types");
20
19
  const utils_1 = require("./utils");
21
20
  class Source {
@@ -24,13 +23,14 @@ class Source {
24
23
  this.body = body;
25
24
  this.mimeType = mimeType;
26
25
  }
27
- getAst() {
26
+ // pass safeLoad as argument to separate it from browser bundle
27
+ getAst(safeLoad) {
28
28
  var _a;
29
29
  if (this._ast === undefined) {
30
- this._ast = (_a = yaml_ast_parser_1.safeLoad(this.body, { filename: this.absoluteRef })) !== null && _a !== void 0 ? _a : undefined;
30
+ this._ast = (_a = safeLoad(this.body, { filename: this.absoluteRef })) !== null && _a !== void 0 ? _a : undefined;
31
31
  // fix ast representation of file with newlines only
32
32
  if (this._ast &&
33
- this._ast.kind === yaml_ast_parser_1.Kind.SCALAR &&
33
+ this._ast.kind === 0 && // KIND.scalar = 0
34
34
  this._ast.value === '' &&
35
35
  this._ast.startPosition !== 1) {
36
36
  this._ast.startPosition = 1;
@@ -70,6 +70,19 @@ class YamlParseError extends Error {
70
70
  }
71
71
  }
72
72
  exports.YamlParseError = YamlParseError;
73
+ function makeDocumentFromString(sourceString, absoluteRef) {
74
+ const source = new Source(absoluteRef, sourceString);
75
+ try {
76
+ return {
77
+ source,
78
+ parsed: yaml.safeLoad(sourceString, { filename: absoluteRef }),
79
+ };
80
+ }
81
+ catch (e) {
82
+ throw new YamlParseError(e, source);
83
+ }
84
+ }
85
+ exports.makeDocumentFromString = makeDocumentFromString;
73
86
  class BaseResolver {
74
87
  constructor(config = { http: { headers: [] } }) {
75
88
  this.config = config;
@@ -1,10 +1,10 @@
1
- import * as Ajv from '@redocly/ajv';
1
+ import { ErrorObject } from '@redocly/ajv';
2
2
  import { Location } from '../ref-utils';
3
3
  import { ResolveFn } from '../walk';
4
4
  export declare function releaseAjvInstance(): void;
5
- export declare function validateJsonSchema(data: any, schema: any, schemaLoc: Location, dataPath: string, resolve: ResolveFn<any>, disallowAdditionalProperties: boolean): {
5
+ export declare function validateJsonSchema(data: any, schema: any, schemaLoc: Location, instancePath: string, resolve: ResolveFn<any>, disallowAdditionalProperties: boolean): {
6
6
  valid: boolean;
7
- errors: (Ajv.ErrorObject & {
7
+ errors: (ErrorObject & {
8
8
  suggest?: string[];
9
9
  })[];
10
10
  };
package/lib/rules/ajv.js CHANGED
@@ -1,10 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.validateJsonSchema = exports.releaseAjvInstance = void 0;
4
- // @ts-ignore
5
- const Ajv = require("@redocly/ajv");
6
- // import * as jsonSpecV4 from 'ajv/lib/refs/json-schema-draft-04.json';
7
- // import { OasVersion } from '../validate';
4
+ const ajv_1 = require("@redocly/ajv");
8
5
  const ref_utils_1 = require("../ref-utils");
9
6
  let ajvInstance = null;
10
7
  function releaseAjvInstance() {
@@ -13,22 +10,22 @@ function releaseAjvInstance() {
13
10
  exports.releaseAjvInstance = releaseAjvInstance;
14
11
  function getAjv(resolve, disallowAdditionalProperties) {
15
12
  if (!ajvInstance) {
16
- ajvInstance = new Ajv({
17
- schemaId: 'auto',
13
+ ajvInstance = new ajv_1.default({
14
+ schemaId: '$id',
18
15
  meta: true,
19
16
  allErrors: true,
20
- jsonPointers: true,
21
- unknownFormats: 'ignore',
22
- nullable: true,
23
- missingRefs: 'ignore',
17
+ strictSchema: false,
24
18
  inlineRefs: false,
25
19
  validateSchema: false,
20
+ discriminator: true,
21
+ allowUnionTypes: true,
22
+ validateFormats: false,
26
23
  defaultAdditionalProperties: !disallowAdditionalProperties,
27
- loadSchemaSync(base, $ref, id) {
28
- const resolvedRef = resolve({ $ref }, base.replace(/#$/, ''));
24
+ loadSchemaSync(base, $ref) {
25
+ const resolvedRef = resolve({ $ref }, base.split('#')[0]);
29
26
  if (!resolvedRef || !resolvedRef.location)
30
27
  return undefined;
31
- return Object.assign({ id }, resolvedRef.node);
28
+ return Object.assign({ $id: resolvedRef.location.absolutePointer }, resolvedRef.node);
32
29
  },
33
30
  logger: false,
34
31
  });
@@ -38,15 +35,21 @@ function getAjv(resolve, disallowAdditionalProperties) {
38
35
  function getAjvValidator(schema, loc, resolve, disallowAdditionalProperties) {
39
36
  const ajv = getAjv(resolve, disallowAdditionalProperties);
40
37
  if (!ajv.getSchema(loc.absolutePointer)) {
41
- ajv.addSchema(Object.assign({ id: loc.absolutePointer }, schema), loc.absolutePointer);
38
+ ajv.addSchema(Object.assign({ $id: loc.absolutePointer }, schema), loc.absolutePointer);
42
39
  }
43
40
  return ajv.getSchema(loc.absolutePointer);
44
41
  }
45
- function validateJsonSchema(data, schema, schemaLoc, dataPath, resolve, disallowAdditionalProperties) {
42
+ function validateJsonSchema(data, schema, schemaLoc, instancePath, resolve, disallowAdditionalProperties) {
46
43
  const validate = getAjvValidator(schema, schemaLoc, resolve, disallowAdditionalProperties);
47
44
  if (!validate)
48
45
  return { valid: true, errors: [] }; // unresolved refs are reported
49
- const valid = validate(data, dataPath);
46
+ const valid = validate(data, {
47
+ instancePath,
48
+ parentData: { fake: {} },
49
+ parentDataProperty: 'fake',
50
+ rootData: {},
51
+ dynamicAnchors: {},
52
+ });
50
53
  return {
51
54
  valid: !!valid,
52
55
  errors: (validate.errors || []).map(beatifyErrorMessage),
@@ -60,7 +63,7 @@ function validateJsonSchema(data, schema, schemaLoc, dataPath, resolve, disallow
60
63
  if (error.keyword === 'type') {
61
64
  message = `type ${message}`;
62
65
  }
63
- const relativePath = error.dataPath.substring(dataPath.length + 1);
66
+ const relativePath = error.instancePath.substring(instancePath.length + 1);
64
67
  const propName = relativePath.substring(relativePath.lastIndexOf('/') + 1);
65
68
  if (propName) {
66
69
  message = `\`${propName}\` property ${message}`;
@@ -68,7 +71,7 @@ function validateJsonSchema(data, schema, schemaLoc, dataPath, resolve, disallow
68
71
  if (error.keyword === 'additionalProperties') {
69
72
  const property = error.params.additionalProperty;
70
73
  message = `${message} \`${property}\``;
71
- error.dataPath += '/' + ref_utils_1.escapePointer(property);
74
+ error.instancePath += '/' + ref_utils_1.escapePointer(property);
72
75
  }
73
76
  return Object.assign(Object.assign({}, error), { message,
74
77
  suggest });
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InfoContact = void 0;
4
4
  const utils_1 = require("../utils");
5
- exports.InfoContact = () => {
5
+ const InfoContact = () => {
6
6
  return {
7
7
  Info(info, { report, location }) {
8
8
  if (!info.contact) {
@@ -14,3 +14,4 @@ exports.InfoContact = () => {
14
14
  },
15
15
  };
16
16
  };
17
+ exports.InfoContact = InfoContact;
@@ -2,10 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InfoDescription = void 0;
4
4
  const utils_1 = require("../utils");
5
- exports.InfoDescription = () => {
5
+ const InfoDescription = () => {
6
6
  return {
7
7
  Info(info, ctx) {
8
8
  utils_1.validateDefinedAndNonEmpty('description', info, ctx);
9
9
  },
10
10
  };
11
11
  };
12
+ exports.InfoDescription = InfoDescription;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InfoLicense = void 0;
4
4
  const utils_1 = require("../utils");
5
- exports.InfoLicense = () => {
5
+ const InfoLicense = () => {
6
6
  return {
7
7
  Info(info, { report }) {
8
8
  if (!info.license) {
@@ -13,3 +13,4 @@ exports.InfoLicense = () => {
13
13
  },
14
14
  };
15
15
  };
16
+ exports.InfoLicense = InfoLicense;
@@ -2,10 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InfoLicenseUrl = void 0;
4
4
  const utils_1 = require("../utils");
5
- exports.InfoLicenseUrl = () => {
5
+ const InfoLicenseUrl = () => {
6
6
  return {
7
7
  License(license, ctx) {
8
8
  utils_1.validateDefinedAndNonEmpty('url', license, ctx);
9
9
  },
10
10
  };
11
11
  };
12
+ exports.InfoLicenseUrl = InfoLicenseUrl;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NoAmbiguousPaths = void 0;
4
- exports.NoAmbiguousPaths = () => {
4
+ const NoAmbiguousPaths = () => {
5
5
  return {
6
6
  PathMap(pathMap, { report, location }) {
7
7
  const seenPaths = [];
@@ -18,6 +18,7 @@ exports.NoAmbiguousPaths = () => {
18
18
  },
19
19
  };
20
20
  };
21
+ exports.NoAmbiguousPaths = NoAmbiguousPaths;
21
22
  function arePathsAmbiguous(a, b) {
22
23
  const partsA = a.split('/');
23
24
  const partsB = b.split('/');
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NoEnumTypeMismatch = void 0;
4
4
  const utils_1 = require("../utils");
5
- exports.NoEnumTypeMismatch = () => {
5
+ const NoEnumTypeMismatch = () => {
6
6
  return {
7
7
  Schema(schema, { report, location }) {
8
8
  if (schema.enum && schema.type && !Array.isArray(schema.type)) {
@@ -38,3 +38,4 @@ exports.NoEnumTypeMismatch = () => {
38
38
  },
39
39
  };
40
40
  };
41
+ exports.NoEnumTypeMismatch = NoEnumTypeMismatch;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NoIdenticalPaths = void 0;
4
- exports.NoIdenticalPaths = () => {
4
+ const NoIdenticalPaths = () => {
5
5
  return {
6
6
  PathMap(pathMap, { report, location }) {
7
7
  const pathsMap = new Map();
@@ -21,3 +21,4 @@ exports.NoIdenticalPaths = () => {
21
21
  },
22
22
  };
23
23
  };
24
+ exports.NoIdenticalPaths = NoIdenticalPaths;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NoPathTrailingSlash = void 0;
4
- exports.NoPathTrailingSlash = () => {
4
+ const NoPathTrailingSlash = () => {
5
5
  return {
6
6
  PathItem(_path, { report, key, location }) {
7
7
  if (key.endsWith('/') && key !== '/') {
@@ -13,3 +13,4 @@ exports.NoPathTrailingSlash = () => {
13
13
  },
14
14
  };
15
15
  };
16
+ exports.NoPathTrailingSlash = NoPathTrailingSlash;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Operation2xxResponse = void 0;
4
- exports.Operation2xxResponse = () => {
4
+ const Operation2xxResponse = () => {
5
5
  return {
6
6
  ResponsesMap(responses, { report }) {
7
7
  const codes = Object.keys(responses);
@@ -14,3 +14,4 @@ exports.Operation2xxResponse = () => {
14
14
  },
15
15
  };
16
16
  };
17
+ exports.Operation2xxResponse = Operation2xxResponse;
@@ -2,10 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OperationDescription = void 0;
4
4
  const utils_1 = require("../utils");
5
- exports.OperationDescription = () => {
5
+ const OperationDescription = () => {
6
6
  return {
7
7
  Operation(operation, ctx) {
8
8
  utils_1.validateDefinedAndNonEmpty('description', operation, ctx);
9
9
  },
10
10
  };
11
11
  };
12
+ exports.OperationDescription = OperationDescription;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OperationIdUnique = void 0;
4
- exports.OperationIdUnique = () => {
4
+ const OperationIdUnique = () => {
5
5
  const seenOperations = new Set();
6
6
  return {
7
7
  Operation(operation, { report, location }) {
@@ -17,3 +17,4 @@ exports.OperationIdUnique = () => {
17
17
  },
18
18
  };
19
19
  };
20
+ exports.OperationIdUnique = OperationIdUnique;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OperationIdUrlSafe = void 0;
4
4
  const validUrlSymbols = /^[A-Za-z0-9-._~:/?#\[\]@!\$&'()*+,;=]*$/;
5
- exports.OperationIdUrlSafe = () => {
5
+ const OperationIdUrlSafe = () => {
6
6
  return {
7
7
  Operation(operation, { report, location }) {
8
8
  if (operation.operationId && !validUrlSymbols.test(operation.operationId)) {
@@ -14,3 +14,4 @@ exports.OperationIdUrlSafe = () => {
14
14
  },
15
15
  };
16
16
  };
17
+ exports.OperationIdUrlSafe = OperationIdUrlSafe;
@@ -2,10 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OperationOperationId = void 0;
4
4
  const utils_1 = require("../utils");
5
- exports.OperationOperationId = () => {
5
+ const OperationOperationId = () => {
6
6
  return {
7
- Operation(operation, ctx) {
8
- utils_1.validateDefinedAndNonEmpty('operationId', operation, ctx);
9
- },
7
+ DefinitionRoot: {
8
+ PathItem: {
9
+ Operation(operation, ctx) {
10
+ utils_1.validateDefinedAndNonEmpty('operationId', operation, ctx);
11
+ },
12
+ },
13
+ }
10
14
  };
11
15
  };
16
+ exports.OperationOperationId = OperationOperationId;