@skmtc/core 0.33.0 → 0.35.0

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 (93) hide show
  1. package/esm/context/CoreContext.d.ts +8 -21
  2. package/esm/context/CoreContext.js +9 -22
  3. package/esm/context/GenerateContext.js +7 -7
  4. package/esm/context/ParseContext.d.ts +1 -1
  5. package/esm/context/ParseContext.js +2 -2
  6. package/esm/context/StackTrail.d.ts +30 -13
  7. package/esm/context/StackTrail.js +36 -14
  8. package/esm/context/generateTypes.d.ts +2 -2
  9. package/esm/context/generateTypes.js +5 -0
  10. package/esm/helpers/ResultsLog.d.ts +19 -181
  11. package/esm/helpers/ResultsLog.js +46 -205
  12. package/esm/parse/descriptiveFields.d.ts +36 -0
  13. package/esm/parse/descriptiveFields.js +56 -0
  14. package/esm/parse/isPlainObject.d.ts +2 -0
  15. package/esm/parse/isPlainObject.js +2 -0
  16. package/esm/parse/toComponentEntries.d.ts +29 -0
  17. package/esm/parse/toComponentEntries.js +31 -0
  18. package/esm/parse/toPresentInfo.d.ts +30 -0
  19. package/esm/parse/toPresentInfo.js +40 -0
  20. package/esm/parse/v3-0/components/toComponentsV3.js +2 -2
  21. package/esm/parse/v3-0/externalDocs/toExternalDocs.d.ts +3 -1
  22. package/esm/parse/v3-0/externalDocs/toExternalDocs.js +9 -8
  23. package/esm/parse/v3-0/header/toHeadersV3.d.ts +12 -0
  24. package/esm/parse/v3-0/header/toHeadersV3.js +20 -0
  25. package/esm/parse/v3-0/info/toInfoV3.d.ts +2 -1
  26. package/esm/parse/v3-0/info/toInfoV3.js +15 -4
  27. package/esm/parse/v3-0/requestBody/toRequestBodiesV3.js +9 -9
  28. package/esm/parse/v3-0/tag/toTagsV3.js +7 -2
  29. package/esm/parse/v3-1/components/toComponentsV3.js +2 -2
  30. package/esm/parse/v3-1/externalDocs/toExternalDocs.d.ts +3 -1
  31. package/esm/parse/v3-1/externalDocs/toExternalDocs.js +9 -8
  32. package/esm/parse/v3-1/header/toHeadersV3.d.ts +12 -0
  33. package/esm/parse/v3-1/header/toHeadersV3.js +20 -0
  34. package/esm/parse/v3-1/info/toInfoV3.d.ts +2 -1
  35. package/esm/parse/v3-1/info/toInfoV3.js +15 -4
  36. package/esm/parse/v3-1/requestBody/toRequestBodiesV3.js +9 -9
  37. package/esm/parse/v3-1/tag/toTagsV3.js +7 -2
  38. package/esm/parse/v3-2/components/toComponentsV3.js +2 -2
  39. package/esm/parse/v3-2/externalDocs/toExternalDocs.d.ts +3 -1
  40. package/esm/parse/v3-2/externalDocs/toExternalDocs.js +9 -8
  41. package/esm/parse/v3-2/header/toHeadersV3.d.ts +12 -0
  42. package/esm/parse/v3-2/header/toHeadersV3.js +20 -0
  43. package/esm/parse/v3-2/info/toInfoV3.d.ts +2 -1
  44. package/esm/parse/v3-2/info/toInfoV3.js +15 -4
  45. package/esm/parse/v3-2/requestBody/toRequestBodiesV3.js +9 -9
  46. package/esm/parse/v3-2/tag/toTagsV3.js +14 -7
  47. package/package.json +1 -1
  48. package/script/context/CoreContext.d.ts +8 -21
  49. package/script/context/CoreContext.js +9 -22
  50. package/script/context/GenerateContext.js +7 -7
  51. package/script/context/ParseContext.d.ts +1 -1
  52. package/script/context/ParseContext.js +2 -2
  53. package/script/context/StackTrail.d.ts +30 -13
  54. package/script/context/StackTrail.js +36 -14
  55. package/script/context/generateTypes.d.ts +2 -2
  56. package/script/context/generateTypes.js +5 -0
  57. package/script/helpers/ResultsLog.d.ts +19 -181
  58. package/script/helpers/ResultsLog.js +46 -208
  59. package/script/parse/descriptiveFields.d.ts +36 -0
  60. package/script/parse/descriptiveFields.js +95 -0
  61. package/script/parse/isPlainObject.d.ts +2 -0
  62. package/script/parse/isPlainObject.js +6 -0
  63. package/script/parse/toComponentEntries.d.ts +29 -0
  64. package/script/parse/toComponentEntries.js +35 -0
  65. package/script/parse/toPresentInfo.d.ts +30 -0
  66. package/script/parse/toPresentInfo.js +45 -0
  67. package/script/parse/v3-0/components/toComponentsV3.js +1 -1
  68. package/script/parse/v3-0/externalDocs/toExternalDocs.d.ts +3 -1
  69. package/script/parse/v3-0/externalDocs/toExternalDocs.js +9 -41
  70. package/script/parse/v3-0/header/toHeadersV3.d.ts +12 -0
  71. package/script/parse/v3-0/header/toHeadersV3.js +22 -1
  72. package/script/parse/v3-0/info/toInfoV3.d.ts +2 -1
  73. package/script/parse/v3-0/info/toInfoV3.js +15 -4
  74. package/script/parse/v3-0/requestBody/toRequestBodiesV3.js +9 -9
  75. package/script/parse/v3-0/tag/toTagsV3.js +7 -2
  76. package/script/parse/v3-1/components/toComponentsV3.js +1 -1
  77. package/script/parse/v3-1/externalDocs/toExternalDocs.d.ts +3 -1
  78. package/script/parse/v3-1/externalDocs/toExternalDocs.js +9 -41
  79. package/script/parse/v3-1/header/toHeadersV3.d.ts +12 -0
  80. package/script/parse/v3-1/header/toHeadersV3.js +22 -1
  81. package/script/parse/v3-1/info/toInfoV3.d.ts +2 -1
  82. package/script/parse/v3-1/info/toInfoV3.js +15 -4
  83. package/script/parse/v3-1/requestBody/toRequestBodiesV3.js +9 -9
  84. package/script/parse/v3-1/tag/toTagsV3.js +7 -2
  85. package/script/parse/v3-2/components/toComponentsV3.js +1 -1
  86. package/script/parse/v3-2/externalDocs/toExternalDocs.d.ts +3 -1
  87. package/script/parse/v3-2/externalDocs/toExternalDocs.js +9 -41
  88. package/script/parse/v3-2/header/toHeadersV3.d.ts +12 -0
  89. package/script/parse/v3-2/header/toHeadersV3.js +22 -1
  90. package/script/parse/v3-2/info/toInfoV3.d.ts +2 -1
  91. package/script/parse/v3-2/info/toInfoV3.js +15 -4
  92. package/script/parse/v3-2/requestBody/toRequestBodiesV3.js +9 -9
  93. package/script/parse/v3-2/tag/toTagsV3.js +14 -7
