@kubb/ast 5.0.0-alpha.4 → 5.0.0-alpha.40

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.
@@ -1,213 +1,604 @@
1
1
  import type { BaseNode } from './base.ts'
2
2
  import type { PropertyNode } from './property.ts'
3
3
 
4
- export type PrimitiveSchemaType = 'string' | 'number' | 'integer' | 'bigint' | 'boolean' | 'null' | 'any' | 'unknown' | 'void' | 'object' | 'array' | 'date'
4
+ export type PrimitiveSchemaType =
5
+ /**
6
+ * Text value.
7
+ */
8
+ | 'string'
9
+ /**
10
+ * Floating-point number.
11
+ */
12
+ | 'number'
13
+ /**
14
+ * Integer number.
15
+ */
16
+ | 'integer'
17
+ /**
18
+ * Big integer number.
19
+ */
20
+ | 'bigint'
21
+ /**
22
+ * Boolean value.
23
+ */
24
+ | 'boolean'
25
+ /**
26
+ * Null value.
27
+ */
28
+ | 'null'
29
+ /**
30
+ * Any value.
31
+ */
32
+ | 'any'
33
+ /**
34
+ * Unknown value.
35
+ */
36
+ | 'unknown'
37
+ /**
38
+ * No value (`void`).
39
+ */
40
+ | 'void'
41
+ /**
42
+ * Never value.
43
+ */
44
+ | 'never'
45
+ /**
46
+ * Object value.
47
+ */
48
+ | 'object'
49
+ /**
50
+ * Array value.
51
+ */
52
+ | 'array'
53
+ /**
54
+ * Date value.
55
+ */
56
+ | 'date'
5
57
 
58
+ /**
59
+ * Composite schema types.
60
+ */
6
61
  export type ComplexSchemaType = 'tuple' | 'union' | 'intersection' | 'enum'
7
62
 
8
63
  /**
9
- * 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.
10
65
  */
11
- export type SpecialSchemaType = 'ref' | 'datetime' | 'time' | 'uuid' | 'email' | 'url' | 'blob'
66
+ export type SpecialSchemaType = 'ref' | 'datetime' | 'time' | 'uuid' | 'email' | 'url' | 'ipv4' | 'ipv6' | 'blob'
12
67
 
68
+ /**
69
+ * All schema type strings.
70
+ */
13
71
  export type SchemaType = PrimitiveSchemaType | ComplexSchemaType | SpecialSchemaType
14
72
 
73
+ /**
74
+ * Scalar schema types without extra object/array/ref structure.
75
+ */
15
76
  export type ScalarSchemaType = Exclude<
16
77
  SchemaType,
17
- 'object' | 'array' | 'tuple' | 'union' | 'intersection' | 'enum' | 'ref' | 'datetime' | 'date' | 'time' | 'string' | 'number' | 'integer' | 'bigint'
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'
18
95
  >
19
96
 
20
97
  /**
21
- * Base fields shared by every schema variant. Does not include spec-specific fields.
98
+ * Fields shared by all schema nodes.
22
99
  */
23
100
  type SchemaNodeBase = BaseNode & {
101
+ /**
102
+ * Node kind.
103
+ */
24
104
  kind: 'Schema'
25
105
  /**
26
- * 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.
27
114
  */
28
- name?: string
29
115
  title?: string
116
+ /**
117
+ * Schema description text.
118
+ */
30
119
  description?: string
120
+ /**
121
+ * Whether `null` is allowed.
122
+ */
31
123
  nullable?: boolean
124
+ /**
125
+ * Whether the field is optional.
126
+ */
32
127
  optional?: boolean
33
128
  /**
34
129
  * Both optional and nullable (`optional` + `nullable`).
35
130
  */
36
131
  nullish?: boolean
132
+ /**
133
+ * Whether the schema is deprecated.
134
+ */
37
135
  deprecated?: boolean
136
+ /**
137
+ * Whether the schema is read-only.
138
+ */
38
139
  readOnly?: boolean
140
+ /**
141
+ * Whether the schema is write-only.
142
+ */
39
143
  writeOnly?: boolean
144
+ /**
145
+ * Default value.
146
+ */
40
147
  default?: unknown
148
+ /**
149
+ * Example value.
150
+ */
41
151
  example?: unknown
42
152
  /**
43
- * 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.
44
155
  */
45
156
  primitive?: PrimitiveSchemaType
46
157
  }
47
158
 
48
159
  /**
49
- * 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
+ * ```
50
170
  */
51
171
  export type ObjectSchemaNode = SchemaNodeBase & {
172
+ /**
173
+ * Schema type discriminator.
174
+ */
52
175
  type: 'object'
176
+ /**
177
+ * Primitive type — always `'object'` for object schemas.
178
+ */
179
+ primitive: 'object'
180
+ /**
181
+ * Ordered object properties.
182
+ */
53
183
  properties: Array<PropertyNode>
54
184
  /**
55
- * `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.
56
194
  */
57
- additionalProperties?: SchemaNode | true
58
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
59
204
  }
60
205
 
61
206
  /**
62
- * 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
+ * ```
63
217
  */
