@kubb/ast 5.0.0-alpha.6 → 5.0.0-alpha.60

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,603 @@ 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
105
276
  }
106
277
 
107
278
  /**
108
- * Intersection schema (`allOf`).
279
+ * Intersection schema, often from `allOf`.
280
+ *
281
+ * @example
282
+ * ```ts
283
+ * const intersectionSchema: IntersectionSchemaNode = {
284
+ * kind: 'Schema',
285
+ * type: 'intersection',
286
+ * members: [],
287
+ * }
288
+ * ```
109
289
  */
110
290
  export type IntersectionSchemaNode = CompositeSchemaNodeBase & {
291
+ /**
292
+ * Schema type discriminator.
293
+ */
111
294
  type: 'intersection'
112
295
  }
113
296
 
114
297
  /**
115
- * A named enum variant.
298
+ * One named enum item.
116
299
  */
117
300
  export type EnumValueNode = {
301
+ /**
302
+ * Enum item name.
303
+ */
118
304
  name: string
305
+ /**
306
+ * Enum item value.
307
+ */
119
308
  value: string | number | boolean
120
- format: 'string' | 'number' | 'boolean'
309
+ /**
310
+ * Primitive type of the enum value.
311
+ */
312
+ primitive: Extract<PrimitiveSchemaType, 'string' | 'number' | 'boolean'>
121
313
  }
122
314
 
123
315
  /**
124
- * Enum schema.
316
+ * Enum schema node.
317
+ *
318
+ * @example
319
+ * ```ts
320
+ * const enumSchema: EnumSchemaNode = {
321
+ * kind: 'Schema',
322
+ * type: 'enum',
323
+ * enumValues: ['a', 'b'],
324
+ * }
325
+ * ```
125
326
  */
126
327
  export type EnumSchemaNode = SchemaNodeBase & {
127
- type: 'enum'
128
328
  /**
129
- * Enum member type. Generators should use const assertions for `'number'` / `'boolean'`.
329
+ * Schema type discriminator.
130
330
  */
131
- enumType?: 'string' | 'number' | 'boolean'
331
+ type: 'enum'
132
332
  /**
133
- * Allowed values (simple form).
333
+ * Enum values in simple form.
134
334
  */
135
335
  enumValues?: Array<string | number | boolean | null>
136
336
  /**
137
- * Named variants (rich form). Takes priority over `enumValues` when present.
337
+ * Enum values in named form.
338
+ * If present, this is used instead of `enumValues`.
138
339
  */
139
340
  namedEnumValues?: Array<EnumValueNode>
140
341
  }
141
342
 
142
343
  /**
143
- * Ref schema pointer to another schema definition.
344
+ * Reference schema that points to another schema definition.
345
+ *
346
+ * @example
347
+ * ```ts
348
+ * const refSchema: RefSchemaNode = {
349
+ * kind: 'Schema',
350
+ * type: 'ref',
351
+ * ref: '#/components/schemas/Pet',
352
+ * }
353
+ * ```
144
354
  */
145
355
  export type RefSchemaNode = SchemaNodeBase & {
356
+ /**
357
+ * Schema type discriminator.
358
+ */
146
359
  type: 'ref'
360
+ /**
361
+ * Referenced schema name.
362
+ */
147
363
  name?: string
148
364
  /**
149
- * Original `$ref` path (e.g. `#/components/schemas/Order`). Used for name resolution.
365
+ * Original `$ref` path, for example, `#/components/schemas/Order`.
366
+ * Used to resolve names later.
150
367
  */
151
368
  ref?: string
152
369
  /**
153
- * Pattern constraint propagated from a sibling `pattern` field next to the `$ref`.
370
+ * Pattern copied from a sibling `pattern` field.
154
371
  */
155
372
  pattern?: string
373
+ /**
374
+ * The fully-parsed schema that this ref resolves to.
375
+ * Populated during OAS parsing when the referenced definition can be resolved.
376
+ * `undefined` when the ref cannot be resolved or is part of a circular chain.
377
+ *
378
+ * Useful for inspecting the referenced schema's structure (e.g. `primitive`, `properties`)
379
+ * without following the reference manually.
380
+ */
381
+ schema?: SchemaNode
156
382
  }
157
383
 
158
384
  /**
159
385
  * Datetime schema.
386
+ *
387
+ * @example
388
+ * ```ts
389
+ * const datetimeSchema: DatetimeSchemaNode = { kind: 'Schema', type: 'datetime' }
390
+ * ```
160
391
  */