@@ -10,216 +10,69 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
12
  var _ResultsLog_instances, _ResultsLog_results, _ResultsLog_incomingResultIsWorse;
13
- import set from 'lodash-es/set.js';
14
13
  /**
15
- * Manages and organizes generation results with intelligent prioritization.
14
+ * Records one result per stack trail and builds the `manifest.results` tree.
16
15
  *
17
- * `ResultsLog` tracks the outcomes of code generation operations, maintaining
18
- * a hierarchical structure of results organized by stack trail paths. It implements
19
- * intelligent result prioritization, ensuring that more severe results (errors)
20
- * take precedence over less severe ones (success, warnings) when multiple results
21
- * exist for the same path.
16
+ * When several results are captured at the same trail, the most severe one
17
+ * wins (see `resultRankings`).
22
18
  *
23
- * The class is designed to work with the SKMTC tracing system, using stack trail
24
- * identifiers to organize results in a meaningful hierarchical structure that
25
- * reflects the execution flow of the generation process.
26
- *
27
- * ## Key Features
28
- *
29
- * - **Result Prioritization**: Automatically prioritizes more severe results
30
- * - **Hierarchical Organization**: Organizes results by stack trail paths
31
- * - **Tree Structure**: Converts flat result logs into nested tree structures
32
- * - **Duplicate Handling**: Prevents less severe results from overwriting more severe ones
33
- *
34
- * @example Basic usage
19
+ * @example
35
20
  * ```typescript
36
- * import { ResultsLog } from '@skmtc/core';
37
- *
38
- * const resultsLog = new ResultsLog();
39
- *
40
- * // Capture results from different parts of generation
41
- * resultsLog.capture('models:User', 'success');
42
- * resultsLog.capture('models:Product', 'error');
43
- * resultsLog.capture('types:Status', 'success');
44
- *
45
- * // Convert to hierarchical structure
46
- * const tree = resultsLog.toTree();
47
- * console.log(tree);
48
- * // {
49
- * // models: {
50
- * // User: 'success',
51
- * // Product: 'error'
52
- * // },
53
- * // types: {
54
- * // Status: 'success'
55
- * // }
56
- * // }
57
- * ```
58
- *
59
- * @example Result prioritization
60
- * ```typescript
61
- * const resultsLog = new ResultsLog();
62
- *
63
- * // First capture a success
64
- * resultsLog.capture('api:createUser', 'success');
65
- *
66
- * // Later encounter an error at the same path
67
- * resultsLog.capture('api:createUser', 'error');
21
+ * const resultsLog = new ResultsLog()
68
22
  *
69
- * // Error takes precedence
70
- * const tree = resultsLog.toTree();
71
- * console.log(tree.api.createUser); // 'error'
23
+ * resultsLog.capture(new StackTrail(['generate', '/pets', 'get']), 'success')
24
+ * resultsLog.capture(new StackTrail(['generate', '/pets/{id}:archive', 'post']), 'error')
72
25
  *
73
- * // But success won't overwrite error
74
- * resultsLog.capture('api:createUser', 'success');
75
- * console.log(resultsLog.toTree().api.createUser); // Still 'error'
76
- * ```
77
- *
78
- * @example Complex nested paths
79
- * ```typescript
80
- * const resultsLog = new ResultsLog();
81
- *
82
- * resultsLog.capture('generation:models:User:properties:name', 'success');
83
- * resultsLog.capture('generation:models:User:properties:email', 'warning');
84
- * resultsLog.capture('generation:models:Product:validation', 'error');
85
- * resultsLog.capture('generation:operations:createUser', 'success');
86
- *
87
- * const tree = resultsLog.toTree();
88
- * console.log(tree);
89
- * // {
90
- * // generation: {
91
- * // models: {
92
- * // User: {
93
- * // properties: {
94
- * // name: 'success',
95
- * // email: 'warning'
96
- * // }
97
- * // },
98
- * // Product: {
99
- * // validation: 'error'
100
- * // }
101
- * // },
102
- * // operations: {
103
- * // createUser: 'success'
104
- * // }
105
- * // }
106
- * // }
107
- * ```
108
- *
109
- * @example Integration with tracing
110
- * ```typescript
111
- * class TrackedGenerator {
112
- * private resultsLog = new ResultsLog();
113
- *
114
- * generateModel(name: string, schema: OasSchema) {
115
- * const stackPath = `models:${name}`;
116
- *
117
- * try {
118
- * const model = this.processSchema(schema);
119
- * this.resultsLog.capture(stackPath, 'success');
120
- * return model;
121
- * } catch (error) {
122
- * this.resultsLog.capture(stackPath, 'error');
123
- * throw error;
124
- * }
125
- * }
126
- *
127
- * generateOperation(operationId: string, operation: OasOperation) {
128
- * const stackPath = `operations:${operationId}`;
129
- *
130
- * if (!this.supportsOperation(operation)) {
131
- * this.resultsLog.capture(stackPath, 'notSupported');
132
- * return null;
133
- * }
134
- *
135
- * const result = this.processOperation(operation);
136
- * this.resultsLog.capture(stackPath, 'success');
137
- * return result;
138
- * }
139
- *
140
- * getResults(): ResultsItem {
141
- * return this.resultsLog.toTree();
142
- * }
143
- * }
26
+ * resultsLog.toTree()
27
+ * // { generate: { '/pets': { get: 'success' }, '/pets/{id}:archive': { post: 'error' } } }
144
28
  * ```
145
29
  */