64
218
  export type ArraySchemaNode = SchemaNodeBase & {
219
+ /**
220
+ * Schema type discriminator (`array` or `tuple`).
221
+ */
65
222
  type: 'array' | 'tuple'
223
+ /**
224
+ * Item schemas.
225
+ */
66
226
  items?: Array<SchemaNode>
67
227
  /**
68
- * Additional items beyond positional `items` in a tuple.
228
+ * Tuple rest-item schema for elements beyond positional `items`.
69
229
  */
70
230
  rest?: SchemaNode
231
+ /**
232
+ * Minimum item count (or tuple length).
233
+ */
71
234
  min?: number
235
+ /**
236
+ * Maximum item count (or tuple length).
237
+ */
72
238
  max?: number
239
+ /**
240
+ * Whether all items must be unique.
241
+ */
73
242
  unique?: boolean
74
243
  }
75
244
 
76
245
  /**
77
- * Shared base for union and intersection schemas.
246
+ * Shared shape for union and intersection schemas.
78
247
  */
79
248
  type CompositeSchemaNodeBase = SchemaNodeBase & {
249
+ /**
250
+ * Member schemas.
251
+ */
80
252
  members?: Array<SchemaNode>
81
253
  }
82
254
 
83
255
  /**
84
- * 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
+ * ```
85
266
  */
86
267
  export type UnionSchemaNode = CompositeSchemaNodeBase & {
268
+ /**
269
+ * Schema type discriminator.
270
+ */
87
271
  type: 'union'
88
272
  /**
89
- * Discriminator property from OAS `discriminator.propertyName`.
273
+ * Discriminator property name from OpenAPI `discriminator.propertyName`.
90
274
  */
91
275
  discriminatorPropertyName?: string
92
276
  }
93
277
 
94
278
  /**
95
- * 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
+ * ```
96
289
  */
97
290
  export type IntersectionSchemaNode = CompositeSchemaNodeBase & {
291
+ /**
292
+ * Schema type discriminator.
293
+ */
98
294
  type: 'intersection'
99
295
  }
100
296
 
101
297
  /**
102
- * A named enum variant.
298
+ * One named enum item.
103
299
  */
104
300
  export type EnumValueNode = {
301
+ /**
302
+ * Enum item name.
303
+ */
105
304
  name: string
305
+ /**
306
+ * Enum item value.
307
+ */
106
308
  value: string | number | boolean
107
- format: 'string' | 'number' | 'boolean'
309
+ /**
310
+ * Primitive type of the enum value.
311
+ */
312
+ primitive: Extract<PrimitiveSchemaType, 'string' | 'number' | 'boolean'>
108
313
  }
109
314
 
110
315
  /**
111
- * 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
+ * ```
112
326
  */
113
327
  export type EnumSchemaNode = SchemaNodeBase & {
114
- type: 'enum'
115
328
  /**
116
- * Enum member type. Generators should use const assertions for `'number'` / `'boolean'`.
329
+ * Schema type discriminator.
117
330
  */
118
- enumType?: 'string' | 'number' | 'boolean'
331
+ type: 'enum'
119
332
  /**
120
- * Allowed values (simple form).
333
+ * Enum values in simple form.
121
334
  */
122
335
  enumValues?: Array<string | number | boolean | null>
123
336
  /**
124
- * 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`.
125
339
  */
126
340
  namedEnumValues?: Array<EnumValueNode>
127
341
  }
128
342
 
129
343
  /**
130
- * 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
+ * ```
131
354
  */
132
355
  export type RefSchemaNode = SchemaNodeBase & {
356
+ /**
357
+ * Schema type discriminator.
358
+ */
133
359
  type: 'ref'
360
+ /**
361
+ * Referenced schema name.
362
+ */
134
363
  name?: string
135
364
  /**
136
- * 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.
137
367
  */
138
368
  ref?: string
139
369
  /**
140
- * Pattern constraint propagated from a sibling `pattern` field next to the `$ref`.
370
+ * Pattern copied from a sibling `pattern` field.
141
371
  */
142
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
143
382
  }
144
383
 
145
384
  /**
146
385
  * Datetime schema.
386
+ *
387
+ * @example
388
+ * ```ts
389
+ * const datetimeSchema: DatetimeSchemaNode = { kind: 'Schema', type: 'datetime' }
390
+ * ```
147
391
  */
148
392
  export type DatetimeSchemaNode = SchemaNodeBase & {
393
+ /**
394
+ * Schema type discriminator.
395
+ */
149
396
  type: 'datetime'
150
397
  /**
151
- * Includes timezone offset (`dateType: 'stringOffset'`).
398
+ * Whether the datetime includes a timezone offset (`dateType: 'stringOffset'`).
152
399
  */
153
400
  offset?: boolean
154
401
  /**
155
- * Local datetime without timezone (`dateType: 'stringLocal'`).
402
+ * Whether the datetime is local (no timezone, `dateType: 'stringLocal'`).
156
403
  */
157
404
  local?: boolean
158
405
  }
159
406
 
160
407
  /**
161
- * Base for `date` and `time` schemas.
408
+ * Shared base for `date` and `time` schemas.
162
409
  */
