@kubb/ast 5.0.0-alpha.8 → 5.0.0-beta.75

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.
@@ -2,236 +2,608 @@ import type { BaseNode } from './base.ts'
2
2
  import type { PropertyNode } from './property.ts'
3
3
 
4
4
  export type PrimitiveSchemaType =
5
+ /**
6
+ * Text value.
7
+ */
5
8
  | 'string'
9
+ /**
10
+ * Floating-point number.
11
+ */
6
12
  | 'number'
13
+ /**
14
+ * Integer number.
15
+ */
7
16
  | 'integer'
17
+ /**
18
+ * Big integer number.
19
+ */
8
20
  | 'bigint'
21
+ /**
22
+ * Boolean value.
23
+ */
9
24
  | 'boolean'
25
+ /**
26
+ * Null value.
27
+ */
10
28
  | 'null'
29
+ /**
30
+ * Any value.
31
+ */
11
32
  | 'any'
33
+ /**
34
+ * Unknown value.
35
+ */
12
36
  | 'unknown'
37
+ /**
38
+ * No value (`void`).
39
+ */
13
40
  | 'void'
41
+ /**
42
+ * Never value.
43
+ */
14
44
  | 'never'
45
+ /**
46
+ * Object value.
47
+ */
15
48
  | 'object'
49
+ /**
50
+ * Array value.
51
+ */
16
52
  | 'array'
53
+ /**
54
+ * Date value.
55
+ */
17
56
  | 'date'
18
57
 
58
+ /**
59
+ * Composite schema types.
60
+ */
19
61
  export type ComplexSchemaType = 'tuple' | 'union' | 'intersection' | 'enum'
20
62
 
21
63
  /**
22
- * Semantic types requiring special handling in code generation (e.g. generating a `Date` object or a branded type).
64
+ * Schema types that need special handling in generators.
23
65
  */
24
- export type SpecialSchemaType = 'ref' | 'datetime' | 'time' | 'uuid' | 'email' | 'url' | 'blob'
66
+ export type SpecialSchemaType = 'ref' | 'datetime' | 'time' | 'uuid' | 'email' | 'url' | 'ipv4' | 'ipv6' | 'blob'
25
67
 
68
+ /**
69
+ * All schema type strings.
70
+ */
26
71
  export type SchemaType = PrimitiveSchemaType | ComplexSchemaType | SpecialSchemaType
27
72
 
73
+ /**
74
+ * Scalar schema types without extra object/array/ref structure.
75
+ */
28
76
  export type ScalarSchemaType = Exclude<
29
77
  SchemaType,
30
- 'object' | 'array' | 'tuple' | 'union' | 'intersection' | 'enum' | 'ref' | 'datetime' | 'date' | 'time' | 'string' | 'number' | 'integer' | 'bigint' | 'url'
78
+ | 'object'
79
+ | 'array'
80
+ | 'tuple'
81
+ | 'union'
82
+ | 'intersection'
83
+ | 'enum'
84
+ | 'ref'
85
+ | 'datetime'
86
+ | 'date'
87
+ | 'time'
88
+ | 'string'
89
+ | 'number'
90
+ | 'integer'
91
+ | 'bigint'
92
+ | 'url'
93
+ | 'uuid'
94
+ | 'email'
31
95
  >
32
96
 
33
97
  /**
34
- * Base fields shared by every schema variant. Does not include spec-specific fields.
98
+ * Fields shared by all schema nodes.
35
99
  */
36
100
  type SchemaNodeBase = BaseNode & {
101
+ /**
102
+ * Node kind.
103
+ */
37
104
  kind: 'Schema'
38
105
  /**
39
- * Named schema identifier (e.g. `"Pet"` from `#/components/schemas/Pet`). `undefined` for inline schemas.
106
+ * Schema name for named definitions (for example, `"Pet"`).
107
+ * Inline schemas omit this field.
108
+ * `null` means kubb has processed this and determined there is no applicable name.
109
+ * `undefined` means the name has not been set yet.
110
+ */
111
+ name?: string | null
112
+ /**
113
+ * Short schema title.
40
114
  */
41
- name?: string
42
115
  title?: string
116
+ /**
117
+ * Schema description text.
118
+ */
43
119
  description?: string
120
+ /**
121
+ * Whether `null` is allowed.
122
+ */
44
123
  nullable?: boolean
124
+ /**
125
+ * Whether the field is optional.
126
+ */
45
127
  optional?: boolean
46
128
  /**
47
129
  * Both optional and nullable (`optional` + `nullable`).
48
130
  */
49
131
  nullish?: boolean
132
+ /**
133
+ * Whether the schema is deprecated.
134
+ */
50
135
  deprecated?: boolean
136
+ /**
137
+ * Whether the schema is read-only.
138
+ */
51
139
  readOnly?: boolean
140
+ /**
141
+ * Whether the schema is write-only.
142
+ */
52
143
  writeOnly?: boolean
144
+ /**
145
+ * Default value.
146
+ */
53
147
  default?: unknown
148
+ /**
149
+ * Example value.
150
+ */
54
151
  example?: unknown
55
152
  /**
56
- * Underlying primitive before format/semantic promotion (e.g. `'string'` for a `uuid` node).
153
+ * Base primitive type.
154
+ * For example, this is `'string'` for a `uuid` schema.
57
155
  */
58
156
  primitive?: PrimitiveSchemaType
59
157
  }