146
30
  export class ResultsLog {
147
- /**
148
- * Creates a new ResultsLog instance.
149
- *
150
- * Initializes an empty results tracking system ready to capture
151
- * generation outcomes from various parts of the pipeline.
152
- */
153
31
  constructor() {
154
32
  _ResultsLog_instances.add(this);
33
+ /**
34
+ * Keyed by `StackTrail.toString()`. The key is one-to-one with the frames,
35
+ * except that `[]` and `['']` both give `''`.
36
+ */
155
37
  _ResultsLog_results.set(this, void 0);
156
- __classPrivateFieldSet(this, _ResultsLog_results, {}, "f");
38
+ __classPrivateFieldSet(this, _ResultsLog_results, new Map(), "f");
157
39
  }
158
40
  /**
159
- * Captures a generation result for a specific stack trail path.
160
- *
161
- * This method records the outcome of a generation operation, using intelligent
162
- * prioritization to ensure that more severe results take precedence over less
163
- * severe ones. For example, an error result will not be overwritten by a
164
- * success result, but a success can be overwritten by an error.
165
- *
166
- * @param stackTrail - Colon-separated path identifying the operation location
167
- * @param result - The result type to capture (error, warning, success, etc.)
168
- *
169
- * @example
170
- * ```typescript
171
- * const resultsLog = new ResultsLog();
41
+ * Captures `result` at `stackTrail`, unless a more severe result is
42
+ * already recorded there.
172
43
  *
173
- * // Capture various results
174
- * resultsLog.capture('models:User:properties:email', 'success');
175
- * resultsLog.capture('models:User:validation', 'warning');
176
- * resultsLog.capture('operations:createUser', 'error');
177
- * ```
44
+ * Stores a copy of the frames: `StackTrail.trace` pops the live array
45
+ * after its callback returns.
178
46
  */
179
47
  capture(stackTrail, result) {
180
- if (__classPrivateFieldGet(this, _ResultsLog_instances, "m", _ResultsLog_incomingResultIsWorse).call(this, __classPrivateFieldGet(this, _ResultsLog_results, "f")[stackTrail], result)) {
181
- __classPrivateFieldGet(this, _ResultsLog_results, "f")[stackTrail] = result;
48
+ const key = stackTrail.toString();
49
+ if (__classPrivateFieldGet(this, _ResultsLog_instances, "m", _ResultsLog_incomingResultIsWorse).call(this, __classPrivateFieldGet(this, _ResultsLog_results, "f").get(key)?.result, result)) {
50
+ __classPrivateFieldGet(this, _ResultsLog_results, "f").set(key, { frames: [...stackTrail.stackTrail], result });
182
51
  }
183
52
  }
184
53
  /**
185
- * Converts the flat results structure into a hierarchical tree.
186
- *
187
- * This method transforms the internal flat storage (using colon-separated paths)
188
- * into a nested object structure that reflects the hierarchy of generation
189
- * operations. This makes it easier to understand and process results in
190
- * a structured way.
191
- *
192
- * @returns Nested object structure representing the results hierarchy
193
- *
194
- * @example
195
- * ```typescript
196
- * const resultsLog = new ResultsLog();
197
- * resultsLog.capture('api:models:User', 'success');
198
- * resultsLog.capture('api:operations:getUser', 'error');
199
- * resultsLog.capture('types:Status', 'success');
200
- *
201
- * const tree = resultsLog.toTree();
202
- * console.log(tree);
203
- * // {
204
- * // api: {
205
- * // models: {
206
- * // User: 'success'
207
- * // },
208
- * // operations: {
209
- * // getUser: 'error'
210
- * // }
211
- * // },
212
- * // types: {
213
- * // Status: 'success'
214
- * // }
215
- * // }
216
- * ```
54
+ * Nests the captured results by their frames, one object level per frame.
55
+ * Frames are used as own-property keys verbatim, so a frame such as
56
+ * `constructor`, `toString` or `0` is a key like any other. A result
57
+ * captured at the empty trail has no key and is left out.
217
58
  */
218
59
  toTree() {
219
60
  const tree = {};
220
- Object.entries(__classPrivateFieldGet(this, _ResultsLog_results, "f")).forEach(([key, value]) => {
221
- const keys = key.split(':');
222
- set(tree, keys, value);
61
+ __classPrivateFieldGet(this, _ResultsLog_results, "f").forEach(({ frames, result }) => {
62
+ const leafFrame = frames.at(-1);
63
+ if (leafFrame === undefined) {
64
+ return;
65
+ }
66
+ const parent = frames.slice(0, -1).reduce((node, frame) => {
67
+ const existing = Object.hasOwn(node, frame) ? node[frame] : undefined;
68
+ if (typeof existing === 'object' && existing !== null && !Array.isArray(existing)) {
69
+ return existing;
70
+ }
71
+ const child = {};
72
+ defineOwn(node, frame, child);
73
+ return child;
74
+ }, tree);
75
+ defineOwn(parent, leafFrame, result);
223
76
  });
224
77
  return tree;
225
78
  }
@@ -230,25 +83,13 @@ _ResultsLog_results = new WeakMap(), _ResultsLog_instances = new WeakSet(), _Res
230
83
  }
231
84
  return resultRankings[current] < resultRankings[incoming];
232
85
  };
86
+ /** Writes an own, enumerable property, even for keys such as `__proto__`. */
87
+ const defineOwn = (node, key, value) => {
88
+ Object.defineProperty(node, key, { value, enumerable: true, writable: true, configurable: true });
89
+ };
233
90
  /**
234
- * Priority rankings for different result types.
235
- *
236
- * This mapping defines the severity hierarchy used by the ResultsLog to determine
237
- * which results should take precedence when multiple results are captured for the
238
- * same path. Higher numbers indicate higher severity/priority.
239
- *
240
- * The ranking system ensures that critical issues (errors) are never masked by
241
- * less important results (success), while still allowing for proper escalation
242
- * when more severe conditions are encountered.
243
- *
244
- * @example Ranking hierarchy (high to low priority)
245
- * ```
246
- * error (50) - Critical failures that prevent generation
247
- * warning (40) - Issues that allow generation but need attention
248
- * success (30) - Normal successful completion
249
- * skipped (20) - Operations that were intentionally bypassed
250
- * notSupported (10) - Features not yet implemented
251
- * ```
91
+ * Severity of each result type. A higher number replaces a lower one
92
+ * captured at the same trail.
252
93
  */
253
94
  const resultRankings = {
254
95
  error: 50,
@@ -0,0 +1,36 @@
1
+ import type { ParseContextType } from '../context/parseTypes.js';
2
+ import type { StackTrail } from '../context/StackTrail.js';
3
+ import { type OasExternalDocsData } from '../oas/externalDocs/externalDocsTypes.js';
4
+ export type ToTagListArgs<Tag> = {
5
+ tags: Tag[];
6
+ /** Trail that ends in the `tags` key. */
7
+ stackTrail: StackTrail;
8
+ context: ParseContextType;
9
+ };
10
+ /**
11
+ * Check the root `tags` field before its entries are parsed. Not an array:
12
+ * logs one `INVALID_TAG` warning and returns `undefined`, read as no tags.
13
+ */
14
+ export declare const toTagList: <Tag>({ tags, stackTrail, context }: ToTagListArgs<Tag>) => Tag[] | undefined;
15
+ export type IsValidTagArgs = {
16
+ tag: unknown;
17
+ /** Trail that ends in the entry's index. */
18
+ stackTrail: StackTrail;
19
+ context: ParseContextType;
20
+ };
21
+ /**
22
+ * A tag entry must be a plain object with a string `name`. Any other entry
23
+ * logs one `INVALID_TAG` warning, and the caller leaves it out.
24
+ */
25
+ export declare const isValidTag: ({ tag, stackTrail, context }: IsValidTagArgs) => boolean;
26
+ export type ToValidExternalDocsArgs = {
27
+ externalDocs: unknown;
28
+ /** Trail that ends in the `externalDocs` key. */
29
+ stackTrail: StackTrail;
30
+ context: ParseContextType;
31
+ };
32
+ /**
33
+ * Validate an `externalDocs` object. One that fails `oasExternalDocsData`
34
+ * logs one `INVALID_EXTERNAL_DOCS` warning and returns `undefined`.
35
+ */
36
+ export declare const toValidExternalDocs: ({ externalDocs, stackTrail, context }: ToValidExternalDocsArgs) => OasExternalDocsData | undefined;
@@ -0,0 +1,56 @@
1
+ import * as v from 'valibot';
2
+ import { isPlainObject } from './isPlainObject.js';
3
+ import { oasExternalDocsData } from '../oas/externalDocs/externalDocsTypes.js';
4
+ /**
5
+ * Check the root `tags` field before its entries are parsed. Not an array:
6
+ * logs one `INVALID_TAG` warning and returns `undefined`, read as no tags.
7
+ */
8
+ export const toTagList = ({ tags, stackTrail, context }) => {
9
+ if (Array.isArray(tags)) {
10
+ return tags;
11
+ }
12
+ context.logIssueNoKey({
13
+ stackTrail,
14
+ parent: tags,
15
+ level: 'warning',
16
+ type: 'INVALID_TAG',
17
+ message: '"tags" must be an array; left out'
18
+ });
19
+ return undefined;
20
+ };
21
+ /**
22
+ * A tag entry must be a plain object with a string `name`. Any other entry
23
+ * logs one `INVALID_TAG` warning, and the caller leaves it out.
24
+ */
25
+ export const isValidTag = ({ tag, stackTrail, context }) => {
26
+ if (isPlainObject(tag) && typeof tag.name === 'string') {
27
+ return true;
28
+ }
29
+ context.logIssueNoKey({
30
+ stackTrail,
31
+ parent: tag,
32
+ level: 'warning',
33
+ type: 'INVALID_TAG',
34
+ message: 'Tag is not an object with a string name; left out'
35
+ });
36
+ return false;
37
+ };
38
+ /**
39
+ * Validate an `externalDocs` object. One that fails `oasExternalDocsData`
40
+ * logs one `INVALID_EXTERNAL_DOCS` warning and returns `undefined`.
41
+ */
42
+ export const toValidExternalDocs = ({ externalDocs, stackTrail, context }) => {
43
+ const result = v.safeParse(oasExternalDocsData, externalDocs);
44
+ if (result.success) {
45
+ return result.output;
46
+ }
47
+ const reason = result.issues.map(({ message }) => message).join(', ');
48
+ context.logIssueNoKey({
49
+ stackTrail,
50
+ parent: externalDocs,
51
+ level: 'warning',
52
+ type: 'INVALID_EXTERNAL_DOCS',
53
+ message: `External docs object is invalid (${reason}); left out`
54
+ });
55
+ return undefined;
56
+ };
@@ -0,0 +1,2 @@
1
+ /** A non-null object that is not an array. */
2
+ export declare const isPlainObject: (value: unknown) => value is Record<string, unknown>;
@@ -0,0 +1,2 @@
1
+ /** A non-null object that is not an array. */
2
+ export const isPlainObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
@@ -0,0 +1,29 @@
1
+ import type { ParseContextType } from '../context/parseTypes.js';
2
+ import type { StackTrail } from '../context/StackTrail.js';
3
+ import type { OasIssueType } from '../context/generateTypes.js';
4
+ export type ToComponentEntriesArgs<Entry, Parsed> = {
5
+ entries: Record<string, Entry>;
6
+ /** Trail that ends in the bucket key, e.g. `components:headers`. */
7
+ stackTrail: StackTrail;
8
+ context: ParseContextType;
9
+ /** Issue type logged for an entry that fails to parse. */
10
+ type: OasIssueType;
11
+ /** Names the entry in the message, e.g. `Request body`. */
12
+ label: string;
13
+ /** Parses one entry. An `undefined` result leaves the entry out. */
14
+ parse: (args: {
15
+ key: string;
16
+ entry: Entry;
17
+ stackTrail: StackTrail;
18
+ }) => Parsed | undefined;
19
+ };
20
+ /**
21
+ * Parse each entry of a `components` bucket inside its own `tryParseAt`
22
+ * boundary. Shared by the 3.0, 3.1 and 3.2 parsers.
23
+ *
24
+ * An entry that is not a plain object (`null`, an array, a string), or that
25
+ * throws while it parses, is dropped with one error of `type` at its
26
+ * location. Consumers reference it by name, so leaving it out silently would
27
+ * change what they render. The other entries are kept.
28
+ */
29
+ export declare const toComponentEntries: <Entry, Parsed>({ entries, stackTrail, context, type, label, parse }: ToComponentEntriesArgs<Entry, Parsed>) => Record<string, Parsed>;
@@ -0,0 +1,31 @@
1
+ import { tryParseAt } from '../context/tryParseAt.js';
2
+ import { isPlainObject } from './isPlainObject.js';
3
+ /**
4
+ * Parse each entry of a `components` bucket inside its own `tryParseAt`
5
+ * boundary. Shared by the 3.0, 3.1 and 3.2 parsers.
6
+ *
7
+ * An entry that is not a plain object (`null`, an array, a string), or that
8
+ * throws while it parses, is dropped with one error of `type` at its
9
+ * location. Consumers reference it by name, so leaving it out silently would
10
+ * change what they render. The other entries are kept.
11
+ */
12
+ export const toComponentEntries = ({ entries, stackTrail, context, type, label, parse }) => {
13
+ const parsedEntries = Object.entries(entries).flatMap(([key, entry]) => {
14
+ const parsed = tryParseAt({
15
+ stackTrail,
16
+ key,
17
+ context,
18
+ type,
19
+ parent: entry,
20
+ fn: st => {
21
+ if (!isPlainObject(entry)) {
22
+ throw new Error(`${label} must be an object, got ${toKind(entry)}`);
23
+ }
24
+ return parse({ key, entry, stackTrail: st });
25
+ }
26
+ });
27
+ return parsed === undefined ? [] : [[key, parsed]];
28
+ });
29
+ return Object.fromEntries(parsedEntries);
30
+ };
31
+ const toKind = (value) => value === null ? 'null' : Array.isArray(value) ? 'an array' : typeof value;
@@ -0,0 +1,30 @@
1
+ import type { ParseContextType } from '../context/parseTypes.js';
2
+ import type { StackTrail } from '../context/StackTrail.js';
3
+ export type ToPresentInfoArgs<Info extends object> = {
4
+ info: Info | null | undefined;
5
+ /** Trail that ends in the `info` key. */
6
+ stackTrail: StackTrail;
7
+ context: ParseContextType;
8
+ };
9
+ /**
10
+ * Check the document's `info` field before its fields are parsed. Shared by
11
+ * the 3.0, 3.1 and 3.2 parsers.
12
+ *
13
+ * Missing, `null` or not a plain object: logs one `MISSING_INFO` warning and
14
+ * returns `undefined`. The caller reads it as an empty title and version.
15
+ */
16
+ export declare const toPresentInfo: <Info extends object>({ info, stackTrail, context }: ToPresentInfoArgs<Info>) => Info | undefined;
17
+ export type ToInfoStringArgs = {
18
+ value: unknown;
19
+ key: 'title' | 'version';
20
+ parent: unknown;
21
+ /** Trail that ends in the `info` key. */
22
+ stackTrail: StackTrail;
23
+ context: ParseContextType;
24
+ };
25
+ /**
26
+ * Read `info.title` or `info.version`. A value that is not a string (missing,
27
+ * or a YAML number such as `version: 1.0`) logs one `MISSING_INFO` warning
28
+ * and reads as `''`.
29
+ */
30
+ export declare const toInfoString: ({ value, key, parent, stackTrail, context }: ToInfoStringArgs) => string;
@@ -0,0 +1,40 @@
1
+ import { isPlainObject } from './isPlainObject.js';
2
+ /**
3
+ * Check the document's `info` field before its fields are parsed. Shared by
4
+ * the 3.0, 3.1 and 3.2 parsers.
5
+ *
6
+ * Missing, `null` or not a plain object: logs one `MISSING_INFO` warning and
7
+ * returns `undefined`. The caller reads it as an empty title and version.
8
+ */
9
+ export const toPresentInfo = ({ info, stackTrail, context }) => {
10
+ if (!isPlainObject(info)) {
11
+ context.logIssueNoKey({
12
+ stackTrail,
13
+ parent: info,
14
+ level: 'warning',
15
+ type: 'MISSING_INFO',
16
+ message: 'Document has no info object; read as empty title and version'
17
+ });
18
+ return undefined;
19
+ }
20
+ return info;
21
+ };
22
+ /**
23
+ * Read `info.title` or `info.version`. A value that is not a string (missing,
24
+ * or a YAML number such as `version: 1.0`) logs one `MISSING_INFO` warning
25
+ * and reads as `''`.
26
+ */
27
+ export const toInfoString = ({ value, key, parent, stackTrail, context }) => {
28
+ if (typeof value === 'string') {
29
+ return value;
30
+ }
31
+ context.logIssue({
32
+ key,
33
+ stackTrail,
34
+ parent,
35
+ level: 'warning',
36
+ type: 'MISSING_INFO',
37
+ message: `Info has no string ${key}; read as ''`
38
+ });
39
+ return '';
40
+ };
@@ -1,5 +1,5 @@
1
1
  import { toOptionalResponsesV3 } from '../response/toResponseV3.js';
2
- import { toHeadersV3 } from '../header/toHeadersV3.js';
2
+ import { toComponentHeadersV3 } from '../header/toHeadersV3.js';
3
3
  import { toLinksV3 } from '../link/toLinksV3.js';
4
4
  import { toOptionalSchemasV3 } from '../schema/toSchemasV3.js';
5
5
  import { toOptionalParametersV3 } from '../parameter/toParameterV3.js';
@@ -32,7 +32,7 @@ export const toComponentsV3 = ({ components, stackTrail, context }) => {
32
32
  context
33
33
  }),
34
34
  requestBodies: stackTrail.trace('requestBodies', st => toRequestBodiesV3({ requestBodies, stackTrail: st, context })),
35
- headers: stackTrail.trace('headers', st => toHeadersV3({ headers, stackTrail: st, context })),
35
+ headers: stackTrail.trace('headers', st => toComponentHeadersV3({ headers, stackTrail: st, context })),
36
36
  securitySchemes: stackTrail.trace('securitySchemes', st => toSecuritySchemesV3({ securitySchemes, stackTrail: st, context })),
37
37
  links: stackTrail.trace('links', st => toLinksV3({ links, stackTrail: st, context })),
38
38
  extensionFields
@@ -4,7 +4,9 @@ import type { ParseContextType } from '../../../context/parseTypes.js';
4
4
  import type { StackTrail } from '../../../context/StackTrail.js';
5
5
  export type ToExternalDocsArgs = {
6
6
  externalDocs: OpenAPIV3.ExternalDocumentationObject | undefined;
7
+ /** Trail that ends in the `externalDocs` key. */
7
8
  stackTrail: StackTrail;
8
9
  context: ParseContextType;
9
10
  };
10
- export declare const toExternalDocs: ({ externalDocs, stackTrail }: ToExternalDocsArgs) => OasExternalDocs | undefined;
11
+ /** An invalid `externalDocs` is left out with a warning; its parent is kept. */
12
+ export declare const toExternalDocs: ({ externalDocs, stackTrail, context }: ToExternalDocsArgs) => OasExternalDocs | undefined;
@@ -1,15 +1,16 @@
1
1
  import { OasExternalDocs } from '../../../oas/externalDocs/ExternalDocs.js';
2
- import * as v from 'valibot';
3
- import { oasExternalDocsData } from '../../../oas/externalDocs/externalDocsTypes.js';
4
- export const toExternalDocs = ({ externalDocs, stackTrail }) => {
5
- if (!externalDocs) {
2
+ import { toValidExternalDocs } from '../../descriptiveFields.js';
3
+ /** An invalid `externalDocs` is left out with a warning; its parent is kept. */
4
+ export const toExternalDocs = ({ externalDocs, stackTrail, context }) => {
5
+ if (externalDocs === undefined) {
6
6
  return undefined;
7
7
  }
8
- if (!v.is(oasExternalDocsData, externalDocs)) {
9
- v.parse(oasExternalDocsData, externalDocs);
8
+ const valid = toValidExternalDocs({ externalDocs, stackTrail, context });
9
+ if (!valid) {
10
+ return undefined;
10
11
  }
11
12
  return new OasExternalDocs({
12
- url: externalDocs.url,
13
- description: externalDocs.description
13
+ url: valid.url,
14
+ description: valid.description
14
15
  });
15
16
  };
@@ -9,6 +9,18 @@ export type ToHeadersV3Args = {
9
9
  context: ParseContextType;
10
10
  };
11
11
  export declare const toHeadersV3: ({ headers, stackTrail, context }: ToHeadersV3Args) => Record<string, OasHeader | OasRef<"header">> | undefined;
12
+ export type ToComponentHeadersV3Args = {
13
+ headers: Record<string, OpenAPIV3.ReferenceObject | OpenAPIV3.HeaderObject> | undefined;
14
+ stackTrail: StackTrail;
15
+ context: ParseContextType;
16
+ };
17
+ /**
18
+ * Parse `components.headers`. Each entry parses inside its own boundary: a
19
+ * broken one is dropped with an INVALID_HEADER error, and the others are
20
+ * kept. Headers inside a response stay with {@link toHeadersV3}, where a
21
+ * broken header drops the response.
22
+ */
23
+ export declare const toComponentHeadersV3: ({ headers, stackTrail, context }: ToComponentHeadersV3Args) => Record<string, OasHeader | OasRef<"header">> | undefined;
12
24
  export type ToHeaderV3Args = {
13
25
  header: OpenAPIV3.ReferenceObject | OpenAPIV3.HeaderObject;
14
26
  /** The header's name, used to key a singular `example` */
@@ -5,6 +5,7 @@ import { isRef } from '../../../helpers/refFns.js';
5
5
  import { toOptionalMediaTypeItemsV3 } from '../mediaType/toMediaTypeItemV3.js';
6
6
  import { OasHeader } from '../../../oas/header/Header.js';
7
7
  import { toSpecificationExtensionsV3 } from '../specificationExtensions/toSpecificationExtensionsV3.js';
8
+ import { toComponentEntries } from '../../toComponentEntries.js';
8
9
  export const toHeadersV3 = ({ headers, stackTrail, context }) => {
9
10
  if (!headers) {
10
11
  return undefined;
@@ -16,6 +17,25 @@ export const toHeadersV3 = ({ headers, stackTrail, context }) => {
16
17
  }
17
18
  return output;
18
19
  };
20
+ /**
21
+ * Parse `components.headers`. Each entry parses inside its own boundary: a
22
+ * broken one is dropped with an INVALID_HEADER error, and the others are
23
+ * kept. Headers inside a response stay with {@link toHeadersV3}, where a
24
+ * broken header drops the response.
25
+ */
26
+ export const toComponentHeadersV3 = ({ headers, stackTrail, context }) => {
27
+ if (!headers) {
28
+ return undefined;
29
+ }
30
+ return toComponentEntries({
31
+ entries: headers,
32
+ stackTrail,
33
+ context,
34
+ type: 'INVALID_HEADER',
35
+ label: 'Header',
36
+ parse: ({ key, entry, stackTrail: st }) => toHeaderV3({ header: entry, name: key, stackTrail: st, context })
37
+ });
38
+ };
19
39
  const toHeaderV3 = ({ header, name, stackTrail, context }) => {
20
40
  if (isRef(header)) {
21
41
  return toRefV31({ ref: header, refType: 'header', stackTrail, context });
@@ -3,7 +3,8 @@ import type { ParseContextType } from '../../../context/parseTypes.js';
3
3
  import { OasInfo } from '../../../oas/info/Info.js';
4
4
  import type { StackTrail } from '../../../context/StackTrail.js';
5
5
  export type ToInfoV3Args = {
6
- info: OpenAPIV3.InfoObject;
6
+ /** Required by the spec; a document without it still parses (see {@link toPresentInfo}). */
7
+ info: OpenAPIV3.InfoObject | null | undefined;
7
8
  stackTrail: StackTrail;
8
9
  context: ParseContextType;
9
10
  };