163
410
  type TemporalSchemaNodeBase<T extends 'date' | 'time'> = SchemaNodeBase & {
411
+ /**
412
+ * Schema type discriminator.
413
+ */
164
414
  type: T
165
415
  /**
166
- * Representation in generated code: native `Date` or plain string.
416
+ * Output representation in generated code.
167
417
  */
168
418
  representation: 'date' | 'string'
169
419
  }
170
420
 
171
421
  /**
172
- * Date schema.
422
+ * Date schema node.
423
+ *
424
+ * @example
425
+ * ```ts
426
+ * const dateSchema: DateSchemaNode = { kind: 'Schema', type: 'date', representation: 'string' }
427
+ * ```
173
428
  */
174
429
  export type DateSchemaNode = TemporalSchemaNodeBase<'date'>
175
430
 
176
431
  /**
177
- * Time schema.
432
+ * Time schema node.
433
+ *
434
+ * @example
435
+ * ```ts
436
+ * const timeSchema: TimeSchemaNode = { kind: 'Schema', type: 'time', representation: 'string' }
437
+ * ```
178
438
  */
179
439
  export type TimeSchemaNode = TemporalSchemaNodeBase<'time'>
180
440
 
181
441
  /**
182
- * String schema.
442
+ * String schema node.
443
+ *
444
+ * @example
445
+ * ```ts
446
+ * const stringSchema: StringSchemaNode = { kind: 'Schema', type: 'string' }
447
+ * ```
183
448
  */
184
449
  export type StringSchemaNode = SchemaNodeBase & {
450
+ /**
451
+ * Schema type discriminator.
452
+ */
185
453
  type: 'string'
454
+ /**
455
+ * Minimum string length.
456
+ */
186
457
  min?: number
458
+ /**
459
+ * Maximum string length.
460
+ */
187
461
  max?: number
462
+ /**
463
+ * Regex pattern.
464
+ */
188
465
  pattern?: string
189
466
  }
190
467
 
191
468
  /**
192
- * 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
+ * ```
193
475
  */
194
476
  export type NumberSchemaNode = SchemaNodeBase & {
477
+ /**
478
+ * Schema type discriminator.
479
+ */
195
480
  type: 'number' | 'integer' | 'bigint'
481
+ /**
482
+ * Minimum value.
483
+ */
196
484
  min?: number
485
+ /**
486
+ * Maximum value.
487
+ */
197
488
  max?: number
489
+ /**
490
+ * Exclusive minimum value.
491
+ */
198
492
  exclusiveMinimum?: number
493
+ /**
494
+ * Exclusive maximum value.
495
+ */
199
496
  exclusiveMaximum?: number
497
+ /**
498
+ * The value must be a multiple of this number.
499
+ */
500
+ multipleOf?: number
200
501
  }
201
502
 
202
503
  /**
203
- * 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
+ * ```
204
510
  */
205
511
  export type ScalarSchemaNode = SchemaNodeBase & {
512
+ /**
513
+ * Schema type discriminator.
514
+ */
206
515
  type: ScalarSchemaType
207
516
  }
208
517
 
209
518
  /**
210
- * Maps each schema type string to its `SchemaNode` variant. Used by `narrowSchema`.
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
+ * ```
526
+ */
527
+ export type UrlSchemaNode = SchemaNodeBase & {
528
+ /**
529
+ * Schema type discriminator.
530
+ */
531
+ type: 'url'
532
+ /**
533
+ * OpenAPI-style path template, for example, `'/pets/{petId}'`.
534
+ */
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'
597
+ }
598
+
599
+ /**
600
+ * Mapping from schema type literals to concrete schema node types.
601
+ * Used by `narrowSchema`.
211
602
  */
212
603
  export type SchemaNodeByType = {
213
604
  object: ObjectSchemaNode
@@ -229,14 +620,17 @@ export type SchemaNodeByType = {
229
620
  any: ScalarSchemaNode
230
621
  unknown: ScalarSchemaNode
231
622
  void: ScalarSchemaNode
232
- uuid: ScalarSchemaNode
233
- email: ScalarSchemaNode
234
- url: ScalarSchemaNode
623
+ never: ScalarSchemaNode
624
+ uuid: FormatStringSchemaNode
625
+ email: FormatStringSchemaNode
626
+ url: UrlSchemaNode
627
+ ipv4: Ipv4SchemaNode
628
+ ipv6: Ipv6SchemaNode
235
629
  blob: ScalarSchemaNode
236
630
  }
237
631
 
238
632
  /**
239
- * Discriminated union of all schema variants.
633
+ * Union of all schema node types.
240
634
  */
241
635
  export type SchemaNode =
242
636
  | ObjectSchemaNode
@@ -250,4 +644,8 @@ export type SchemaNode =
250
644
  | TimeSchemaNode
251
645
  | StringSchemaNode
252
646
  | NumberSchemaNode
647
+ | UrlSchemaNode
648
+ | FormatStringSchemaNode
649
+ | Ipv4SchemaNode
650
+ | Ipv6SchemaNode
253
651
  | ScalarSchemaNode