161
392
  export type DatetimeSchemaNode = SchemaNodeBase & {
393
+ /**
394
+ * Schema type discriminator.
395
+ */
162
396
  type: 'datetime'
163
397
  /**
164
- * Includes timezone offset (`dateType: 'stringOffset'`).
398
+ * Whether the datetime includes a timezone offset (`dateType: 'stringOffset'`).
165
399
  */
166
400
  offset?: boolean
167
401
  /**
168
- * Local datetime without timezone (`dateType: 'stringLocal'`).
402
+ * Whether the datetime is local (no timezone, `dateType: 'stringLocal'`).
169
403
  */
170
404
  local?: boolean
171
405
  }
172
406
 
173
407
  /**
174
- * Base for `date` and `time` schemas.
408
+ * Shared base for `date` and `time` schemas.
175
409
  */
176
410
  type TemporalSchemaNodeBase<T extends 'date' | 'time'> = SchemaNodeBase & {
411
+ /**
412
+ * Schema type discriminator.
413
+ */
177
414
  type: T
178
415
  /**
179
- * Representation in generated code: native `Date` or plain string.
416
+ * Output representation in generated code.
180
417
  */
181
418
  representation: 'date' | 'string'
182
419
  }
183
420
 
184
421
  /**
185
- * Date schema.
422
+ * Date schema node.
423
+ *
424
+ * @example
425
+ * ```ts
426
+ * const dateSchema: DateSchemaNode = { kind: 'Schema', type: 'date', representation: 'string' }
427
+ * ```
186
428
  */
187
429
  export type DateSchemaNode = TemporalSchemaNodeBase<'date'>
188
430
 
189
431
  /**
190
- * Time schema.
432
+ * Time schema node.
433
+ *
434
+ * @example
435
+ * ```ts
436
+ * const timeSchema: TimeSchemaNode = { kind: 'Schema', type: 'time', representation: 'string' }
437
+ * ```
191
438
  */
192
439
  export type TimeSchemaNode = TemporalSchemaNodeBase<'time'>
193
440
 
194
441
  /**
195
- * String schema.
442
+ * String schema node.
443
+ *
444
+ * @example
445
+ * ```ts
446
+ * const stringSchema: StringSchemaNode = { kind: 'Schema', type: 'string' }
447
+ * ```
196
448
  */
197
449
  export type StringSchemaNode = SchemaNodeBase & {
450
+ /**
451
+ * Schema type discriminator.
452
+ */
198
453
  type: 'string'
454
+ /**
455
+ * Minimum string length.
456
+ */
199
457
  min?: number
458
+ /**
459
+ * Maximum string length.
460
+ */
200
461
  max?: number
462
+ /**
463
+ * Regex pattern.
464
+ */
201
465
  pattern?: string
202
466
  }
203
467
 
204
468
  /**
205
- * Number, integer, or bigint schema.
469
+ * Numeric schema (`number`, `integer`, or `bigint`).
470
+ *
471
+ * @example
472
+ * ```ts
473
+ * const numberSchema: NumberSchemaNode = { kind: 'Schema', type: 'number' }
474
+ * ```
206
475
  */
207
476
  export type NumberSchemaNode = SchemaNodeBase & {
477
+ /**
478
+ * Schema type discriminator.
479
+ */
208
480
  type: 'number' | 'integer' | 'bigint'
481
+ /**
482
+ * Minimum value.
483
+ */
209
484
  min?: number
485
+ /**
486
+ * Maximum value.
487
+ */
210
488
  max?: number
489
+ /**
490
+ * Exclusive minimum value.
491
+ */
211
492
  exclusiveMinimum?: number
493
+ /**
494
+ * Exclusive maximum value.
495
+ */
212
496
  exclusiveMaximum?: number
497
+ /**
498
+ * The value must be a multiple of this number.
499
+ */
500
+ multipleOf?: number
213
501
  }
214
502
 
215
503
  /**
216
- * Schema for scalar types with no additional constraints.
504
+ * Scalar schema with no extra constraints.
505
+ *
506
+ * @example
507
+ * ```ts
508
+ * const anySchema: ScalarSchemaNode = { kind: 'Schema', type: 'any' }
509
+ * ```
217
510
  */