60
158
 
61
159
  /**
62
- * Object schema with ordered property definitions.
160
+ * Object schema with ordered properties.
161
+ *
162
+ * @example
163
+ * ```ts
164
+ * const objectSchema: ObjectSchemaNode = {
165
+ * kind: 'Schema',
166
+ * type: 'object',
167
+ * properties: [],
168
+ * }
169
+ * ```
63
170
  */
64
171
  export type ObjectSchemaNode = SchemaNodeBase & {
172
+ /**
173
+ * Schema type discriminator.
174
+ */
65
175
  type: 'object'
176
+ /**
177
+ * Primitive type — always `'object'` for object schemas.
178
+ */
179
+ primitive: 'object'
180
+ /**
181
+ * Ordered object properties.
182
+ */
66
183
  properties: Array<PropertyNode>
67
184
  /**
68
- * `true` allows any value; a `SchemaNode` constrains it; absent means not permitted.
185
+ * Additional object properties behavior:
186
+ * - `true`: allow any value
187
+ * - `false`: reject unknown properties (maps to `.strict()` in Zod)
188
+ * - `SchemaNode`: allow values that match that schema
189
+ * - `undefined`: no additional properties constraint (open object)
190
+ */
191
+ additionalProperties?: SchemaNode | boolean
192
+ /**
193
+ * Pattern-based property schemas.
69
194
  */
70
- additionalProperties?: SchemaNode | true
71
195
  patternProperties?: Record<string, SchemaNode>
196
+ /**
197
+ * Minimum number of properties allowed.
198
+ */
199
+ minProperties?: number
200
+ /**
201
+ * Maximum number of properties allowed.
202
+ */
203
+ maxProperties?: number
72
204
  }
73
205
 
74
206
  /**
75
- * Array or tuple schema.
207
+ * Array-like schema (`array` or `tuple`).
208
+ *
209
+ * @example
210
+ * ```ts
211
+ * const arraySchema: ArraySchemaNode = {
212
+ * kind: 'Schema',
213
+ * type: 'array',
214
+ * items: [],
215
+ * }
216
+ * ```
76
217
  */
77
218
  export type ArraySchemaNode = SchemaNodeBase & {
219
+ /**
220
+ * Schema type discriminator (`array` or `tuple`).
221
+ */
78
222
  type: 'array' | 'tuple'
223
+ /**
224
+ * Item schemas.
225
+ */
79
226
  items?: Array<SchemaNode>
80
227
  /**
81
- * Additional items beyond positional `items` in a tuple.
228
+ * Tuple rest-item schema for elements beyond positional `items`.
82
229
  */
83
230
  rest?: SchemaNode
231
+ /**
232
+ * Minimum item count (or tuple length).
233
+ */
84
234
  min?: number
235
+ /**
236
+ * Maximum item count (or tuple length).
237
+ */
85
238
  max?: number
239
+ /**
240
+ * Whether all items must be unique.
241
+ */
86
242
  unique?: boolean
87
243
  }
88
244
 
89
245
  /**
90
- * Shared base for union and intersection schemas.
246
+ * Shared shape for union and intersection schemas.
91
247
  */
92
248
  type CompositeSchemaNodeBase = SchemaNodeBase & {
249
+ /**
250
+ * Member schemas.
251
+ */
93
252
  members?: Array<SchemaNode>
94
253
  }
95
254
 
96
255
  /**
97
- * Union schema (`oneOf` / `anyOf`).
256
+ * Union schema, often from `oneOf` or `anyOf`.
257
+ *
258
+ * @example
259
+ * ```ts
260
+ * const unionSchema: UnionSchemaNode = {
261
+ * kind: 'Schema',
262
+ * type: 'union',
263
+ * members: [],
264
+ * }
265
+ * ```
98
266
  */
