@kubb/ast 5.0.6 → 5.2.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.
package/dist/index.cjs CHANGED
@@ -1536,6 +1536,89 @@ function findCircularSchemas(schemas) {
1536
1536
  return findCircularSchemasMemo(schemas);
1537
1537
  }
1538
1538
  //#endregion
1539
+ //#region src/utils/schemaProperties.ts
1540
+ /**
1541
+ * Finds every matching property exposed by an object, resolved reference, or intersection.
1542
+ *
1543
+ * References are followed through their parsed `schema`. Circular references terminate without
1544
+ * revisiting a node. Unions are not traversed because a property on one branch does not describe
1545
+ * every branch.
1546
+ *
1547
+ * @example Faker discriminator narrowing
1548
+ * ```ts
1549
+ * const values = resolveSchemaProperties({ node: member, propertyName: union.discriminatorPropertyName }).flatMap(({ schema }) =>
1550
+ * getSchemaLiteralValues(schema),
1551
+ * )
1552
+ * const typeName = `Extract<Pet, { type: ${values.map(JSON.stringify).join(' | ')} }>`
1553
+ * ```
1554
+ *
1555
+ * @example Zod discriminated unions
1556
+ * ```ts
1557
+ * const canDiscriminate = union.members?.every(
1558
+ * (member) => resolveSchemaProperties({ node: member, propertyName: 'type' }).length > 0,
1559
+ * )
1560
+ * const expression = canDiscriminate ? `z.discriminatedUnion('type', members)` : `z.union(members)`
1561
+ * ```
1562
+ *
1563
+ * @example TypeScript property lookup
1564
+ * ```ts
1565
+ * const properties = resolveSchemaProperties({ node: schema, propertyName: 'status' })
1566
+ * const statusSchemas = properties.map((property) => print(property.schema))
1567
+ * ```
1568
+ */
1569
+ function resolveSchemaProperties({ node, propertyName }) {
1570
+ const properties = [];
1571
+ const visited = /* @__PURE__ */ new WeakSet();
1572
+ function visit(current) {
1573
+ if (!current || visited.has(current)) return;
1574
+ visited.add(current);
1575
+ if (current.type === "object") {
1576
+ const property = current.properties.find((candidate) => candidate.name === propertyName);
1577
+ if (property) properties.push(property);
1578
+ return;
1579
+ }
1580
+ if (current.type === "ref") {
1581
+ visit(current.schema);
1582
+ return;
1583
+ }
1584
+ if (current.type === "intersection") for (const member of current.members ?? []) visit(member);
1585
+ }
1586
+ visit(node);
1587
+ return properties;
1588
+ }
1589
+ /**
1590
+ * Reads the unique literal values represented by an enum, resolved reference, or union.
1591
+ *
1592
+ * Named enum values take precedence over simple enum values, matching how renderers consume
1593
+ * `EnumSchemaNode`.
1594
+ *
1595
+ * @example
1596
+ * ```ts
1597
+ * const values = getSchemaLiteralValues(enumNode)
1598
+ * // ['cat', 'dog']
1599
+ * ```
1600
+ */
1601
+ function getSchemaLiteralValues(node) {
1602
+ const values = /* @__PURE__ */ new Set();
1603
+ const visited = /* @__PURE__ */ new WeakSet();
1604
+ function visit(current) {
1605
+ if (!current || visited.has(current)) return;
1606
+ visited.add(current);
1607
+ if (current.type === "enum") {
1608
+ const enumValues = current.namedEnumValues?.map(({ value }) => value) ?? current.enumValues ?? [];
1609
+ for (const value of enumValues) values.add(value);
1610
+ return;
1611
+ }
1612
+ if (current.type === "ref") {
1613
+ visit(current.schema);
1614
+ return;
1615
+ }
1616
+ if (current.type === "union") for (const member of current.members ?? []) visit(member);
1617
+ }
1618
+ visit(node);
1619
+ return [...values];
1620
+ }
1621
+ //#endregion
1539
1622
  //#region src/factory.ts
1540
1623
  var factory_exports = /* @__PURE__ */ __exportAll({
1541
1624
  createArrowFunction: () => createArrowFunction,
@@ -1606,6 +1689,7 @@ var exports_exports = /* @__PURE__ */ __exportAll({
1606
1689
  findCircularSchemas: () => findCircularSchemas,
1607
1690
  findCircularSchemasFromGraph: () => findCircularSchemasFromGraph,
1608
1691
  functionDef: () => functionDef,
1692
+ getSchemaLiteralValues: () => getSchemaLiteralValues,
1609
1693
  importDef: () => importDef,
1610
1694
  inputDef: () => inputDef,
1611
1695
  isHttpOperationNode: () => isHttpOperationNode,
@@ -1619,6 +1703,7 @@ var exports_exports = /* @__PURE__ */ __exportAll({
1619
1703
  propertyDef: () => propertyDef,
1620
1704
  requestBodyDef: () => requestBodyDef,
1621
1705
  resolveRefName: () => resolveRefName,
1706
+ resolveSchemaProperties: () => resolveSchemaProperties,
1622
1707
  responseDef: () => responseDef,
1623
1708
  schemaDef: () => schemaDef,
1624
1709
  schemaTypes: () => schemaTypes,
@@ -1659,6 +1744,7 @@ exports.fileDef = fileDef;
1659
1744
  exports.findCircularSchemas = findCircularSchemas;
1660
1745
  exports.findCircularSchemasFromGraph = findCircularSchemasFromGraph;
1661
1746
  exports.functionDef = functionDef;
1747
+ exports.getSchemaLiteralValues = getSchemaLiteralValues;
1662
1748
  exports.importDef = importDef;
1663
1749
  exports.inputDef = inputDef;
1664
1750
  exports.isHttpOperationNode = isHttpOperationNode;
@@ -1672,6 +1758,7 @@ exports.parameterDef = parameterDef;
1672
1758
  exports.propertyDef = propertyDef;
1673
1759
  exports.requestBodyDef = requestBodyDef;
1674
1760
  exports.resolveRefName = resolveRefName;
1761
+ exports.resolveSchemaProperties = resolveSchemaProperties;
1675
1762
  exports.responseDef = responseDef;
1676
1763
  exports.schemaDef = schemaDef;
1677
1764
  exports.schemaTypes = schemaTypes;