@kubb/ast 5.13.0 → 5.13.1
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/dist/index.cjs +111 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +61 -1
- package/dist/index.d.ts +61 -1
- package/dist/index.js +108 -8
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -368,6 +368,21 @@ type ObjectSchemaNode = SchemaNodeBase & {
|
|
|
368
368
|
* Pattern-based property schemas.
|
|
369
369
|
*/
|
|
370
370
|
patternProperties?: Record<string, SchemaNode>;
|
|
371
|
+
/**
|
|
372
|
+
* Schema that each property name must match.
|
|
373
|
+
*
|
|
374
|
+
* @example
|
|
375
|
+
* ```ts
|
|
376
|
+
* const objectSchema: ObjectSchemaNode = {
|
|
377
|
+
* kind: 'Schema',
|
|
378
|
+
* type: 'object',
|
|
379
|
+
* primitive: 'object',
|
|
380
|
+
* properties: [],
|
|
381
|
+
* propertyNames: { kind: 'Schema', type: 'string', primitive: 'string', pattern: '^[a-z]+$' },
|
|
382
|
+
* }
|
|
383
|
+
* ```
|
|
384
|
+
*/
|
|
385
|
+
propertyNames?: SchemaNode;
|
|
371
386
|
/**
|
|
372
387
|
* Minimum number of properties allowed.
|
|
373
388
|
*/
|
|
@@ -2103,6 +2118,42 @@ type TransformOptions = Visitor & TraversalOptions;
|
|
|
2103
2118
|
* ```
|
|
2104
2119
|
*/
|
|
2105
2120
|
type CollectOptions<T> = VisitorOf<T> & TraversalOptions;
|
|
2121
|
+
/**
|
|
2122
|
+
* Visits each direct child node in the AST's traversal order.
|
|
2123
|
+
*
|
|
2124
|
+
* @example
|
|
2125
|
+
* ```ts
|
|
2126
|
+
* forEachChild(operationNode, (child) => {
|
|
2127
|
+
* console.log(child.kind)
|
|
2128
|
+
* })
|
|
2129
|
+
* ```
|
|
2130
|
+
*/
|
|
2131
|
+
export declare function forEachChild(node: Node, visit: (child: Node) => void): void;
|
|
2132
|
+
/**
|
|
2133
|
+
* Visits each schema in a node's child tree in depth-first order. Includes `node` when it is a schema.
|
|
2134
|
+
*
|
|
2135
|
+
* @example
|
|
2136
|
+
* ```ts
|
|
2137
|
+
* forEachSchema(operationNode, (schema) => {
|
|
2138
|
+
* console.log(schema.type)
|
|
2139
|
+
* })
|
|
2140
|
+
* ```
|
|
2141
|
+
*/
|
|
2142
|
+
export declare function forEachSchema(node: Node, visit: (schema: SchemaNode) => void): void;
|
|
2143
|
+
/**
|
|
2144
|
+
* Finds the first schema that matches `predicate` in depth-first order. Set `followRefs` to include resolved ref targets.
|
|
2145
|
+
*
|
|
2146
|
+
* @example
|
|
2147
|
+
* ```ts
|
|
2148
|
+
* const firstString = findSchema(operationNode, {
|
|
2149
|
+
* predicate: (schema) => schema.type === 'string',
|
|
2150
|
+
* })
|
|
2151
|
+
* ```
|
|
2152
|
+
*/
|
|
2153
|
+
export declare function findSchema(node: Node, { predicate, followRefs }: {
|
|
2154
|
+
predicate: (schema: SchemaNode) => boolean;
|
|
2155
|
+
followRefs?: boolean;
|
|
2156
|
+
}): SchemaNode | undefined;
|
|
2106
2157
|
/**
|
|
2107
2158
|
* Synchronous depth-first transform. Each visitor callback can return a
|
|
2108
2159
|
* replacement node. Returning `undefined` keeps the original.
|
|
@@ -2465,6 +2516,15 @@ export declare function refName(ref: string): string;
|
|
|
2465
2516
|
* `resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Order', targetName: 'OrderSchema' }) // 'OrderSchema'`
|
|
2466
2517
|
*/
|
|
2467
2518
|
export declare function resolveRefName(node: SchemaNode | null | undefined): string | null;
|
|
2519
|
+
/**
|
|
2520
|
+
* Follows a resolved ref chain to its target schema. Returns an unresolved or cyclic ref as is.
|
|
2521
|
+
*
|
|
2522
|
+
* @example
|
|
2523
|
+
* ```ts
|
|
2524
|
+
* const target = resolveRef(refSchema)
|
|
2525
|
+
* ```
|
|
2526
|
+
*/
|
|
2527
|
+
export declare function resolveRef(node: SchemaNode): SchemaNode;
|
|
2468
2528
|
/**
|
|
2469
2529
|
* Merges a ref node with its resolved schema, giving the fields set at the usage site precedence.
|
|
2470
2530
|
*
|
|
@@ -2690,7 +2750,7 @@ type ParserOptions = {
|
|
|
2690
2750
|
enumSuffix: 'enum' | (string & {});
|
|
2691
2751
|
};
|
|
2692
2752
|
declare namespace index_d_exports {
|
|
2693
|
-
export { ArraySchemaNode, ArrowFunctionNode, BreakNode, CodeNode, ConstNode, ContentNode, DateOnlyTypeValue, DateSchemaNode, DateTimeTypeValue, DateTypeOptions, DatetimeSchemaNode, DistributiveOmit, Enforce, EnumSchemaNode, ExportNode, FileNode, FunctionNode, GenericOperationNode, HttpMethod, HttpOperationNode, ImportNode, InputMeta, InputNode, InputSecurityScheme, InputServer, IntersectionSchemaNode, JSDocNode, JsxNode, Macro, Node, NodeKind, NotSchemaNode, NumberSchemaNode, ObjectSchemaNode, OperationNode, ParameterLocation, ParameterNode, ParameterStyle, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, RequestBodyNode, ResponseNode, ScalarSchemaNode, ScalarSchemaType, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext, applyMacros, index_d_exports as ast, collect, collectImportedRefNames, collectSchemaRefs, collectSync, collectUsedSchemaNames, composeMacros, createPrinter, defineMacro, extractStringsFromNodes, index_d_exports$1 as factory, findCircularSchemas, findCircularSchemasFromGraph, getSchemaLiteralValues, inferDiscriminatorPropertyName, isBareRef, isHttpOperationNode, mergeRefWithSchema, narrowSchema, optionality, refName, resolveRefName, resolveSchemaProperties, schemaTypes, transform };
|
|
2753
|
+
export { ArraySchemaNode, ArrowFunctionNode, BreakNode, CodeNode, ConstNode, ContentNode, DateOnlyTypeValue, DateSchemaNode, DateTimeTypeValue, DateTypeOptions, DatetimeSchemaNode, DistributiveOmit, Enforce, EnumSchemaNode, ExportNode, FileNode, FunctionNode, GenericOperationNode, HttpMethod, HttpOperationNode, ImportNode, InputMeta, InputNode, InputSecurityScheme, InputServer, IntersectionSchemaNode, JSDocNode, JsxNode, Macro, Node, NodeKind, NotSchemaNode, NumberSchemaNode, ObjectSchemaNode, OperationNode, ParameterLocation, ParameterNode, ParameterStyle, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, RequestBodyNode, ResponseNode, ScalarSchemaNode, ScalarSchemaType, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext, applyMacros, index_d_exports as ast, collect, collectImportedRefNames, collectSchemaRefs, collectSync, collectUsedSchemaNames, composeMacros, createPrinter, defineMacro, extractStringsFromNodes, index_d_exports$1 as factory, findCircularSchemas, findCircularSchemasFromGraph, findSchema, forEachChild, forEachSchema, getSchemaLiteralValues, inferDiscriminatorPropertyName, isBareRef, isHttpOperationNode, mergeRefWithSchema, narrowSchema, optionality, refName, resolveRef, resolveRefName, resolveSchemaProperties, schemaTypes, transform };
|
|
2694
2754
|
}
|
|
2695
2755
|
//#endregion
|
|
2696
2756
|
export { type ArraySchemaNode, type ArrowFunctionNode, type BreakNode, type CodeNode, type ConstNode, type ContentNode, type DateOnlyTypeValue, type DateSchemaNode, type DateTimeTypeValue, type DateTypeOptions, type DatetimeSchemaNode, type DistributiveOmit, type Enforce, type EnumSchemaNode, type ExportNode, type FileNode, type FunctionNode, type GenericOperationNode, type HttpMethod, type HttpOperationNode, type ImportNode, type InputMeta, type InputNode, type InputSecurityScheme, type InputServer, type IntersectionSchemaNode, type JSDocNode, type JsxNode, type Macro, type Node, type NodeKind, type NotSchemaNode, type NumberSchemaNode, type ObjectSchemaNode, type OperationNode, type ParameterLocation, type ParameterNode, type ParameterStyle, type ParentOf, type ParserOptions, type PrimitiveSchemaType, type Printer, type PrinterFactoryOptions, type PrinterPartial, type PropertyNode, type RefSchemaNode, type RequestBodyNode, type ResponseNode, type ScalarSchemaNode, type ScalarSchemaType, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, type UserFileNode, type Visitor, type VisitorContext, index_d_exports as ast, index_d_exports$1 as factory };
|
package/dist/index.d.ts
CHANGED
|
@@ -368,6 +368,21 @@ type ObjectSchemaNode = SchemaNodeBase & {
|
|
|
368
368
|
* Pattern-based property schemas.
|
|
369
369
|
*/
|
|
370
370
|
patternProperties?: Record<string, SchemaNode>;
|
|
371
|
+
/**
|
|
372
|
+
* Schema that each property name must match.
|
|
373
|
+
*
|
|
374
|
+
* @example
|
|
375
|
+
* ```ts
|
|
376
|
+
* const objectSchema: ObjectSchemaNode = {
|
|
377
|
+
* kind: 'Schema',
|
|
378
|
+
* type: 'object',
|
|
379
|
+
* primitive: 'object',
|
|
380
|
+
* properties: [],
|
|
381
|
+
* propertyNames: { kind: 'Schema', type: 'string', primitive: 'string', pattern: '^[a-z]+$' },
|
|
382
|
+
* }
|
|
383
|
+
* ```
|
|
384
|
+
*/
|
|
385
|
+
propertyNames?: SchemaNode;
|
|
371
386
|
/**
|
|
372
387
|
* Minimum number of properties allowed.
|
|
373
388
|
*/
|
|
@@ -2103,6 +2118,42 @@ type TransformOptions = Visitor & TraversalOptions;
|
|
|
2103
2118
|
* ```
|
|
2104
2119
|
*/
|
|
2105
2120
|
type CollectOptions<T> = VisitorOf<T> & TraversalOptions;
|
|
2121
|
+
/**
|
|
2122
|
+
* Visits each direct child node in the AST's traversal order.
|
|
2123
|
+
*
|
|
2124
|
+
* @example
|
|
2125
|
+
* ```ts
|
|
2126
|
+
* forEachChild(operationNode, (child) => {
|
|
2127
|
+
* console.log(child.kind)
|
|
2128
|
+
* })
|
|
2129
|
+
* ```
|
|
2130
|
+
*/
|
|
2131
|
+
export declare function forEachChild(node: Node, visit: (child: Node) => void): void;
|
|
2132
|
+
/**
|
|
2133
|
+
* Visits each schema in a node's child tree in depth-first order. Includes `node` when it is a schema.
|
|
2134
|
+
*
|
|
2135
|
+
* @example
|
|
2136
|
+
* ```ts
|
|
2137
|
+
* forEachSchema(operationNode, (schema) => {
|
|
2138
|
+
* console.log(schema.type)
|
|
2139
|
+
* })
|
|
2140
|
+
* ```
|
|
2141
|
+
*/
|
|
2142
|
+
export declare function forEachSchema(node: Node, visit: (schema: SchemaNode) => void): void;
|
|
2143
|
+
/**
|
|
2144
|
+
* Finds the first schema that matches `predicate` in depth-first order. Set `followRefs` to include resolved ref targets.
|
|
2145
|
+
*
|
|
2146
|
+
* @example
|
|
2147
|
+
* ```ts
|
|
2148
|
+
* const firstString = findSchema(operationNode, {
|
|
2149
|
+
* predicate: (schema) => schema.type === 'string',
|
|
2150
|
+
* })
|
|
2151
|
+
* ```
|
|
2152
|
+
*/
|
|
2153
|
+
export declare function findSchema(node: Node, { predicate, followRefs }: {
|
|
2154
|
+
predicate: (schema: SchemaNode) => boolean;
|
|
2155
|
+
followRefs?: boolean;
|
|
2156
|
+
}): SchemaNode | undefined;
|
|
2106
2157
|
/**
|
|
2107
2158
|
* Synchronous depth-first transform. Each visitor callback can return a
|
|
2108
2159
|
* replacement node. Returning `undefined` keeps the original.
|
|
@@ -2465,6 +2516,15 @@ export declare function refName(ref: string): string;
|
|
|
2465
2516
|
* `resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Order', targetName: 'OrderSchema' }) // 'OrderSchema'`
|
|
2466
2517
|
*/
|
|
2467
2518
|
export declare function resolveRefName(node: SchemaNode | null | undefined): string | null;
|
|
2519
|
+
/**
|
|
2520
|
+
* Follows a resolved ref chain to its target schema. Returns an unresolved or cyclic ref as is.
|
|
2521
|
+
*
|
|
2522
|
+
* @example
|
|
2523
|
+
* ```ts
|
|
2524
|
+
* const target = resolveRef(refSchema)
|
|
2525
|
+
* ```
|
|
2526
|
+
*/
|
|
2527
|
+
export declare function resolveRef(node: SchemaNode): SchemaNode;
|
|
2468
2528
|
/**
|
|
2469
2529
|
* Merges a ref node with its resolved schema, giving the fields set at the usage site precedence.
|
|
2470
2530
|
*
|
|
@@ -2690,7 +2750,7 @@ type ParserOptions = {
|
|
|
2690
2750
|
enumSuffix: 'enum' | (string & {});
|
|
2691
2751
|
};
|
|
2692
2752
|
declare namespace index_d_exports {
|
|
2693
|
-
export { ArraySchemaNode, ArrowFunctionNode, BreakNode, CodeNode, ConstNode, ContentNode, DateOnlyTypeValue, DateSchemaNode, DateTimeTypeValue, DateTypeOptions, DatetimeSchemaNode, DistributiveOmit, Enforce, EnumSchemaNode, ExportNode, FileNode, FunctionNode, GenericOperationNode, HttpMethod, HttpOperationNode, ImportNode, InputMeta, InputNode, InputSecurityScheme, InputServer, IntersectionSchemaNode, JSDocNode, JsxNode, Macro, Node, NodeKind, NotSchemaNode, NumberSchemaNode, ObjectSchemaNode, OperationNode, ParameterLocation, ParameterNode, ParameterStyle, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, RequestBodyNode, ResponseNode, ScalarSchemaNode, ScalarSchemaType, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext, applyMacros, index_d_exports as ast, collect, collectImportedRefNames, collectSchemaRefs, collectSync, collectUsedSchemaNames, composeMacros, createPrinter, defineMacro, extractStringsFromNodes, index_d_exports$1 as factory, findCircularSchemas, findCircularSchemasFromGraph, getSchemaLiteralValues, inferDiscriminatorPropertyName, isBareRef, isHttpOperationNode, mergeRefWithSchema, narrowSchema, optionality, refName, resolveRefName, resolveSchemaProperties, schemaTypes, transform };
|
|
2753
|
+
export { ArraySchemaNode, ArrowFunctionNode, BreakNode, CodeNode, ConstNode, ContentNode, DateOnlyTypeValue, DateSchemaNode, DateTimeTypeValue, DateTypeOptions, DatetimeSchemaNode, DistributiveOmit, Enforce, EnumSchemaNode, ExportNode, FileNode, FunctionNode, GenericOperationNode, HttpMethod, HttpOperationNode, ImportNode, InputMeta, InputNode, InputSecurityScheme, InputServer, IntersectionSchemaNode, JSDocNode, JsxNode, Macro, Node, NodeKind, NotSchemaNode, NumberSchemaNode, ObjectSchemaNode, OperationNode, ParameterLocation, ParameterNode, ParameterStyle, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, RequestBodyNode, ResponseNode, ScalarSchemaNode, ScalarSchemaType, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext, applyMacros, index_d_exports as ast, collect, collectImportedRefNames, collectSchemaRefs, collectSync, collectUsedSchemaNames, composeMacros, createPrinter, defineMacro, extractStringsFromNodes, index_d_exports$1 as factory, findCircularSchemas, findCircularSchemasFromGraph, findSchema, forEachChild, forEachSchema, getSchemaLiteralValues, inferDiscriminatorPropertyName, isBareRef, isHttpOperationNode, mergeRefWithSchema, narrowSchema, optionality, refName, resolveRef, resolveRefName, resolveSchemaProperties, schemaTypes, transform };
|
|
2694
2754
|
}
|
|
2695
2755
|
//#endregion
|
|
2696
2756
|
export { type ArraySchemaNode, type ArrowFunctionNode, type BreakNode, type CodeNode, type ConstNode, type ContentNode, type DateOnlyTypeValue, type DateSchemaNode, type DateTimeTypeValue, type DateTypeOptions, type DatetimeSchemaNode, type DistributiveOmit, type Enforce, type EnumSchemaNode, type ExportNode, type FileNode, type FunctionNode, type GenericOperationNode, type HttpMethod, type HttpOperationNode, type ImportNode, type InputMeta, type InputNode, type InputSecurityScheme, type InputServer, type IntersectionSchemaNode, type JSDocNode, type JsxNode, type Macro, type Node, type NodeKind, type NotSchemaNode, type NumberSchemaNode, type ObjectSchemaNode, type OperationNode, type ParameterLocation, type ParameterNode, type ParameterStyle, type ParentOf, type ParserOptions, type PrimitiveSchemaType, type Printer, type PrinterFactoryOptions, type PrinterPartial, type PropertyNode, type RefSchemaNode, type RequestBodyNode, type ResponseNode, type ScalarSchemaNode, type ScalarSchemaType, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, type UserFileNode, type Visitor, type VisitorContext, index_d_exports as ast, index_d_exports$1 as factory };
|
package/dist/index.js
CHANGED
|
@@ -174,6 +174,7 @@ const VISITOR_KEYS = {
|
|
|
174
174
|
"members",
|
|
175
175
|
"additionalProperties",
|
|
176
176
|
"patternProperties",
|
|
177
|
+
"propertyNames",
|
|
177
178
|
"rest"
|
|
178
179
|
],
|
|
179
180
|
Property: ["schema"],
|
|
@@ -221,25 +222,101 @@ function isNodeRecord(value) {
|
|
|
221
222
|
* ```
|
|
222
223
|
*/
|
|
223
224
|
function getChildren(node, recurse) {
|
|
225
|
+
if (node.kind === "Schema" && !recurse) return [];
|
|
224
226
|
const children = [];
|
|
225
|
-
|
|
226
|
-
|
|
227
|
+
visitChildNodes(node, (child) => {
|
|
228
|
+
children.push(child);
|
|
229
|
+
return false;
|
|
230
|
+
});
|
|
231
|
+
return children;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Visits each direct child node in the AST's traversal order.
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```ts
|
|
238
|
+
* forEachChild(operationNode, (child) => {
|
|
239
|
+
* console.log(child.kind)
|
|
240
|
+
* })
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
function forEachChild(node, visit) {
|
|
244
|
+
visitChildNodes(node, (child) => {
|
|
245
|
+
visit(child);
|
|
246
|
+
return false;
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
function visitChildNodes(node, visit) {
|
|
250
|
+
const keys = VISITOR_KEYS[node.kind];
|
|
251
|
+
if (!keys) return false;
|
|
227
252
|
const record = node;
|
|
228
253
|
for (const key of keys) {
|
|
229
254
|
const value = record[key];
|
|
230
255
|
if (Array.isArray(value)) {
|
|
231
|
-
for (const item of value) if (isNode(item)
|
|
256
|
+
for (const item of value) if (isNode(item) && visit(item)) return true;
|
|
232
257
|
continue;
|
|
233
258
|
}
|
|
234
259
|
if (isNode(value)) {
|
|
235
|
-
|
|
260
|
+
if (visit(value)) return true;
|
|
236
261
|
continue;
|
|
237
262
|
}
|
|
238
|
-
if (isNodeRecord(value)) {
|
|
239
|
-
|
|
263
|
+
if (isNodeRecord(value)) for (const name in value) {
|
|
264
|
+
if (!Object.hasOwn(value, name)) continue;
|
|
265
|
+
const item = value[name];
|
|
266
|
+
if (isNode(item) && visit(item)) return true;
|
|
240
267
|
}
|
|
241
268
|
}
|
|
242
|
-
return
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Visits each schema in a node's child tree in depth-first order. Includes `node` when it is a schema.
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* ```ts
|
|
276
|
+
* forEachSchema(operationNode, (schema) => {
|
|
277
|
+
* console.log(schema.type)
|
|
278
|
+
* })
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
281
|
+
function forEachSchema(node, visit) {
|
|
282
|
+
const walk = (current) => {
|
|
283
|
+
if (current.kind === "Schema") visit(current);
|
|
284
|
+
forEachChild(current, walk);
|
|
285
|
+
};
|
|
286
|
+
walk(node);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Finds the first schema that matches `predicate` in depth-first order. Set `followRefs` to include resolved ref targets.
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* ```ts
|
|
293
|
+
* const firstString = findSchema(operationNode, {
|
|
294
|
+
* predicate: (schema) => schema.type === 'string',
|
|
295
|
+
* })
|
|
296
|
+
* ```
|
|
297
|
+
*/
|
|
298
|
+
function findSchema(node, { predicate, followRefs = false }) {
|
|
299
|
+
const seen = followRefs ? /* @__PURE__ */ new WeakSet() : void 0;
|
|
300
|
+
const walk = (current) => {
|
|
301
|
+
if (current.kind === "Schema") {
|
|
302
|
+
if (predicate(current)) return current;
|
|
303
|
+
if (seen) {
|
|
304
|
+
if (seen.has(current)) return void 0;
|
|
305
|
+
seen.add(current);
|
|
306
|
+
if (current.type === "ref" && current.schema) {
|
|
307
|
+
const found = walk(current.schema);
|
|
308
|
+
if (found) return found;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
let found;
|
|
313
|
+
visitChildNodes(current, (child) => {
|
|
314
|
+
found = walk(child);
|
|
315
|
+
return found !== void 0;
|
|
316
|
+
});
|
|
317
|
+
if (found) return found;
|
|
318
|
+
};
|
|
319
|
+
return walk(node);
|
|
243
320
|
}
|
|
244
321
|
/**
|
|
245
322
|
* Runs the visitor callback that matches `node.kind` with the traversal
|
|
@@ -623,6 +700,25 @@ function resolveRefName(node) {
|
|
|
623
700
|
if (node.ref) return refName(node.ref);
|
|
624
701
|
return node.name ?? node.schema?.name ?? null;
|
|
625
702
|
}
|
|
703
|
+
/**
|
|
704
|
+
* Follows a resolved ref chain to its target schema. Returns an unresolved or cyclic ref as is.
|
|
705
|
+
*
|
|
706
|
+
* @example
|
|
707
|
+
* ```ts
|
|
708
|
+
* const target = resolveRef(refSchema)
|
|
709
|
+
* ```
|
|
710
|
+
*/
|
|
711
|
+
function resolveRef(node) {
|
|
712
|
+
if (node.type !== "ref" || !node.schema) return node;
|
|
713
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
714
|
+
let current = node;
|
|
715
|
+
while (current.type === "ref" && current.schema) {
|
|
716
|
+
if (seen.has(current)) return current;
|
|
717
|
+
seen.add(current);
|
|
718
|
+
current = current.schema;
|
|
719
|
+
}
|
|
720
|
+
return current;
|
|
721
|
+
}
|
|
626
722
|
/** Fields of a ref node that describe the ref itself, so they never override the resolved schema. */
|
|
627
723
|
const REF_OWN_KEYS = /* @__PURE__ */ new Set([
|
|
628
724
|
"kind",
|
|
@@ -1517,6 +1613,9 @@ var src_exports = /* @__PURE__ */ __exportAll({
|
|
|
1517
1613
|
factory: () => nodes_exports,
|
|
1518
1614
|
findCircularSchemas: () => findCircularSchemas,
|
|
1519
1615
|
findCircularSchemasFromGraph: () => findCircularSchemasFromGraph,
|
|
1616
|
+
findSchema: () => findSchema,
|
|
1617
|
+
forEachChild: () => forEachChild,
|
|
1618
|
+
forEachSchema: () => forEachSchema,
|
|
1520
1619
|
getSchemaLiteralValues: () => getSchemaLiteralValues,
|
|
1521
1620
|
inferDiscriminatorPropertyName: () => inferDiscriminatorPropertyName,
|
|
1522
1621
|
isBareRef: () => isBareRef,
|
|
@@ -1525,12 +1624,13 @@ var src_exports = /* @__PURE__ */ __exportAll({
|
|
|
1525
1624
|
narrowSchema: () => narrowSchema,
|
|
1526
1625
|
optionality: () => optionality,
|
|
1527
1626
|
refName: () => refName,
|
|
1627
|
+
resolveRef: () => resolveRef,
|
|
1528
1628
|
resolveRefName: () => resolveRefName,
|
|
1529
1629
|
resolveSchemaProperties: () => resolveSchemaProperties,
|
|
1530
1630
|
schemaTypes: () => schemaTypes,
|
|
1531
1631
|
transform: () => transform
|
|
1532
1632
|
});
|
|
1533
1633
|
//#endregion
|
|
1534
|
-
export { applyMacros, src_exports as ast, collect, collectImportedRefNames, collectSchemaRefs, collectSync, collectUsedSchemaNames, composeMacros, createPrinter, defineMacro, extractStringsFromNodes, nodes_exports as factory, findCircularSchemas, findCircularSchemasFromGraph, getSchemaLiteralValues, inferDiscriminatorPropertyName, isBareRef, isHttpOperationNode, mergeRefWithSchema, narrowSchema, optionality, refName, resolveRefName, resolveSchemaProperties, schemaTypes, transform };
|
|
1634
|
+
export { applyMacros, src_exports as ast, collect, collectImportedRefNames, collectSchemaRefs, collectSync, collectUsedSchemaNames, composeMacros, createPrinter, defineMacro, extractStringsFromNodes, nodes_exports as factory, findCircularSchemas, findCircularSchemasFromGraph, findSchema, forEachChild, forEachSchema, getSchemaLiteralValues, inferDiscriminatorPropertyName, isBareRef, isHttpOperationNode, mergeRefWithSchema, narrowSchema, optionality, refName, resolveRef, resolveRefName, resolveSchemaProperties, schemaTypes, transform };
|
|
1535
1635
|
|
|
1536
1636
|
//# sourceMappingURL=index.js.map
|