99
267
  export type UnionSchemaNode = CompositeSchemaNodeBase & {
268
+ /**
269
+ * Schema type discriminator.
270
+ */
100
271
  type: 'union'
101
272
  /**
102
- * Discriminator property from OAS `discriminator.propertyName`.
273
+ * Discriminator property name from OpenAPI `discriminator.propertyName`.
103
274
  */
104
275
  discriminatorPropertyName?: string
276
+ /**
277
+ * Logical strategy applied to union members: 'one' means exactly one member must be valid (from `oneOf`),
278
+ * 'any' means any number of members can be valid (from `anyOf`).
279
+ */
280
+ strategy?: 'one' | 'any'
105
281
  }
106
282
 
107
283
  /**
108
- * Intersection schema (`allOf`).
284
+ * Intersection schema, often from `allOf`.
285
+ *
286
+ * @example
287
+ * ```ts
288
+ * const intersectionSchema: IntersectionSchemaNode = {
289
+ * kind: 'Schema',
290
+ * type: 'intersection',
291
+ * members: [],
292
+ * }
293
+ * ```
109
294
  */
110
295
  export type IntersectionSchemaNode = CompositeSchemaNodeBase & {
296
+ /**
297
+ * Schema type discriminator.
298
+ */
111
299
  type: 'intersection'
112
300
  }
113
301
 
114
302
  /**
115
- * A named enum variant.
303
+ * One named enum item.
116
304
  */
117
305
  export type EnumValueNode = {
306
+ /**
307
+ * Enum item name.
308
+ */
118
309
  name: string
310
+ /**
311
+ * Enum item value.
312
+ */
119
313
  value: string | number | boolean
120
- format: 'string' | 'number' | 'boolean'
314
+ /**
315
+ * Primitive type of the enum value.
316
+ */
317
+ primitive: Extract<PrimitiveSchemaType, 'string' | 'number' | 'boolean'>
121
318
  }
122
319
 
123
320
  /**
124
- * Enum schema.
321
+ * Enum schema node.
322
+ *
323
+ * @example
324
+ * ```ts
325
+ * const enumSchema: EnumSchemaNode = {
326
+ * kind: 'Schema',
327
+ * type: 'enum',
328
+ * enumValues: ['a', 'b'],
329
+ * }
330
+ * ```
125
331
  */
126
332
  export type EnumSchemaNode = SchemaNodeBase & {
127
- type: 'enum'
128
333
  /**
129
- * Enum member type. Generators should use const assertions for `'number'` / `'boolean'`.
334
+ * Schema type discriminator.
130
335
  */
131
- enumType?: 'string' | 'number' | 'boolean'
336
+ type: 'enum'
132
337
  /**
133
- * Allowed values (simple form).
338
+ * Enum values in simple form.
134
339
  */
135
340
  enumValues?: Array<string | number | boolean | null>
136
341
  /**
137
- * Named variants (rich form). Takes priority over `enumValues` when present.
342
+ * Enum values in named form.
343
+ * If present, this is used instead of `enumValues`.
138
344
  */
139
345
  namedEnumValues?: Array<EnumValueNode>
140
346
  }
141
347
 
142
348
  /**
143
- * Ref schema pointer to another schema definition.
349
+ * Reference schema that points to another schema definition.
350
+ *
351
+ * @example
352
+ * ```ts
353
+ * const refSchema: RefSchemaNode = {
354
+ * kind: 'Schema',
355
+ * type: 'ref',
356
+ * ref: '#/components/schemas/Pet',
357
+ * }
358
+ * ```
144
359
  */
145
360
  export type RefSchemaNode = SchemaNodeBase & {
361
+ /**
362
+ * Schema type discriminator.
363
+ */
146
364
  type: 'ref'
365
+ /**
366
+ * Referenced schema name.
367
+ */
147
368
  name?: string
148
369
  /**
149
- * Original `$ref` path (e.g. `#/components/schemas/Order`). Used for name resolution.
370
+ * Original `$ref` path, for example, `#/components/schemas/Order`.
371
+ * Used to resolve names later.
150
372
  */
151
373
  ref?: string
152
374
  /**
153
- * Pattern constraint propagated from a sibling `pattern` field next to the `$ref`.
375
+ * Pattern copied from a sibling `pattern` field.
154
376
  */
155
377
  pattern?: string
378
+ /**
379
+ * The fully-parsed schema that this ref resolves to.
380
+ * Populated during OAS parsing when the referenced definition can be resolved.
381
+ * `undefined` when the ref cannot be resolved or is part of a circular chain.
382
+ *
383
+ * Useful for inspecting the referenced schema's structure (e.g. `primitive`, `properties`)
384
+ * without following the reference manually.
385
+ */
386
+ schema?: SchemaNode
156
387
  }