218
511
  export type ScalarSchemaNode = SchemaNodeBase & {
512
+ /**
513
+ * Schema type discriminator.
514
+ */
219
515
  type: ScalarSchemaType
220
516
  }
221
517
 
222
518
  /**
223
- * URL schema, optionally carrying an Express-style path template for template literal generation.
519
+ * URL schema node.
520
+ * Can include an OpenAPI-style path template for template literal types.
521
+ *
522
+ * @example
523
+ * ```ts
524
+ * const urlSchema: UrlSchemaNode = { kind: 'Schema', type: 'url', path: '/pets/{petId}' }
525
+ * ```
224
526
  */
225
527
  export type UrlSchemaNode = SchemaNodeBase & {
528
+ /**
529
+ * Schema type discriminator.
530
+ */
226
531
  type: 'url'
227
532
  /**
228
- * Express-style path (e.g. `'/pets/:petId'`). When set, printers may emit a template literal type.
533
+ * OpenAPI-style path template, for example, `'/pets/{petId}'`.
229
534
  */
230
535
  path?: string
536
+ /**
537
+ * Minimum string length.
538
+ */
539
+ min?: number
540
+ /**
541
+ * Maximum string length.
542
+ */
543
+ max?: number
544
+ }
545
+
546
+ /**
547
+ * Format-string schema for string-based formats that support length constraints.
548
+ *
549
+ * @example
550
+ * ```ts
551
+ * const uuidSchema: FormatStringSchemaNode = { kind: 'Schema', type: 'uuid', min: 36, max: 36 }
552
+ * ```
553
+ */
554
+ export type FormatStringSchemaNode = SchemaNodeBase & {
555
+ /**
556
+ * Schema type discriminator.
557
+ */
558
+ type: 'uuid' | 'email'
559
+ /**
560
+ * Minimum string length.
561
+ */
562
+ min?: number
563
+ /**
564
+ * Maximum string length.
565
+ */
566
+ max?: number
567
+ }
568
+
569
+ /**
570
+ * IPv4 address schema node.
571
+ *
572
+ * @example
573
+ * ```ts
574
+ * const ipv4Schema: Ipv4SchemaNode = { kind: 'Schema', type: 'ipv4' }
575
+ * ```
576
+ */
577
+ export type Ipv4SchemaNode = SchemaNodeBase & {
578
+ /**
579
+ * Schema type discriminator.
580
+ */
581
+ type: 'ipv4'
582
+ }
583
+
584
+ /**
585
+ * IPv6 address schema node.
586
+ *
587
+ * @example
588
+ * ```ts
589
+ * const ipv6Schema: Ipv6SchemaNode = { kind: 'Schema', type: 'ipv6' }
590
+ * ```
591
+ */
592
+ export type Ipv6SchemaNode = SchemaNodeBase & {
593
+ /**
594
+ * Schema type discriminator.
595
+ */
596
+ type: 'ipv6'
231
597
  }
232
598
 
233
599
  /**
234
- * Maps each schema type string to its `SchemaNode` variant. Used by `narrowSchema`.
600
+ * Mapping from schema type literals to concrete schema node types.
601
+ * Used by `narrowSchema`.
235
602
  */
236
603
  export type SchemaNodeByType = {
237
604
  object: ObjectSchemaNode
@@ -254,14 +621,16 @@ export type SchemaNodeByType = {
254
621
  unknown: ScalarSchemaNode
255
622
  void: ScalarSchemaNode
256
623
  never: ScalarSchemaNode
257
- uuid: ScalarSchemaNode
258
- email: ScalarSchemaNode
624
+ uuid: FormatStringSchemaNode
625
+ email: FormatStringSchemaNode
259
626
  url: UrlSchemaNode
627
+ ipv4: Ipv4SchemaNode
628
+ ipv6: Ipv6SchemaNode
260
629
  blob: ScalarSchemaNode
261
630
  }
262
631
 
263
632
  /**
264
- * Discriminated union of all schema variants.
633
+ * Union of all schema node types.
265
634
  */
266
635
  export type SchemaNode =
267
636
  | ObjectSchemaNode
@@ -276,4 +645,7 @@ export type SchemaNode =
276
645
  | StringSchemaNode
277
646
  | NumberSchemaNode
278
647
  | UrlSchemaNode
648
+ | FormatStringSchemaNode
649
+ | Ipv4SchemaNode
650
+ | Ipv6SchemaNode
279
651
  | ScalarSchemaNode