@amritk/helpers 0.7.1 → 0.9.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.
Files changed (83) hide show
  1. package/dist/build-dynamic-ref-map.d.ts +0 -1
  2. package/dist/derive-root-type-name.d.ts +0 -1
  3. package/dist/escape-regex-pattern.d.ts +16 -0
  4. package/dist/escape-regex-pattern.js +20 -0
  5. package/dist/extract-dynamic-anchor-defs.d.ts +0 -1
  6. package/dist/extract-refs.d.ts +0 -1
  7. package/dist/generate-index-barrel.d.ts +0 -1
  8. package/dist/generate-type-definition.d.ts +0 -1
  9. package/dist/has-ref.d.ts +0 -1
  10. package/dist/is-object.d.ts +0 -1
  11. package/dist/mjst-extension.d.ts +0 -1
  12. package/dist/parse-documentation.d.ts +0 -1
  13. package/dist/ref-to-filename.d.ts +0 -1
  14. package/dist/ref-to-name.d.ts +0 -1
  15. package/dist/resolve-dynamic-refs.d.ts +4 -4
  16. package/dist/resolve-dynamic-refs.js +10 -4
  17. package/dist/resolve-ref.d.ts +0 -1
  18. package/dist/safe-accessor.d.ts +0 -1
  19. package/dist/schema-guards.d.ts +16 -1
  20. package/dist/schema-guards.js +29 -0
  21. package/dist/upgrade-draft07-schema.d.ts +0 -1
  22. package/dist/validate-array.d.ts +0 -1
  23. package/dist/validate-record.d.ts +0 -1
  24. package/dist/walk-ref-graph.d.ts +0 -1
  25. package/package.json +2 -4
  26. package/dist/build-dynamic-ref-map.d.ts.map +0 -1
  27. package/dist/derive-root-type-name.d.ts.map +0 -1
  28. package/dist/extract-dynamic-anchor-defs.d.ts.map +0 -1
  29. package/dist/extract-refs.d.ts.map +0 -1
  30. package/dist/generate-index-barrel.d.ts.map +0 -1
  31. package/dist/generate-type-definition.d.ts.map +0 -1
  32. package/dist/has-ref.d.ts.map +0 -1
  33. package/dist/is-object.d.ts.map +0 -1
  34. package/dist/mjst-extension.d.ts.map +0 -1
  35. package/dist/parse-documentation.d.ts.map +0 -1
  36. package/dist/ref-to-filename.d.ts.map +0 -1
  37. package/dist/ref-to-name.d.ts.map +0 -1
  38. package/dist/resolve-dynamic-refs.d.ts.map +0 -1
  39. package/dist/resolve-ref.d.ts.map +0 -1
  40. package/dist/safe-accessor.d.ts.map +0 -1
  41. package/dist/schema-guards.d.ts.map +0 -1
  42. package/dist/upgrade-draft07-schema.d.ts.map +0 -1
  43. package/dist/validate-array.d.ts.map +0 -1
  44. package/dist/validate-record.d.ts.map +0 -1
  45. package/dist/walk-ref-graph.d.ts.map +0 -1
  46. package/src/build-dynamic-ref-map.test.ts +0 -65
  47. package/src/build-dynamic-ref-map.ts +0 -42
  48. package/src/derive-root-type-name.test.ts +0 -42
  49. package/src/derive-root-type-name.ts +0 -42
  50. package/src/extract-dynamic-anchor-defs.test.ts +0 -29
  51. package/src/extract-dynamic-anchor-defs.ts +0 -33
  52. package/src/extract-refs.test.ts +0 -183
  53. package/src/extract-refs.ts +0 -73
  54. package/src/generate-index-barrel.test.ts +0 -41
  55. package/src/generate-index-barrel.ts +0 -62
  56. package/src/generate-type-definition.test.ts +0 -1386
  57. package/src/generate-type-definition.ts +0 -580
  58. package/src/has-ref.ts +0 -10
  59. package/src/is-object.test.ts +0 -65
  60. package/src/is-object.ts +0 -15
  61. package/src/mjst-extension.test.ts +0 -59
  62. package/src/mjst-extension.ts +0 -81
  63. package/src/parse-documentation.test.ts +0 -313
  64. package/src/parse-documentation.ts +0 -173
  65. package/src/ref-to-filename.test.ts +0 -64
  66. package/src/ref-to-filename.ts +0 -116
  67. package/src/ref-to-name.test.ts +0 -57
  68. package/src/ref-to-name.ts +0 -42
  69. package/src/resolve-dynamic-refs.test.ts +0 -94
  70. package/src/resolve-dynamic-refs.ts +0 -48
  71. package/src/resolve-ref.test.ts +0 -186
  72. package/src/resolve-ref.ts +0 -82
  73. package/src/safe-accessor.test.ts +0 -41
  74. package/src/safe-accessor.ts +0 -48
  75. package/src/schema-guards.test.ts +0 -406
  76. package/src/schema-guards.ts +0 -160
  77. package/src/upgrade-draft07-schema.ts +0 -179
  78. package/src/validate-array.test.ts +0 -52
  79. package/src/validate-array.ts +0 -15
  80. package/src/validate-record.test.ts +0 -56
  81. package/src/validate-record.ts +0 -19
  82. package/src/walk-ref-graph.test.ts +0 -118
  83. package/src/walk-ref-graph.ts +0 -184