157
388
 
158
389
  /**
159
390
  * Datetime schema.
391
+ *
392
+ * @example
393
+ * ```ts
394
+ * const datetimeSchema: DatetimeSchemaNode = { kind: 'Schema', type: 'datetime' }
395
+ * ```
160
396
  */
161
397
  export type DatetimeSchemaNode = SchemaNodeBase & {
398
+ /**
399
+ * Schema type discriminator.
400
+ */
162
401
  type: 'datetime'
163
402
  /**
164
- * Includes timezone offset (`dateType: 'stringOffset'`).
403
+ * Whether the datetime includes a timezone offset (`dateType: 'stringOffset'`).
165
404
  */
166
405
  offset?: boolean
167
406
  /**
168
- * Local datetime without timezone (`dateType: 'stringLocal'`).
407
+ * Whether the datetime is local (no timezone, `dateType: 'stringLocal'`).
169
408
  */
170
409
  local?: boolean
171
410
  }
172
411
 
173
412
  /**
174
- * Base for `date` and `time` schemas.
413
+ * Shared base for `date` and `time` schemas.
175
414
  */
176
415
  type TemporalSchemaNodeBase<T extends 'date' | 'time'> = SchemaNodeBase & {
416
+ /**
417
+ * Schema type discriminator.
418
+ */
177
419
  type: T
178
420
  /**
179
- * Representation in generated code: native `Date` or plain string.
421
+ * Output representation in generated code.
180
422
  */
181
423
  representation: 'date' | 'string'
182
424
  }
183
425
 
184
426
  /**
185
- * Date schema.
427
+ * Date schema node.
428
+ *
429
+ * @example
430
+ * ```ts
431
+ * const dateSchema: DateSchemaNode = { kind: 'Schema', type: 'date', representation: 'string' }
432
+ * ```
186
433
  */
187
434
  export type DateSchemaNode = TemporalSchemaNodeBase<'date'>
188
435
 
189
436
  /**
190
- * Time schema.
437
+ * Time schema node.
438
+ *
439
+ * @example
440
+ * ```ts
441
+ * const timeSchema: TimeSchemaNode = { kind: 'Schema', type: 'time', representation: 'string' }
442
+ * ```
191
443
  */
192
444
  export type TimeSchemaNode = TemporalSchemaNodeBase<'time'>
193
445
 
194
446
  /**
195
- * String schema.
447
+ * String schema node.
448
+ *
449
+ * @example
450
+ * ```ts
451
+ * const stringSchema: StringSchemaNode = { kind: 'Schema', type: 'string' }
452
+ * ```
196
453
  */
197
454
  export type StringSchemaNode = SchemaNodeBase & {
455
+ /**
456
+ * Schema type discriminator.
457
+ */
198
458
  type: 'string'
459
+ /**
460
+ * Minimum string length.
461
+ */
199
462
  min?: number
463
+ /**
464
+ * Maximum string length.
465
+ */
200
466
  max?: number
467
+ /**
468
+ * Regex pattern.
469
+ */
201
470
  pattern?: string
202
471
  }
203
472
 
204
473
  /**
205
- * Number, integer, or bigint schema.
474
+ * Numeric schema (`number`, `integer`, or `bigint`).
475
+ *
476
+ * @example
477
+ * ```ts
478
+ * const numberSchema: NumberSchemaNode = { kind: 'Schema', type: 'number' }
479
+ * ```
206
480
  */
207
481
  export type NumberSchemaNode = SchemaNodeBase & {
482
+ /**
483
+ * Schema type discriminator.
484
+ */
208
485
  type: 'number' | 'integer' | 'bigint'
486
+ /**
487
+ * Minimum value.
488
+ */
209
489
  min?: number
490
+ /**
491
+ * Maximum value.
492
+ */
210
493
  max?: number
494
+ /**
495
+ * Exclusive minimum value.
496
+ */
211
497
  exclusiveMinimum?: number
498
+ /**
499
+ * Exclusive maximum value.
500
+ */
212
501
  exclusiveMaximum?: number
502
+ /**
503
+ * The value must be a multiple of this number.
504
+ */
505
+ multipleOf?: number
213
506
  }
214
507
 
215
508
  /**
216
- * Schema for scalar types with no additional constraints.
509
+ * Scalar schema with no extra constraints.
510
+ *
511
+ * @example
512
+ * ```ts
513
+ * const anySchema: ScalarSchemaNode = { kind: 'Schema', type: 'any' }
514
+ * ```
217
515
  */