@@ -1,1386 +0,0 @@
1
- import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
2
- import { describe, expect, it } from 'vitest'
3
-
4
- import { generateTypeDefinition } from './generate-type-definition'
5
-
6
- describe('generateTypeDefinition', () => {
7
- it('generates type for deeply nested objects', () => {
8
- const schema = {
9
- type: 'object',
10
- properties: {
11
- user: {
12
- type: 'object',
13
- properties: {
14
- profile: {
15
- type: 'object',
16
- properties: {
17
- address: {
18
- type: 'object',
19
- properties: {
20
- street: { type: 'string' },
21
- city: { type: 'string' },
22
- zipCode: { type: 'string' },
23
- },
24
- required: ['street', 'city'],
25
- },
26
- },
27
- },
28
- },
29
- },
30
- },
31
- }
32
-
33
- const result = generateTypeDefinition(schema, 'DeeplyNested')
34
-
35
- expect(result).toStrictEqual(
36
- 'export type DeeplyNested = {\n' +
37
- ' user?: { profile?: { address?: { street: string; city: string; zipCode?: string } } };\n' +
38
- '};',
39
- )
40
- })
41
-
42
- it('generates type for array of nested objects', () => {
43
- const schema = {
44
- type: 'object',
45
- properties: {
46
- users: {
47
- type: 'array',
48
- items: {
49
- type: 'object',
50
- properties: {
51
- id: { type: 'number' },
52
- name: { type: 'string' },
53
- email: { type: 'string' },
54
- },
55
- required: ['id', 'name'],
56
- },
57
- },
58
- },
59
- required: ['users'],
60
- }
61
-
62
- const result = generateTypeDefinition(schema, 'UserList')
63
-
64
- expect(result).toStrictEqual(
65
- 'export type UserList = {\n' + ' users: { id: number; name: string; email?: string }[];\n' + '};',
66
- )
67
- })
68
-
69
- it('generates type for nested arrays', () => {
70
- const schema = {
71
- type: 'object',
72
- properties: {
73
- matrix: {
74
- type: 'array',
75
- items: {
76
- type: 'array',
77
- items: {
78
- type: 'number',
79
- },
80
- },
81
- },
82
- },
83
- }
84
-
85
- const result = generateTypeDefinition(schema, 'Matrix')
86
-
87
- expect(result).toStrictEqual('export type Matrix = {\n' + ' matrix?: number[][];\n' + '};')
88
- })
89
-
90
- it('generates type for mixed required and optional fields', () => {
91
- const schema = {
92
- type: 'object',
93
- properties: {
94
- id: { type: 'string' },
95
- name: { type: 'string' },
96
- email: { type: 'string' },
97
- age: { type: 'number' },
98
- active: { type: 'boolean' },
99
- tags: { type: 'array', items: { type: 'string' } },
100
- },
101
- required: ['id', 'name', 'email'],
102
- }
103
-
104
- const result = generateTypeDefinition(schema, 'MixedFields')
105
-
106
- expect(result).toStrictEqual(
107
- 'export type MixedFields = {\n' +
108
- ' id: string;\n' +
109
- ' name: string;\n' +
110
- ' email: string;\n' +
111
- ' age?: number;\n' +
112
- ' active?: boolean;\n' +
113
- ' tags?: string[];\n' +
114
- '};',
115
- )
116
- })
117
-
118
- it('generates type for object with all primitive types', () => {
119
- const schema = {
120
- type: 'object',
121
- properties: {
122
- stringField: { type: 'string' },
123
- numberField: { type: 'number' },
124
- integerField: { type: 'integer' },
125
- booleanField: { type: 'boolean' },
126
- },
127
- required: ['stringField', 'numberField', 'integerField', 'booleanField'],
128
- }
129
-
130
- const result = generateTypeDefinition(schema, 'AllPrimitives')
131
-
132
- expect(result).toStrictEqual(
133
- 'export type AllPrimitives = {\n' +
134
- ' stringField: string;\n' +
135
- ' numberField: number;\n' +
136
- ' integerField: number;\n' +
137
- ' booleanField: boolean;\n' +
138
- '};',
139
- )
140
- })
141
-
142
- it('generates type for array without items definition', () => {
143
- const schema = {
144
- type: 'object',
145
- properties: {
146
- data: {
147
- type: 'array',
148
- },
149
- },
150
- }
151
-
152
- const result = generateTypeDefinition(schema, 'UnknownArray')
153
-
154
- expect(result).toStrictEqual('export type UnknownArray = {\n' + ' data?: unknown[];\n' + '};')
155
- })
156
-
157
- it('generates type for object without properties', () => {
158
- const schema = {
159
- type: 'object',
160
- properties: {
161
- metadata: {
162
- type: 'object',
163
- },
164
- },
165
- }
166
-
167
- const result = generateTypeDefinition(schema, 'Generic')
168
-
169
- expect(result).toStrictEqual('export type Generic = {\n' + ' metadata?: object;\n' + '};')
170
- })
171
-
172
- it('generates type for complex nested structure with arrays and objects', () => {
173
- const schema = {
174
- type: 'object',
175
- properties: {
176
- company: {
177
- type: 'object',
178
- properties: {
179
- name: { type: 'string' },
180
- departments: {
181
- type: 'array',
182
- items: {
183
- type: 'object',
184
- properties: {
185
- name: { type: 'string' },
186
- employees: {
187
- type: 'array',
188
- items: {
189
- type: 'object',
190
- properties: {
191
- id: { type: 'number' },
192
- name: { type: 'string' },
193
- },
194
- required: ['id'],
195
- },
196
- },
197
- },
198
- required: ['name'],
199
- },
200
- },
201
- },
202
- required: ['name'],
203
- },
204
- },
205
- required: ['company'],
206
- }
207
-
208
- const result = generateTypeDefinition(schema, 'Company')
209
-
210
- expect(result).toStrictEqual(
211
- 'export type Company = {\n' +
212
- ' company: { name: string; departments?: { name: string; employees?: { id: number; name?: string }[] }[] };\n' +
213
- '};',
214
- )
215
- })
216
-
217
- it('generates type for empty object schema', () => {
218
- const schema = {
219
- type: 'object',
220
- properties: {},
221
- }
222
-
223
- const result = generateTypeDefinition(schema, 'Empty')
224
-
225
- expect(result).toStrictEqual('export type Empty = {\n' + '\n' + '};')
226
- })
227
-
228
- it('generates type for object with no type specified', () => {
229
- const schema = {
230
- properties: {
231
- field: { type: 'string' },
232
- },
233
- }
234
-
235
- const result = generateTypeDefinition(schema, 'NoType')
236
-
237
- expect(result).toStrictEqual('export type NoType = {\n' + ' field?: string;\n' + '};')
238
- })
239
-
240
- it('generates type for boolean schema true (any value valid)', () => {
241
- const schema = true
242
-
243
- const result = generateTypeDefinition(schema, 'BooleanSchema')
244
-
245
- expect(result).toStrictEqual('export type BooleanSchema = unknown;')
246
- })
247
-
248
- it('generates type for boolean schema false (no value valid)', () => {
249
- const schema = false
250
-
251
- const result = generateTypeDefinition(schema, 'NeverSchema')
252
-
253
- expect(result).toStrictEqual('export type NeverSchema = never;')
254
- })
255
-
256
- it('generates type for array of arrays of objects', () => {
257
- const schema = {
258
- type: 'object',
259
- properties: {
260
- grid: {
261
- type: 'array',
262
- items: {
263
- type: 'array',
264
- items: {
265
- type: 'object',
266
- properties: {
267
- x: { type: 'number' },
268
- y: { type: 'number' },
269
- value: { type: 'string' },
270
- },
271
- required: ['x', 'y'],
272
- },
273
- },
274
- },
275
- },
276
- }
277
-
278
- const result = generateTypeDefinition(schema, 'Grid')
279
-
280
- expect(result).toStrictEqual(
281
- 'export type Grid = {\n' + ' grid?: { x: number; y: number; value?: string }[][];\n' + '};',
282
- )
283
- })
284
-
285
- it('generates type for object with all fields required', () => {
286
- const schema = {
287
- type: 'object',
288
- properties: {
289
- id: { type: 'string' },
290
- name: { type: 'string' },
291
- email: { type: 'string' },
292
- age: { type: 'number' },
293
- },
294
- required: ['id', 'name', 'email', 'age'],
295
- }
296
-
297
- const result = generateTypeDefinition(schema, 'AllRequired')
298
-
299
- expect(result).toStrictEqual(
300
- 'export type AllRequired = {\n' +
301
- ' id: string;\n' +
302
- ' name: string;\n' +
303
- ' email: string;\n' +
304
- ' age: number;\n' +
305
- '};',
306
- )
307
- })
308
-
309
- it('generates type for object with no fields required', () => {
310
- const schema = {
311
- type: 'object',
312
- properties: {
313
- id: { type: 'string' },
314
- name: { type: 'string' },
315
- email: { type: 'string' },
316
- },
317
- }
318
-
319
- const result = generateTypeDefinition(schema, 'AllOptional')
320
-
321
- expect(result).toStrictEqual(
322
- 'export type AllOptional = {\n' + ' id?: string;\n' + ' name?: string;\n' + ' email?: string;\n' + '};',
323
- )
324
- })
325
-
326
- it('generates type for complex API response structure', () => {
327
- const schema = {
328
- type: 'object',
329
- properties: {
330
- status: { type: 'string' },
331
- data: {
332
- type: 'object',
333
- properties: {
334
- users: {
335
- type: 'array',
336
- items: {
337
- type: 'object',
338
- properties: {
339
- id: { type: 'number' },
340
- username: { type: 'string' },
341
- profile: {
342
- type: 'object',
343
- properties: {
344
- avatar: { type: 'string' },
345
- bio: { type: 'string' },
346
- },
347
- },
348
- },
349
- required: ['id', 'username'],
350
- },
351
- },
352
- pagination: {
353
- type: 'object',
354
- properties: {
355
- page: { type: 'number' },
356
- perPage: { type: 'number' },
357
- total: { type: 'number' },
358
- },
359
- required: ['page', 'perPage', 'total'],
360
- },
361
- },
362
- required: ['users'],
363
- },
364
- error: {
365
- type: 'object',
366
- properties: {
367
- code: { type: 'string' },
368
- message: { type: 'string' },
369
- },
370
- },
371
- },
372
- required: ['status'],
373
- }
374
-
375
- const result = generateTypeDefinition(schema, 'APIResponse')
376
-
377
- expect(result).toStrictEqual(
378
- 'export type APIResponse = {\n' +
379
- ' status: string;\n' +
380
- ' data?: { users: { id: number; username: string; profile?: { avatar?: string; bio?: string } }[]; pagination?: { page: number; perPage: number; total: number } };\n' +
381
- ' error?: { code?: string; message?: string };\n' +
382
- '};',
383
- )
384
- })
385
-
386
- it('generates type for array of different primitive types', () => {
387
- const schema = {
388
- type: 'object',
389
- properties: {
390
- strings: {
391
- type: 'array',
392
- items: { type: 'string' },
393
- },
394
- numbers: {
395
- type: 'array',
396
- items: { type: 'number' },
397
- },
398
- booleans: {
399
- type: 'array',
400
- items: { type: 'boolean' },
401
- },
402
- },
403
- }
404
-
405
- const result = generateTypeDefinition(schema, 'ArrayTypes')
406
-
407
- expect(result).toStrictEqual(
408
- 'export type ArrayTypes = {\n' +
409
- ' strings?: string[];\n' +
410
- ' numbers?: number[];\n' +
411
- ' booleans?: boolean[];\n' +
412
- '};',
413
- )
414
- })
415
-
416
- it('generates type for recursive-like structure', () => {
417
- const schema = {
418
- type: 'object',
419
- properties: {
420
- id: { type: 'string' },
421
- children: {
422
- type: 'array',
423
- items: {
424
- type: 'object',
425
- properties: {
426
- id: { type: 'string' },
427
- children: {
428
- type: 'array',
429
- items: {
430
- type: 'object',
431
- properties: {
432
- id: { type: 'string' },
433
- },
434
- required: ['id'],
435
- },
436
- },
437
- },
438
- required: ['id'],
439
- },
440
- },
441
- },
442
- required: ['id'],
443
- }
444
-
445
- const result = generateTypeDefinition(schema, 'TreeNode')
446
-
447
- expect(result).toStrictEqual(
448
- 'export type TreeNode = {\n' +
449
- ' id: string;\n' +
450
- ' children?: { id: string; children?: { id: string }[] }[];\n' +
451
- '};',
452
- )
453
- })
454
-
455
- it('generates type for schema with mixed nested structures', () => {
456
- const schema = {
457
- type: 'object',
458
- properties: {
459
- config: {
460
- type: 'object',
461
- properties: {
462
- settings: {
463
- type: 'object',
464
- properties: {
465
- enabled: { type: 'boolean' },
466
- options: {
467
- type: 'array',
468
- items: { type: 'string' },
469
- },
470
- },
471
- required: ['enabled'],
472
- },
473
- metadata: {
474
- type: 'array',
475
- items: {
476
- type: 'object',
477
- properties: {
478
- key: { type: 'string' },
479
- value: { type: 'string' },
480
- },
481
- required: ['key', 'value'],
482
- },
483
- },
484
- },
485
- },
486
- },
487
- }
488
-
489
- const result = generateTypeDefinition(schema, 'Configuration')
490
-
491
- expect(result).toStrictEqual(
492
- 'export type Configuration = {\n' +
493
- ' config?: { settings?: { enabled: boolean; options?: string[] }; metadata?: { key: string; value: string }[] };\n' +
494
- '};',
495
- )
496
- })
497
-
498
- it('generates type for schema with property without type', () => {
499
- const schema = {
500
- type: 'object',
501
- properties: {
502
- name: { type: 'string' },
503
- unknownField: {},
504
- age: { type: 'number' },
505
- },
506
- required: ['name'],
507
- }
508
-
509
- const result = generateTypeDefinition(schema, 'UnknownField')
510
-
511
- expect(result).toStrictEqual(
512
- 'export type UnknownField = {\n' +
513
- ' name: string;\n' +
514
- ' unknownField?: unknown;\n' +
515
- ' age?: number;\n' +
516
- '};',
517
- )
518
- })
519
-
520
- it('generates type for deeply nested array structures with mixed types', () => {
521
- const schema: JSONSchema.Object = {
522
- type: 'object',
523
- properties: {
524
- data: {
525
- type: 'array',
526
- items: {
527
- type: 'object',
528
- properties: {
529
- id: { type: 'string' },
530
- tags: {
531
- type: 'array',
532
- items: { type: 'string' },
533
- },
534
- metadata: {
535
- type: 'array',
536
- items: {
537
- type: 'object',
538
- properties: {
539
- key: { type: 'string' },
540
- values: {
541
- type: 'array',
542
- items: { type: 'number' },
543
- },
544
- },
545
- required: ['key'],
546
- },
547
- },
548
- },
549
- required: ['id'],
550
- },
551
- },
552
- },
553
- required: ['data'],
554
- }
555
-
556
- const result = generateTypeDefinition(schema, 'ComplexNestedArrays')
557
-
558
- expect(result).toStrictEqual(
559
- 'export type ComplexNestedArrays = {\n' +
560
- ' data: { id: string; tags?: string[]; metadata?: { key: string; values?: number[] }[] }[];\n' +
561
- '};',
562
- )
563
- })
564
-
565
- it('generates type for info-like object schema with URL $comment as JSDoc description', () => {
566
- const info: JSONSchema.Object = {
567
- $comment: 'https://spec.openapis.org/oas/v3.1#info-object',
568
- type: 'object',
569
- properties: {
570
- title: { type: 'string' },
571
- summary: { type: 'string' },
572
- contact: { $ref: '#/$defs/contact' },
573
- version: { type: 'string' },
574
- },
575
- required: ['title', 'version'],
576
- }
577
-
578
- const result = generateTypeDefinition(info, 'Info')
579
-
580
- expect(result).toStrictEqual(
581
- '/**\n' +
582
- '* Info\n' +
583
- '*\n' +
584
- '* https://spec.openapis.org/oas/v3.1#info-object\n' +
585
- '*/\n' +
586
- 'export type Info = {\n' +
587
- ' title: string;\n' +
588
- ' summary?: string;\n' +
589
- ' contact?: Contact;\n' +
590
- ' version: string;\n' +
591
- '};',
592
- )
593
- })
594
-
595
- it('generates type for object with additionalProperties refs as Record type', () => {
596
- const components: JSONSchema.Object = {
597
- type: 'object',
598
- properties: {
599
- responses: {
600
- type: 'object',
601
- additionalProperties: { $ref: '#/$defs/response' },
602
- },
603
- parameters: {
604
- type: 'object',
605
- additionalProperties: { $ref: '#/$defs/parameter' },
606
- },
607
- pathItems: {
608
- type: 'object',
609
- additionalProperties: { $ref: '#/$defs/path-item' },
610
- },
611
- },
612
- }
613
-
614
- const result = generateTypeDefinition(components, 'Components')
615
-
616
- expect(result).toStrictEqual(
617
- 'export type Components = {\n' +
618
- ' responses?: Record<string, Response>;\n' +
619
- ' parameters?: Record<string, Parameter>;\n' +
620
- ' pathItems?: Record<string, PathItem>;\n' +
621
- '};',
622
- )
623
- })
624
-
625
- it('generates type for object with paths property as Record<string, PathItem>', () => {
626
- const document: JSONSchema.Object = {
627
- $comment: 'https://spec.openapis.org/oas/v3.1#openapi-object',
628
- type: 'object',
629
- properties: {
630
- openapi: { type: 'string' },
631
- info: { $ref: '#/$defs/info' },
632
- paths: {
633
- type: 'object',
634
- additionalProperties: { $ref: '#/$defs/path-item' },
635
- },
636
- },
637
- required: ['openapi', 'info'],
638
- }
639
-
640
- const result = generateTypeDefinition(document, 'Document')
641
-
642
- expect(result).toStrictEqual(
643
- '/**\n' +
644
- '* Document\n' +
645
- '*\n' +
646
- '* https://spec.openapis.org/oas/v3.1#openapi-object\n' +
647
- '*/\n' +
648
- 'export type Document = {\n' +
649
- ' openapi: string;\n' +
650
- ' info: Info;\n' +
651
- ' paths?: Record<string, PathItem>;\n' +
652
- '};',
653
- )
654
- })
655
-
656
- it('generates type for Document with paths and webhooks as Record types', () => {
657
- const document: JSONSchema.Object = {
658
- $comment: 'https://spec.openapis.org/oas/v3.1#openapi-object',
659
- type: 'object',
660
- properties: {
661
- openapi: {
662
- type: 'string',
663
- },
664
- info: {
665
- $ref: '#/$defs/info',
666
- },
667
- jsonSchemaDialect: {
668
- type: 'string',
669
- },
670
- servers: {
671
- type: 'array',
672
- items: {
673
- $ref: '#/$defs/server',
674
- },
675
- },
676
- paths: {
677
- type: 'object',
678
- additionalProperties: {
679
- $ref: '#/$defs/path-item',
680
- },
681
- },
682
- webhooks: {
683
- type: 'object',
684
- additionalProperties: {
685
- $ref: '#/$defs/path-item',
686
- },
687
- },
688
- components: {
689
- $ref: '#/$defs/components',
690
- },
691
- },
692
- required: ['openapi', 'info'],
693
- }
694
-
695
- const result = generateTypeDefinition(document, 'Document')
696
-
697
- expect(result).toStrictEqual(
698
- '/**\n' +
699
- '* Document\n' +
700
- '*\n' +
701
- '* https://spec.openapis.org/oas/v3.1#openapi-object\n' +
702
- '*/\n' +
703
- 'export type Document = {\n' +
704
- ' openapi: string;\n' +
705
- ' info: Info;\n' +
706
- ' jsonSchemaDialect?: string;\n' +
707
- ' servers?: Server[];\n' +
708
- ' paths?: Record<string, PathItem>;\n' +
709
- ' webhooks?: Record<string, PathItem>;\n' +
710
- ' components?: Components;\n' +
711
- '};',
712
- )
713
- })
714
-
715
- it('generates type for object with patternProperties as Record type', () => {
716
- const paths: JSONSchema.Object = {
717
- $comment: 'https://spec.openapis.org/oas/v3.1#paths-object',
718
- type: 'object',
719
- patternProperties: {
720
- '^/': {
721
- $ref: '#/$defs/path-item',
722
- },
723
- },
724
- }
725
-
726
- const result = generateTypeDefinition(paths, 'Paths')
727
-
728
- expect(result).toStrictEqual(
729
- '/**\n' +
730
- '* Paths\n' +
731
- '*\n' +
732
- '* https://spec.openapis.org/oas/v3.1#paths-object\n' +
733
- '*/\n' +
734
- 'export type Paths = Record<string, PathItem>;',
735
- )
736
- })
737
-
738
- it('quotes hyphenated property names in type definitions', () => {
739
- const schema = {
740
- type: 'object' as const,
741
- properties: {
742
- 'x-linkedin': { type: 'string' as const },
743
- name: { type: 'string' as const },
744
- },
745
- }
746
- const result = generateTypeDefinition(schema, 'InfoExtensions')
747
- expect(result).toContain("'x-linkedin'?: string;")
748
- expect(result).toContain('name?: string;')
749
- })
750
-
751
- it('generates type from conditional if/then object fragments', () => {
752
- const schema: JSONSchema = {
753
- if: {
754
- properties: {
755
- type: {
756
- const: 'http',
757
- },
758
- },
759
- },
760
- then: {
761
- properties: {
762
- scheme: {
763
- type: 'string',
764
- },
765
- },
766
- required: ['scheme'],
767
- },
768
- }
769
-
770
- const result = generateTypeDefinition(schema, 'TypeHttp')
771
-
772
- expect(result).toStrictEqual('export type TypeHttp = {\n' + ' type: "http";\n' + ' scheme: string;\n' + '};')
773
- })
774
-
775
- it('generates required property from then properties without explicit required', () => {
776
- const schema: JSONSchema = {
777
- if: {
778
- properties: {
779
- type: {
780
- const: 'http',
781
- },
782
- },
783
- },
784
- then: {
785
- properties: {
786
- bearerFormat: {
787
- type: 'string',
788
- },
789
- },
790
- },
791
- }
792
-
793
- const result = generateTypeDefinition(schema, 'TypeHttp')
794
-
795
- expect(result).toStrictEqual(
796
- 'export type TypeHttp = {\n' + ' type: "http";\n' + ' bearerFormat: string;\n' + '};',
797
- )
798
- })
799
-
800
- it('generates required discriminator for conditional type-http schema with $comment JSDoc', () => {
801
- const schema: JSONSchema = {
802
- $comment: 'https://spec.openapis.org/oas/v3.1#security-scheme-object',
803
- if: {
804
- properties: {
805
- type: {
806
- const: 'http',
807
- },
808
- scheme: {
809
- type: 'string',
810
- pattern: '^[Bb][Ee][Aa][Rr][Ee][Rr]$',
811
- },
812
- },
813
- required: ['type', 'scheme'],
814
- },
815
- then: {
816
- properties: {
817
- bearerFormat: {
818
- type: 'string',
819
- },
820
- },
821
- },
822
- }
823
-
824
- const result = generateTypeDefinition(schema, 'TypeHttp')
825
-
826
- expect(result).toContain('* TypeHttp')
827
- expect(result).toContain('* https://spec.openapis.org/oas/v3.1#security-scheme-object')
828
- expect(result).toContain('type: "http";')
829
- expect(result).toContain('scheme: string;')
830
- expect(result).toContain('bearerFormat: string;')
831
- })
832
-
833
- it('generates intersection type for schema with allOf $ref entries', () => {
834
- const securityScheme: JSONSchema.Object = {
835
- type: 'object',
836
- properties: {
837
- type: {
838
- enum: ['apiKey', 'http', 'oauth2'],
839
- },
840
- description: {
841
- type: 'string',
842
- },
843
- },
844
- required: ['type'],
845
- allOf: [{ $ref: '#/$defs/type-apikey' }, { $ref: '#/$defs/type-http' }, { $ref: '#/$defs/type-oauth2' }],
846
- }
847
-
848
- const result = generateTypeDefinition(securityScheme, 'SecurityScheme')
849
-
850
- expect(result).toStrictEqual(
851
- 'export type SecurityScheme = {\n' +
852
- ' type: "apiKey" | "http" | "oauth2";\n' +
853
- ' description?: string;\n' +
854
- '} & TypeApikey & TypeHttp & TypeOauth2;',
855
- )
856
- })
857
-
858
- it('generates JSDoc from $comment URL for schema with allOf intersections', () => {
859
- const securityScheme: JSONSchema.Object = {
860
- $comment: 'https://spec.openapis.org/oas/v3.1#security-scheme-object',
861
- type: 'object',
862
- properties: {
863
- type: {
864
- enum: ['apiKey', 'http', 'oauth2'],
865
- },
866
- },
867
- required: ['type'],
868
- allOf: [{ $ref: '#/$defs/type-apikey' }, { $ref: '#/$defs/type-http' }],
869
- }
870
-
871
- const result = generateTypeDefinition(securityScheme, 'SecurityScheme')
872
-
873
- expect(result).toContain('* SecurityScheme')
874
- expect(result).toContain('* https://spec.openapis.org/oas/v3.1#security-scheme-object')
875
- expect(result).toContain('type: "apiKey" | "http" | "oauth2";')
876
- expect(result).toContain('} & TypeApikey & TypeHttp;')
877
- })
878
-
879
- it('preserves property descriptions as JSDoc comments when allOf contains an inline object schema', () => {
880
- const schema: JSONSchema = {
881
- allOf: [
882
- { $ref: '#/$defs/baseTargetConfig' },
883
- {
884
- type: 'object',
885
- additionalProperties: false,
886
- properties: {
887
- packageName: {
888
- type: 'string',
889
- description: 'Import/package name for TypeScript and Node packages.',
890
- },
891
- packageManager: {
892
- type: 'string',
893
- description: 'TypeScript package manager preference for generated package metadata.',
894
- },
895
- publish: {
896
- $ref: '#/$defs/npmPublishConfig',
897
- description: 'npm publishing configuration.',
898
- },
899
- },
900
- required: ['publish'],
901
- },
902
- ],
903
- }
904
-
905
- const result = generateTypeDefinition(schema, 'TypeScriptTargetConfig')
906
-
907
- expect(result).toContain('/** Import/package name for TypeScript and Node packages. */')
908
- expect(result).toContain('/** TypeScript package manager preference for generated package metadata. */')
909
- expect(result).toContain('/** npm publishing configuration. */')
910
- })
911
-
912
- it('generates record type for patternProperties-only schema without explicit type', () => {
913
- const schema: JSONSchema = {
914
- patternProperties: {
915
- '^x-': true,
916
- },
917
- }
918
-
919
- const result = generateTypeDefinition(schema, 'SpecificationExtensions')
920
-
921
- expect(result).toStrictEqual('export type SpecificationExtensions = Record<`x-${string}`, unknown>;')
922
- })
923
-
924
- it('generates Record<string, never> for patternProperties-only schema with false boolean value', () => {
925
- // The false boolean schema means no values are allowed for matching keys,
926
- // which maps to the never type in TypeScript.
927
- const schema: JSONSchema = {
928
- patternProperties: {
929
- '^x-': false,
930
- },
931
- }
932
-
933
- const result = generateTypeDefinition(schema, 'Restricted')
934
-
935
- expect(result).toStrictEqual('export type Restricted = Record<`x-${string}`, never>;')
936
- })
937
-
938
- it('generates Schema type for property with $dynamicRef pointing to #meta', () => {
939
- // $dynamicRef: '#meta' is a JSON Schema 2020-12 pattern used for recursive
940
- // schema definitions that refer to the root Schema type itself.
941
- const schema: JSONSchema.Object = {
942
- type: 'object',
943
- properties: {
944
- schema: { $dynamicRef: '#meta' },
945
- },
946
- }
947
-
948
- const result = generateTypeDefinition(schema, 'SchemaContainer')
949
-
950
- expect(result).toContain('schema?: Schema')
951
- })
952
-
953
- it('generates type name from non-meta $dynamicRef', () => {
954
- // A $dynamicRef other than '#meta' is converted via refToName like a $ref.
955
- const schema: JSONSchema.Object = {
956
- type: 'object',
957
- properties: {
958
- content: { $dynamicRef: '#/$defs/schema' },
959
- },
960
- }
961
-
962
- const result = generateTypeDefinition(schema, 'ContentContainer')
963
-
964
- expect(result).toContain('content?: Schema')
965
- })
966
-
967
- it('generates union type for schema with array of types', () => {
968
- // JSON Schema allows `type` to be an array of strings to express a union type.
969
- const schema: JSONSchema.Object = {
970
- type: 'object',
971
- properties: {
972
- value: { type: ['string', 'null'] },
973
- },
974
- }
975
-
976
- const result = generateTypeDefinition(schema, 'NullableStringContainer')
977
-
978
- expect(result).toContain('value?: string | null')
979
- })
980
-
981
- it('generates correct union for all supported types in type array', () => {
982
- const schema: JSONSchema.Object = {
983
- type: 'object',
984
- properties: {
985
- anything: { type: ['string', 'number', 'boolean', 'null', 'array', 'object'] },
986
- },
987
- }
988
-
989
- const result = generateTypeDefinition(schema, 'AnyTypeContainer')
990
-
991
- expect(result).toContain('string | number | boolean | null | unknown[] | Record<string, unknown>')
992
- })
993
-
994
- it('infers Record<string, unknown> type for no-type property with boolean true additionalProperties', () => {
995
- // A schema with additionalProperties: true and no explicit type is treated
996
- // as an open record allowing any values.
997
- const schema: JSONSchema.Object = {
998
- type: 'object',
999
- properties: {
1000
- extensions: { additionalProperties: true },
1001
- },
1002
- }
1003
-
1004
- const result = generateTypeDefinition(schema, 'Container')
1005
-
1006
- expect(result).toContain('extensions?: Record<string, unknown>')
1007
- })
1008
-
1009
- it('infers Record<string, never> type for no-type property with boolean false additionalProperties', () => {
1010
- // A schema with additionalProperties: false and no explicit type means
1011
- // no values are allowed, which maps to the never type.
1012
- const schema: JSONSchema.Object = {
1013
- type: 'object',
1014
- properties: {
1015
- locked: { additionalProperties: false },
1016
- },
1017
- }
1018
-
1019
- const result = generateTypeDefinition(schema, 'Container')
1020
-
1021
- expect(result).toContain('locked?: Record<string, never>')
1022
- })
1023
-
1024
- it('infers Record<`x-${string}`, unknown> for no-type property with ^x- patternProperties', () => {
1025
- // The ^x- pattern is a common JSON Schema convention for vendor extensions that
1026
- // maps naturally to the TypeScript template literal `x-${string}`.
1027
- const schema: JSONSchema.Object = {
1028
- type: 'object',
1029
- properties: {
1030
- extensions: { patternProperties: { '^x-': true } },
1031
- },
1032
- }
1033
-
1034
- const result = generateTypeDefinition(schema, 'Container')
1035
-
1036
- expect(result).toContain('extensions?: Record<`x-${string}`, unknown>')
1037
- })
1038
-
1039
- it('infers string type for no-type property whose default is a string', () => {
1040
- // When a property has no explicit type but has a string default, we infer
1041
- // the type as string so the generated TypeScript stays as specific as possible.
1042
- const schema: JSONSchema.Object = {
1043
- type: 'object',
1044
- properties: {
1045
- format: { default: 'json' },
1046
- },
1047
- }
1048
-
1049
- const result = generateTypeDefinition(schema, 'Config')
1050
-
1051
- expect(result).toContain('format?: string')
1052
- })
1053
-
1054
- it('infers number type for no-type property whose default is a number', () => {
1055
- const schema: JSONSchema.Object = {
1056
- type: 'object',
1057
- properties: {
1058
- timeout: { default: 30 },
1059
- },
1060
- }
1061
-
1062
- const result = generateTypeDefinition(schema, 'Config')
1063
-
1064
- expect(result).toContain('timeout?: number')
1065
- })
1066
-
1067
- it('infers boolean type for no-type property whose default is a boolean', () => {
1068
- const schema: JSONSchema.Object = {
1069
- type: 'object',
1070
- properties: {
1071
- enabled: { default: true },
1072
- },
1073
- }
1074
-
1075
- const result = generateTypeDefinition(schema, 'Config')
1076
-
1077
- expect(result).toContain('enabled?: boolean')
1078
- })
1079
-
1080
- it('generates type with JSDoc for additionalProperties-only schema when documentation is found', () => {
1081
- // This tests the documentation block (lines 431–438) inside the additionalProperties-only
1082
- // path — a code path that is only reached when the schema has no fixed properties but
1083
- // does have additionalProperties, and a matching documentation section exists.
1084
- const schema: JSONSchema.Object = {
1085
- $comment: 'https://spec.openapis.org/oas/v3.1#callback-object',
1086
- type: 'object',
1087
- additionalProperties: {
1088
- $ref: '#/$defs/path-item',
1089
- },
1090
- }
1091
-
1092
- const result = generateTypeDefinition(schema, 'Callback')
1093
-
1094
- expect(result).toContain('/**')
1095
- expect(result).toContain('* Callback')
1096
- expect(result).toContain('* https://spec.openapis.org/oas/v3.1#callback-object')
1097
- expect(result).toContain('[key: string]: PathItem')
1098
- })
1099
-
1100
- it('generates type for product schema with required, optional, and array fields', () => {
1101
- const schema: JSONSchema = {
1102
- description: 'A product available for purchase in the catalog.',
1103
- type: 'object',
1104
- properties: {
1105
- id: { description: 'Unique product identifier (UUID).', type: 'string' },
1106
- name: { description: 'Display name shown to customers.', type: 'string' },
1107
- price: { description: 'Unit price in USD cents (must be non-negative).', type: 'number', minimum: 0 },
1108
- inStock: { description: 'Whether the product is currently available for purchase.', type: 'boolean' },
1109
- tags: {
1110
- description: 'Searchable labels associated with the product.',
1111
- type: 'array',
1112
- items: { type: 'string' },
1113
- },
1114
- },
1115
- required: ['id', 'name', 'price'],
1116
- }
1117
-
1118
- const result = generateTypeDefinition(schema, 'Product')
1119
-
1120
- expect(result).toBe(
1121
- '/**\n' +
1122
- '* Product\n' +
1123
- '*\n' +
1124
- '* A product available for purchase in the catalog.\n' +
1125
- '*/\n' +
1126
- 'export type Product = {\n' +
1127
- ' /** Unique product identifier (UUID). */\n' +
1128
- ' id: string;\n' +
1129
- ' /** Display name shown to customers. */\n' +
1130
- ' name: string;\n' +
1131
- ' /** Unit price in USD cents (must be non-negative). */\n' +
1132
- ' price: number;\n' +
1133
- ' /** Whether the product is currently available for purchase. */\n' +
1134
- ' inStock?: boolean;\n' +
1135
- ' /** Searchable labels associated with the product. */\n' +
1136
- ' tags?: string[];\n' +
1137
- '};',
1138
- )
1139
- })
1140
-
1141
- it('generates type for string enum schema', () => {
1142
- const schema: JSONSchema = {
1143
- description: 'One of the supported theme colors.',
1144
- type: 'string',
1145
- enum: ['red', 'green', 'blue', 'yellow', 'purple'],
1146
- }
1147
-
1148
- const result = generateTypeDefinition(schema, 'ThemeColor')
1149
-
1150
- expect(result).toBe(
1151
- '/**\n' +
1152
- '* ThemeColor\n' +
1153
- '*\n' +
1154
- '* One of the supported theme colors.\n' +
1155
- '*/\n' +
1156
- 'export type ThemeColor = "red" | "green" | "blue" | "yellow" | "purple";',
1157
- )
1158
- })
1159
-
1160
- it('generates type for geo coordinate with min/max constraints on required number fields', () => {
1161
- const schema: JSONSchema = {
1162
- description: 'A geographic coordinate pair.',
1163
- type: 'object',
1164
- properties: {
1165
- latitude: { description: 'Degrees latitude, from -90 to 90.', type: 'number', minimum: -90, maximum: 90 },
1166
- longitude: { description: 'Degrees longitude, from -180 to 180.', type: 'number', minimum: -180, maximum: 180 },
1167
- altitude: { description: 'Elevation in metres above sea level.', type: 'number' },
1168
- label: { description: 'Human-readable name for this location.', type: 'string' },
1169
- },
1170
- required: ['latitude', 'longitude'],
1171
- }
1172
-
1173
- const result = generateTypeDefinition(schema, 'GeoCoordinate')
1174
-
1175
- expect(result).toBe(
1176
- '/**\n' +
1177
- '* GeoCoordinate\n' +
1178
- '*\n' +
1179
- '* A geographic coordinate pair.\n' +
1180
- '*/\n' +
1181
- 'export type GeoCoordinate = {\n' +
1182
- ' /** Degrees latitude, from -90 to 90. */\n' +
1183
- ' latitude: number;\n' +
1184
- ' /** Degrees longitude, from -180 to 180. */\n' +
1185
- ' longitude: number;\n' +
1186
- ' /** Elevation in metres above sea level. */\n' +
1187
- ' altitude?: number;\n' +
1188
- ' /** Human-readable name for this location. */\n' +
1189
- ' label?: string;\n' +
1190
- '};',
1191
- )
1192
- })
1193
-
1194
- it('wraps union item types in parentheses for root-level array schema', () => {
1195
- const schema: JSONSchema = {
1196
- type: 'array',
1197
- items: { anyOf: [{ $ref: '#/$defs/parameter' }, { $ref: '#/$defs/reference' }] },
1198
- }
1199
-
1200
- const result = generateTypeDefinition(schema, 'Parameters')
1201
-
1202
- expect(result).toBe('export type Parameters = (Parameter | Reference)[];')
1203
- })
1204
-
1205
- it('emits JSDoc for non-object schemas with a $comment URL', () => {
1206
- const schema: JSONSchema = {
1207
- $comment: 'https://spec.openapis.org/oas/v3.1#contact-object',
1208
- type: 'array',
1209
- items: { $ref: '#/$defs/server' },
1210
- }
1211
-
1212
- const result = generateTypeDefinition(schema, 'Contacts')
1213
-
1214
- expect(result).toContain('/**')
1215
- expect(result).toContain('* https://spec.openapis.org/oas/v3.1#contact-object')
1216
- expect(result).toContain('export type Contacts = Server[];')
1217
- })
1218
-
1219
- it('emits JSDoc for non-object schemas with a plain-text $comment', () => {
1220
- const schema: JSONSchema = {
1221
- $comment: 'A list of parameters applicable to the operation.',
1222
- type: 'array',
1223
- items: { $ref: '#/$defs/parameter' },
1224
- }
1225
-
1226
- const result = generateTypeDefinition(schema, 'Parameters')
1227
-
1228
- expect(result).toContain('/**')
1229
- expect(result).toContain('A list of parameters applicable to the operation.')
1230
- expect(result).toContain('export type Parameters = Parameter[];')
1231
- })
1232
-
1233
- it('generates unknown for external $ref', () => {
1234
- // External refs (e.g. from draft-04 schemas) cannot be resolved locally — treated as unknown.
1235
- const schema: JSONSchema = {
1236
- $ref: 'http://json-schema.org/draft-04/schema#/properties/maximum',
1237
- }
1238
-
1239
- const result = generateTypeDefinition(schema, 'Maximum')
1240
-
1241
- expect(result).toBe('export type Maximum = unknown;')
1242
- })
1243
-
1244
- it('does not emit a trailing blank line in JSDoc when there is no @see link', () => {
1245
- const schema: JSONSchema = {
1246
- $comment: 'A plain-text description with no URL.',
1247
- type: 'object',
1248
- properties: {
1249
- value: { type: 'string' },
1250
- },
1251
- }
1252
-
1253
- const result = generateTypeDefinition(schema, 'PlainComment')
1254
-
1255
- expect(result).toMatch(/\* A plain-text description with no URL\.\n\*\//)
1256
- expect(result).not.toContain('* \n*/')
1257
- })
1258
-
1259
- it('emits the class name for an x-mjst instanceOf property', () => {
1260
- const schema: JSONSchema = {
1261
- type: 'object',
1262
- properties: { createdAt: { 'x-mjst': { instanceOf: 'Date' } } },
1263
- required: ['createdAt'],
1264
- }
1265
-
1266
- expect(generateTypeDefinition(schema, 'Event')).toContain('createdAt: Date;')
1267
- })
1268
-
1269
- it('emits the class name for a top-level x-mjst instanceOf schema', () => {
1270
- const schema: JSONSchema = { 'x-mjst': { instanceOf: 'Date' } }
1271
-
1272
- expect(generateTypeDefinition(schema, 'When')).toBe('export type When = Date;')
1273
- })
1274
-
1275
- it('ignores an x-mjst instanceOf that is not a safe identifier', () => {
1276
- const schema: JSONSchema = { 'x-mjst': { instanceOf: 'Date; doEvil()' } }
1277
-
1278
- expect(generateTypeDefinition(schema, 'When')).not.toContain('doEvil')
1279
- })
1280
-
1281
- it('emits a primitive type for an x-mjst bigint property', () => {
1282
- const schema: JSONSchema = {
1283
- type: 'object',
1284
- properties: { balance: { 'x-mjst': { primitive: 'bigint' } } },
1285
- required: ['balance'],
1286
- }
1287
-
1288
- expect(generateTypeDefinition(schema, 'Account')).toContain('balance: bigint;')
1289
- })
1290
-
1291
- it('emits a primitive type for a top-level x-mjst bigint schema', () => {
1292
- expect(generateTypeDefinition({ 'x-mjst': { primitive: 'bigint' } }, 'Big')).toBe('export type Big = bigint;')
1293
- })
1294
-
1295
- it('wraps a branded property in a nominal intersection', () => {
1296
- const schema: JSONSchema = {
1297
- type: 'object',
1298
- properties: { id: { type: 'string', 'x-mjst': { brand: 'UserId' } } },
1299
- required: ['id'],
1300
- }
1301
-
1302
- expect(generateTypeDefinition(schema, 'User')).toContain("id: (string & { readonly __brand: 'UserId' });")
1303
- })
1304
-
1305
- it('brands a top-level schema and combines with instanceOf', () => {
1306
- expect(generateTypeDefinition({ type: 'string', 'x-mjst': { brand: 'Email' } }, 'Email')).toBe(
1307
- "export type Email = (string & { readonly __brand: 'Email' });",
1308
- )
1309
- expect(generateTypeDefinition({ 'x-mjst': { instanceOf: 'Date', brand: 'Timestamp' } }, 'Ts')).toBe(
1310
- "export type Ts = (Date & { readonly __brand: 'Timestamp' });",
1311
- )
1312
- })
1313
-
1314
- it('ignores an x-mjst brand that is not safe to embed', () => {
1315
- const schema: JSONSchema = { type: 'string', 'x-mjst': { brand: "x'; doEvil()" } }
1316
-
1317
- const result = generateTypeDefinition(schema, 'Bad')
1318
- expect(result).not.toContain('doEvil')
1319
- expect(result).toBe('export type Bad = string;')
1320
- })
1321
-
1322
- describe('readonly option', () => {
1323
- it('marks every property as readonly, deeply', () => {
1324
- const schema: JSONSchema = {
1325
- type: 'object',
1326
- properties: {
1327
- id: { type: 'string' },
1328
- tags: { type: 'array', items: { type: 'string' } },
1329
- nested: {
1330
- type: 'object',
1331
- properties: { value: { type: 'number' } },
1332
- },
1333
- },
1334
- required: ['id'],
1335
- }
1336
-
1337
- const result = generateTypeDefinition(schema, 'Doc', { readonly: true })
1338
-
1339
- expect(result).toContain('readonly id: string;')
1340
- expect(result).toContain('readonly tags?: readonly string[];')
1341
- expect(result).toContain('readonly nested?: { readonly value?: number }')
1342
- })
1343
-
1344
- it('wraps record types in Readonly', () => {
1345
- const schema: JSONSchema = {
1346
- type: 'object',
1347
- additionalProperties: { type: 'number' },
1348
- }
1349
-
1350
- expect(generateTypeDefinition(schema, 'Map', { readonly: true })).toContain('readonly [key: string]: number;')
1351
- })
1352
-
1353
- it('leaves output unchanged when readonly is not set', () => {
1354
- const schema: JSONSchema = {
1355
- type: 'object',
1356
- properties: { id: { type: 'string' } },
1357
- required: ['id'],
1358
- }
1359
-
1360
- const result = generateTypeDefinition(schema, 'Doc')
1361
- expect(result).not.toContain('readonly')
1362
- expect(result).toContain('id: string;')
1363
- })
1364
- })
1365
-
1366
- describe('typeSuffix', () => {
1367
- const schema: JSONSchema = {
1368
- type: 'object',
1369
- properties: {
1370
- contact: { $ref: '#/$defs/contact' },
1371
- },
1372
- required: ['contact'],
1373
- }
1374
-
1375
- it('appends the suffix to ref-derived type names', () => {
1376
- const result = generateTypeDefinition(schema, 'Document', { typeSuffix: 'Object' })
1377
- expect(result).toContain('contact: ContactObject;')
1378
- })
1379
-
1380
- it('emits no suffix by default', () => {
1381
- const result = generateTypeDefinition(schema, 'Document')
1382
- expect(result).toContain('contact: Contact;')
1383
- expect(result).not.toContain('ContactObject')
1384
- })
1385
- })
1386
- })