218
516
  export type ScalarSchemaNode = SchemaNodeBase & {
517
+ /**
518
+ * Schema type discriminator.
519
+ */
219
520
  type: ScalarSchemaType
220
521
  }
221
522
 
222
523
  /**
223
- * URL schema, optionally carrying an Express-style path template for template literal generation.
524
+ * URL schema node.
525
+ * Can include an OpenAPI-style path template for template literal types.
526
+ *
527
+ * @example
528
+ * ```ts
529
+ * const urlSchema: UrlSchemaNode = { kind: 'Schema', type: 'url', path: '/pets/{petId}' }
530
+ * ```
224
531
  */
225
532
  export type UrlSchemaNode = SchemaNodeBase & {
533
+ /**
534
+ * Schema type discriminator.
535
+ */
226
536
  type: 'url'
227
537
  /**
228
- * Express-style path (e.g. `'/pets/:petId'`). When set, printers may emit a template literal type.
538
+ * OpenAPI-style path template, for example, `'/pets/{petId}'`.
229
539
  */
230
540
  path?: string
541
+ /**
542
+ * Minimum string length.
543
+ */
544
+ min?: number
545
+ /**
546
+ * Maximum string length.
547
+ */
548
+ max?: number
549
+ }
550
+
551
+ /**
552
+ * Format-string schema for string-based formats that support length constraints.
553
+ *
554
+ * @example
555
+ * ```ts
556
+ * const uuidSchema: FormatStringSchemaNode = { kind: 'Schema', type: 'uuid', min: 36, max: 36 }
557
+ * ```
558
+ */
559
+ export type FormatStringSchemaNode = SchemaNodeBase & {
560
+ /**
561
+ * Schema type discriminator.
562
+ */
563
+ type: 'uuid' | 'email'
564
+ /**
565
+ * Minimum string length.
566
+ */
567
+ min?: number
568
+ /**
569
+ * Maximum string length.
570
+ */
571
+ max?: number
572
+ }
573
+
574
+ /**
575
+ * IPv4 address schema node.
576
+ *
577
+ * @example
578
+ * ```ts
579
+ * const ipv4Schema: Ipv4SchemaNode = { kind: 'Schema', type: 'ipv4' }
580
+ * ```
581
+ */
582
+ export type Ipv4SchemaNode = SchemaNodeBase & {
583
+ /**
584
+ * Schema type discriminator.
585
+ */
586
+ type: 'ipv4'
587
+ }
588
+
589
+ /**
590
+ * IPv6 address schema node.
591
+ *
592
+ * @example
593
+ * ```ts
594
+ * const ipv6Schema: Ipv6SchemaNode = { kind: 'Schema', type: 'ipv6' }
595
+ * ```
596
+ */
597
+ export type Ipv6SchemaNode = SchemaNodeBase & {
598
+ /**
599
+ * Schema type discriminator.
600
+ */
601
+ type: 'ipv6'
231
602
  }
232
603
 
233
604
  /**
234
- * Maps each schema type string to its `SchemaNode` variant. Used by `narrowSchema`.
605
+ * Mapping from schema type literals to concrete schema node types.
606
+ * Used by `narrowSchema`.
235
607
  */
236
608
  export type SchemaNodeByType = {
237
609
  object: ObjectSchemaNode
@@ -254,14 +626,16 @@ export type SchemaNodeByType = {
254
626
  unknown: ScalarSchemaNode
255
627
  void: ScalarSchemaNode
256
628
  never: ScalarSchemaNode
257
- uuid: ScalarSchemaNode
258
- email: ScalarSchemaNode
629
+ uuid: FormatStringSchemaNode
630
+ email: FormatStringSchemaNode
259
631
  url: UrlSchemaNode
632
+ ipv4: Ipv4SchemaNode
633
+ ipv6: Ipv6SchemaNode
260
634
  blob: ScalarSchemaNode
261
635
  }
262
636
 
263
637
  /**
264
- * Discriminated union of all schema variants.
638
+ * Union of all schema node types.
265
639
  */
266
640
  export type SchemaNode =
267
641
  | ObjectSchemaNode
@@ -276,4 +650,7 @@ export type SchemaNode =
276
650
  | StringSchemaNode
277
651
  | NumberSchemaNode
278
652
  | UrlSchemaNode
653
+ | FormatStringSchemaNode
654
+ | Ipv4SchemaNode
655
+ | Ipv6SchemaNode
279
656
  | ScalarSchemaNode