@amritk/helpers 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build-dynamic-ref-map.js +5 -3
- package/dist/generate-type-definition.js +5 -3
- package/dist/has-ref.d.ts +4 -0
- package/dist/has-ref.js +7 -0
- package/dist/schema-guards.d.ts +1 -5
- package/dist/schema-guards.js +5 -3
- package/package.json +10 -3
- package/src/build-dynamic-ref-map.test.ts +65 -0
- package/src/build-dynamic-ref-map.ts +42 -0
- package/src/extract-refs.test.ts +183 -0
- package/src/extract-refs.ts +73 -0
- package/src/generate-type-definition.test.ts +1210 -0
- package/src/generate-type-definition.ts +487 -0
- package/src/has-ref.ts +10 -0
- package/src/is-object.test.ts +65 -0
- package/src/is-object.ts +15 -0
- package/src/parse-documentation.test.ts +313 -0
- package/src/parse-documentation.ts +173 -0
- package/src/ref-to-filename.test.ts +64 -0
- package/src/ref-to-filename.ts +116 -0
- package/src/ref-to-name.test.ts +48 -0
- package/src/ref-to-name.ts +38 -0
- package/src/resolve-dynamic-refs.test.ts +94 -0
- package/src/resolve-dynamic-refs.ts +48 -0
- package/src/resolve-ref.test.ts +186 -0
- package/src/resolve-ref.ts +82 -0
- package/src/safe-accessor.test.ts +41 -0
- package/src/safe-accessor.ts +48 -0
- package/src/schema-guards.test.ts +406 -0
- package/src/schema-guards.ts +160 -0
- package/src/upgrade-draft07-schema.ts +179 -0
- package/src/validate-array.test.ts +52 -0
- package/src/validate-array.ts +15 -0
- package/src/validate-record.test.ts +56 -0
- package/src/validate-record.ts +19 -0
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
|
|
2
|
+
|
|
3
|
+
import { refToName } from './ref-to-name'
|
|
4
|
+
import { safeKey } from './safe-accessor'
|
|
5
|
+
import { isObjectSchema, isSchemaObject } from './schema-guards'
|
|
6
|
+
|
|
7
|
+
type ConditionalObjectResult = {
|
|
8
|
+
schema: JSONSchema.Object
|
|
9
|
+
thenRef: string | null
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const getConditionalObjectSchema = (schema: JSONSchema): ConditionalObjectResult | null => {
|
|
13
|
+
if (!isSchemaObject(schema)) {
|
|
14
|
+
return null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (!('if' in schema) || !('then' in schema)) {
|
|
18
|
+
return null
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const ifSchema = schema.if
|
|
22
|
+
const thenSchema = schema.then
|
|
23
|
+
|
|
24
|
+
if (!isSchemaObject(ifSchema) || !isSchemaObject(thenSchema)) {
|
|
25
|
+
return null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const ifProperties = ifSchema.properties
|
|
29
|
+
const thenProperties = thenSchema.properties
|
|
30
|
+
const hasIfProperties = ifProperties && typeof ifProperties === 'object'
|
|
31
|
+
const hasThenProperties = thenProperties && typeof thenProperties === 'object'
|
|
32
|
+
|
|
33
|
+
if (!hasIfProperties && !hasThenProperties) {
|
|
34
|
+
return null
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const properties = {
|
|
38
|
+
...(hasIfProperties ? ifProperties : {}),
|
|
39
|
+
...(hasThenProperties ? thenProperties : {}),
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const required = new Set<string>()
|
|
43
|
+
|
|
44
|
+
if (Array.isArray(ifSchema.required)) {
|
|
45
|
+
for (const key of ifSchema.required) {
|
|
46
|
+
required.add(key)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (hasIfProperties) {
|
|
51
|
+
for (const key in ifProperties) {
|
|
52
|
+
required.add(key)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (Array.isArray(thenSchema.required)) {
|
|
57
|
+
for (const key of thenSchema.required) {
|
|
58
|
+
required.add(key)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (hasThenProperties) {
|
|
63
|
+
for (const key in thenProperties) {
|
|
64
|
+
required.add(key)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const thenRef = typeof thenSchema.$ref === 'string' ? thenSchema.$ref : null
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
schema: {
|
|
72
|
+
type: 'object',
|
|
73
|
+
properties,
|
|
74
|
+
...(required.size > 0 ? { required: Array.from(required) } : {}),
|
|
75
|
+
},
|
|
76
|
+
thenRef,
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const isObjectLikeSchema = (schema: JSONSchema): schema is JSONSchema.Object => {
|
|
81
|
+
if (!isSchemaObject(schema)) {
|
|
82
|
+
return false
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (isObjectSchema(schema)) {
|
|
86
|
+
return true
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return 'patternProperties' in schema || 'additionalProperties' in schema || ('if' in schema && 'then' in schema)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const getBooleanSubSchemaType = (schema: boolean): string => {
|
|
93
|
+
return schema ? 'unknown' : 'never'
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const buildJsDocBlock = (title: string, description: string, commentUrl?: string): string => {
|
|
97
|
+
let block = `/**\n`
|
|
98
|
+
block += `* ${title}\n`
|
|
99
|
+
block += `*\n`
|
|
100
|
+
block += `* ${description}\n`
|
|
101
|
+
if (commentUrl?.startsWith('http')) {
|
|
102
|
+
block += `* \n`
|
|
103
|
+
block += `* @see {@link ${commentUrl}}\n`
|
|
104
|
+
}
|
|
105
|
+
block += `*/\n`
|
|
106
|
+
return block
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Converts a JSON Schema type to its TypeScript equivalent.
|
|
111
|
+
* Recursively handles nested objects and arrays.
|
|
112
|
+
*/
|
|
113
|
+
const getTypeScriptType = (schema: JSONSchema): string => {
|
|
114
|
+
// Boolean schema: `true` means any value is valid (unknown), `false` means no value is valid (never)
|
|
115
|
+
if (typeof schema === 'boolean') {
|
|
116
|
+
return getBooleanSubSchemaType(schema)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Check if schema is an object (not a boolean schema)
|
|
120
|
+
if (typeof schema !== 'object' || schema === null) {
|
|
121
|
+
return 'unknown'
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Handle $ref
|
|
125
|
+
if (schema.$ref) {
|
|
126
|
+
// External refs (e.g. http://json-schema.org/...) cannot be resolved locally — treat as unknown
|
|
127
|
+
if (!schema.$ref.startsWith('#')) {
|
|
128
|
+
return 'unknown'
|
|
129
|
+
}
|
|
130
|
+
return refToName(schema.$ref)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Handle $dynamicRef (used for recursive schemas)
|
|
134
|
+
if (schema.$dynamicRef) {
|
|
135
|
+
// For #meta, this refers to the Schema type itself (JSON Schema 2020-12 $dynamicAnchor pattern)
|
|
136
|
+
if (schema.$dynamicRef === '#meta') {
|
|
137
|
+
return 'Schema'
|
|
138
|
+
}
|
|
139
|
+
return refToName(schema.$dynamicRef)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Handle const - literal type
|
|
143
|
+
if (schema.const !== undefined) {
|
|
144
|
+
return JSON.stringify(schema.const)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Handle enum - union of literal types
|
|
148
|
+
if (schema.enum && schema.enum.length > 0) {
|
|
149
|
+
if (schema.enum.length === 1) {
|
|
150
|
+
return JSON.stringify(schema.enum[0])
|
|
151
|
+
}
|
|
152
|
+
let enumUnion = JSON.stringify(schema.enum[0])
|
|
153
|
+
for (let i = 1; i < schema.enum.length; i++) {
|
|
154
|
+
enumUnion += ' | ' + JSON.stringify(schema.enum[i])
|
|
155
|
+
}
|
|
156
|
+
return enumUnion
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Handle multi-value enum - union of literal types
|
|
160
|
+
if (schema.enum && schema.enum.length > 1) {
|
|
161
|
+
let multiEnumUnion = JSON.stringify(schema.enum[0])
|
|
162
|
+
for (let i = 1; i < schema.enum.length; i++) {
|
|
163
|
+
multiEnumUnion += ' | ' + JSON.stringify(schema.enum[i])
|
|
164
|
+
}
|
|
165
|
+
return multiEnumUnion
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Handle union types (oneOf, anyOf) - check this before returning unknown
|
|
169
|
+
if (schema.oneOf && Array.isArray(schema.oneOf) && schema.oneOf.length > 0) {
|
|
170
|
+
// schema.oneOf[0] is safe: we guard with .length > 0 above
|
|
171
|
+
let oneOfUnion = getTypeScriptType(schema.oneOf[0]!)
|
|
172
|
+
for (let i = 1; i < schema.oneOf.length; i++) {
|
|
173
|
+
oneOfUnion += ' | ' + getTypeScriptType(schema.oneOf[i]!)
|
|
174
|
+
}
|
|
175
|
+
return oneOfUnion
|
|
176
|
+
}
|
|
177
|
+
if (schema.anyOf && Array.isArray(schema.anyOf) && schema.anyOf.length > 0) {
|
|
178
|
+
// schema.anyOf[0] is safe: we guard with .length > 0 above
|
|
179
|
+
let anyOfUnion = getTypeScriptType(schema.anyOf[0]!)
|
|
180
|
+
for (let i = 1; i < schema.anyOf.length; i++) {
|
|
181
|
+
anyOfUnion += ' | ' + getTypeScriptType(schema.anyOf[i]!)
|
|
182
|
+
}
|
|
183
|
+
return anyOfUnion
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Handle allOf (intersection types)
|
|
187
|
+
if (schema.allOf && Array.isArray(schema.allOf) && schema.allOf.length > 0) {
|
|
188
|
+
// schema.allOf[0] is safe: we guard with .length > 0 above
|
|
189
|
+
let intersectionTypes = getTypeScriptType(schema.allOf[0]!)
|
|
190
|
+
for (let i = 1; i < schema.allOf.length; i++) {
|
|
191
|
+
intersectionTypes += ' & ' + getTypeScriptType(schema.allOf[i]!)
|
|
192
|
+
}
|
|
193
|
+
return intersectionTypes
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Handle object-like conditional schemas that use if/then without declaring type
|
|
197
|
+
const conditionalResult = getConditionalObjectSchema(schema)
|
|
198
|
+
if (conditionalResult) {
|
|
199
|
+
const baseType = getTypeScriptType(conditionalResult.schema)
|
|
200
|
+
if (conditionalResult.thenRef) {
|
|
201
|
+
return `(${baseType}) & ${refToName(conditionalResult.thenRef)}`
|
|
202
|
+
}
|
|
203
|
+
return baseType
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// No type so we return unknown
|
|
207
|
+
if (!schema.type) {
|
|
208
|
+
if (schema.additionalProperties !== undefined) {
|
|
209
|
+
if (typeof schema.additionalProperties === 'boolean') {
|
|
210
|
+
return `Record<string, ${getBooleanSubSchemaType(schema.additionalProperties)}>`
|
|
211
|
+
}
|
|
212
|
+
return `Record<string, ${getTypeScriptType(schema.additionalProperties)}>`
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (schema.patternProperties && typeof schema.patternProperties === 'object') {
|
|
216
|
+
const firstEntry = Object.entries(schema.patternProperties)[0]
|
|
217
|
+
if (firstEntry) {
|
|
218
|
+
const [pattern, value] = firstEntry
|
|
219
|
+
if (value !== undefined) {
|
|
220
|
+
const valueType = typeof value === 'boolean' ? getBooleanSubSchemaType(value) : getTypeScriptType(value)
|
|
221
|
+
// The ^x- pattern is a common JSON Schema convention for vendor extensions
|
|
222
|
+
// that maps naturally to the TypeScript template literal `x-${string}`
|
|
223
|
+
if (pattern === '^x-') {
|
|
224
|
+
return `Record<\`x-\${string}\`, ${valueType}>`
|
|
225
|
+
}
|
|
226
|
+
return `Record<string, ${valueType}>`
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (schema.default !== undefined) {
|
|
232
|
+
if (typeof schema.default === 'string') {
|
|
233
|
+
return 'string'
|
|
234
|
+
}
|
|
235
|
+
if (typeof schema.default === 'number') {
|
|
236
|
+
return 'number'
|
|
237
|
+
}
|
|
238
|
+
if (typeof schema.default === 'boolean') {
|
|
239
|
+
return 'boolean'
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return 'unknown'
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Handle type as an array (union of types)
|
|
247
|
+
if (Array.isArray(schema.type)) {
|
|
248
|
+
const mapType = (t: string): string => {
|
|
249
|
+
switch (t) {
|
|
250
|
+
case 'string':
|
|
251
|
+
return 'string'
|
|
252
|
+
case 'number':
|
|
253
|
+
case 'integer':
|
|
254
|
+
return 'number'
|
|
255
|
+
case 'boolean':
|
|
256
|
+
return 'boolean'
|
|
257
|
+
case 'null':
|
|
258
|
+
return 'null'
|
|
259
|
+
case 'array':
|
|
260
|
+
return 'unknown[]'
|
|
261
|
+
case 'object':
|
|
262
|
+
return 'Record<string, unknown>'
|
|
263
|
+
default:
|
|
264
|
+
return 'unknown'
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
let typeUnion = mapType(schema.type[0])
|
|
268
|
+
for (let i = 1; i < schema.type.length; i++) {
|
|
269
|
+
typeUnion += ' | ' + mapType(schema.type[i])
|
|
270
|
+
}
|
|
271
|
+
return typeUnion
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
switch (schema.type) {
|
|
275
|
+
// String
|
|
276
|
+
case 'string':
|
|
277
|
+
return 'string'
|
|
278
|
+
|
|
279
|
+
// Number
|
|
280
|
+
case 'number':
|
|
281
|
+
case 'integer':
|
|
282
|
+
return 'number'
|
|
283
|
+
|
|
284
|
+
// Boolean
|
|
285
|
+
case 'boolean':
|
|
286
|
+
return 'boolean'
|
|
287
|
+
|
|
288
|
+
// Array
|
|
289
|
+
case 'array':
|
|
290
|
+
if (schema.items) {
|
|
291
|
+
const itemType = getTypeScriptType(schema.items)
|
|
292
|
+
// Wrap union types in parentheses so `(A | B)[]` is not misread as `A | B[]`
|
|
293
|
+
const wrappedItemType = itemType.includes(' | ') ? `(${itemType})` : itemType
|
|
294
|
+
return `${wrappedItemType}[]`
|
|
295
|
+
}
|
|
296
|
+
return 'unknown[]'
|
|
297
|
+
|
|
298
|
+
// Object
|
|
299
|
+
case 'object':
|
|
300
|
+
if (schema.properties) {
|
|
301
|
+
let properties = ''
|
|
302
|
+
let first = true
|
|
303
|
+
for (const key in schema.properties) {
|
|
304
|
+
// schema.properties[key] is safe: key comes from iterating schema.properties
|
|
305
|
+
const propSchema = schema.properties[key]!
|
|
306
|
+
const isRequired = schema.required?.includes(key) ?? false
|
|
307
|
+
const optional = isRequired ? '' : '?'
|
|
308
|
+
const propType = getTypeScriptType(propSchema)
|
|
309
|
+
if (!first) properties += '; '
|
|
310
|
+
properties += safeKey(key) + optional + ': ' + propType
|
|
311
|
+
first = false
|
|
312
|
+
}
|
|
313
|
+
return '{ ' + properties + ' }'
|
|
314
|
+
}
|
|
315
|
+
// Handle additionalProperties with $ref or $dynamicRef
|
|
316
|
+
if (schema.additionalProperties && typeof schema.additionalProperties === 'object') {
|
|
317
|
+
const additionalPropType = getTypeScriptType(schema.additionalProperties)
|
|
318
|
+
return `Record<string, ${additionalPropType}>`
|
|
319
|
+
}
|
|
320
|
+
// Handle patternProperties as a Record type
|
|
321
|
+
if (schema.patternProperties && typeof schema.patternProperties === 'object') {
|
|
322
|
+
const firstEntry = Object.entries(schema.patternProperties)[0]
|
|
323
|
+
if (firstEntry) {
|
|
324
|
+
const [pattern, patternVal] = firstEntry
|
|
325
|
+
if (patternVal) {
|
|
326
|
+
const valueType = getTypeScriptType(patternVal)
|
|
327
|
+
if (pattern === '^x-') {
|
|
328
|
+
return `Record<\`x-\${string}\`, ${valueType}>`
|
|
329
|
+
}
|
|
330
|
+
return `Record<string, ${valueType}>`
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return 'object'
|
|
335
|
+
|
|
336
|
+
// Default to unknown
|
|
337
|
+
default:
|
|
338
|
+
return 'unknown'
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Generates a TypeScript type definition from a JSON Schema.
|
|
344
|
+
* Handles required vs optional properties based on the schema's required array.
|
|
345
|
+
* Uses $comment as inline JSDoc description when present.
|
|
346
|
+
*/
|
|
347
|
+
export const generateTypeDefinition = (schema: JSONSchema, typeName: string): string => {
|
|
348
|
+
// Handle non-object schemas first
|
|
349
|
+
if (!isObjectLikeSchema(schema)) {
|
|
350
|
+
const tsType = getTypeScriptType(schema)
|
|
351
|
+
let result = ''
|
|
352
|
+
|
|
353
|
+
if (isSchemaObject(schema) && schema.$comment && typeof schema.$comment === 'string') {
|
|
354
|
+
result += buildJsDocBlock(typeName, schema.$comment)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
result += `export type ${typeName} = ${tsType};`
|
|
358
|
+
return result
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (isObjectLikeSchema(schema)) {
|
|
362
|
+
const conditionalResult = getConditionalObjectSchema(schema)
|
|
363
|
+
const normalizedSchema = conditionalResult?.schema ?? schema
|
|
364
|
+
const conditionalThenRef = conditionalResult?.thenRef ?? null
|
|
365
|
+
let jsDocTitle: string | undefined
|
|
366
|
+
let jsDocDescription: string | undefined
|
|
367
|
+
|
|
368
|
+
if (isSchemaObject(schema) && schema.$comment && typeof schema.$comment === 'string') {
|
|
369
|
+
jsDocTitle = typeName
|
|
370
|
+
jsDocDescription = schema.$comment
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const hasProperties = normalizedSchema.properties && Object.keys(normalizedSchema.properties).length > 0
|
|
374
|
+
const hasAdditionalProperties =
|
|
375
|
+
normalizedSchema.additionalProperties && typeof normalizedSchema.additionalProperties === 'object'
|
|
376
|
+
const hasPatternProperties =
|
|
377
|
+
normalizedSchema.patternProperties &&
|
|
378
|
+
typeof normalizedSchema.patternProperties === 'object' &&
|
|
379
|
+
Object.keys(normalizedSchema.patternProperties).length > 0
|
|
380
|
+
|
|
381
|
+
// Handle objects with only patternProperties (no fixed properties)
|
|
382
|
+
if (!hasProperties && hasPatternProperties && normalizedSchema.patternProperties) {
|
|
383
|
+
const firstEntry = Object.entries(normalizedSchema.patternProperties)[0]
|
|
384
|
+
const firstPattern = firstEntry?.[0]
|
|
385
|
+
const firstPatternProperty = firstEntry?.[1]
|
|
386
|
+
|
|
387
|
+
if (firstPatternProperty === undefined) {
|
|
388
|
+
return `export type ${typeName} = Record<string, unknown>;`
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const patternPropType =
|
|
392
|
+
typeof firstPatternProperty === 'boolean'
|
|
393
|
+
? getBooleanSubSchemaType(firstPatternProperty)
|
|
394
|
+
: getTypeScriptType(firstPatternProperty)
|
|
395
|
+
|
|
396
|
+
// The ^x- pattern is a common JSON Schema convention for vendor extensions
|
|
397
|
+
// that maps naturally to the TypeScript template literal `x-${string}`
|
|
398
|
+
const keyType = firstPattern === '^x-' ? '`x-${string}`' : 'string'
|
|
399
|
+
|
|
400
|
+
let result = ''
|
|
401
|
+
if (jsDocTitle && jsDocDescription) {
|
|
402
|
+
result += buildJsDocBlock(jsDocTitle, jsDocDescription)
|
|
403
|
+
}
|
|
404
|
+
result += `export type ${typeName} = Record<${keyType}, ${patternPropType}>;`
|
|
405
|
+
|
|
406
|
+
return result
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// Handle objects with only additionalProperties (no fixed properties)
|
|
410
|
+
if (!hasProperties && hasAdditionalProperties && normalizedSchema.additionalProperties) {
|
|
411
|
+
const additionalPropType = getTypeScriptType(normalizedSchema.additionalProperties)
|
|
412
|
+
|
|
413
|
+
let result = ''
|
|
414
|
+
if (jsDocTitle && jsDocDescription) {
|
|
415
|
+
result += buildJsDocBlock(jsDocTitle, jsDocDescription)
|
|
416
|
+
}
|
|
417
|
+
result += `export type ${typeName} = {\n [key: string]: ${additionalPropType};\n};`
|
|
418
|
+
|
|
419
|
+
return result
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const schemaProps = normalizedSchema.properties ?? {}
|
|
423
|
+
let properties = ''
|
|
424
|
+
let isFirstProp = true
|
|
425
|
+
for (const key in schemaProps) {
|
|
426
|
+
// schemaProps[key] is safe: key comes from iterating schemaProps
|
|
427
|
+
const propSchema = schemaProps[key]!
|
|
428
|
+
const isRequired = normalizedSchema.required?.includes(key) ?? false
|
|
429
|
+
const optional = isRequired ? '' : '?'
|
|
430
|
+
const propType = getTypeScriptType(propSchema)
|
|
431
|
+
const quotedKey = safeKey(key)
|
|
432
|
+
|
|
433
|
+
if (!isFirstProp) properties += '\n'
|
|
434
|
+
isFirstProp = false
|
|
435
|
+
|
|
436
|
+
// Add JSDoc comment from $comment or description if available
|
|
437
|
+
const inlineDescription =
|
|
438
|
+
isSchemaObject(propSchema) && typeof propSchema.description === 'string'
|
|
439
|
+
? propSchema.description
|
|
440
|
+
: isSchemaObject(propSchema) && typeof propSchema.$comment === 'string'
|
|
441
|
+
? propSchema.$comment
|
|
442
|
+
: undefined
|
|
443
|
+
if (inlineDescription) {
|
|
444
|
+
properties += ' /** ' + inlineDescription + ' */\n ' + quotedKey + optional + ': ' + propType + ';'
|
|
445
|
+
} else {
|
|
446
|
+
properties += ' ' + quotedKey + optional + ': ' + propType + ';'
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// Collect allOf $ref intersections
|
|
451
|
+
const allOfIntersections: string[] = []
|
|
452
|
+
if (isSchemaObject(schema) && Array.isArray(schema.allOf)) {
|
|
453
|
+
for (const entry of schema.allOf) {
|
|
454
|
+
if (isSchemaObject(entry) && entry.$ref) {
|
|
455
|
+
allOfIntersections.push(refToName(entry.$ref))
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// JSON Schema 2019-09+ allows $ref as a sibling to other keywords.
|
|
461
|
+
// Treat it as an additional intersection type (e.g. for specification-extensions).
|
|
462
|
+
if (isSchemaObject(schema) && typeof schema.$ref === 'string' && schema.$ref.startsWith('#')) {
|
|
463
|
+
allOfIntersections.push(refToName(schema.$ref))
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
let result = ''
|
|
467
|
+
if (jsDocTitle && jsDocDescription) {
|
|
468
|
+
result += buildJsDocBlock(jsDocTitle, jsDocDescription)
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
let typeBody = '{\n' + properties + '\n}'
|
|
472
|
+
|
|
473
|
+
if (conditionalThenRef) {
|
|
474
|
+
typeBody += ' & ' + refToName(conditionalThenRef)
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
for (const intersectionType of allOfIntersections) {
|
|
478
|
+
typeBody += ' & ' + intersectionType
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
result += 'export type ' + typeName + ' = ' + typeBody + ';'
|
|
482
|
+
|
|
483
|
+
return result
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
return 'export type ' + typeName + ' = unknown;'
|
|
487
|
+
}
|
package/src/has-ref.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Type guard to check if a value is a non-null, non-array object with a string $ref property */
|
|
2
|
+
export const hasRef = (value: unknown): value is { $ref: string } & Record<string, unknown> => {
|
|
3
|
+
return (
|
|
4
|
+
typeof value === 'object' &&
|
|
5
|
+
value !== null &&
|
|
6
|
+
!Array.isArray(value) &&
|
|
7
|
+
'$ref' in value &&
|
|
8
|
+
typeof (value as { $ref: unknown }).$ref === 'string'
|
|
9
|
+
)
|
|
10
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { isObject } from './is-object'
|
|
4
|
+
|
|
5
|
+
describe('is-object', () => {
|
|
6
|
+
it('returns true for plain object', () => {
|
|
7
|
+
expect(isObject({})).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('returns true for object with properties', () => {
|
|
11
|
+
expect(isObject({ a: 1, b: 'hello' })).toBe(true)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('returns false for null', () => {
|
|
15
|
+
expect(isObject(null)).toBe(false)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('returns false for undefined', () => {
|
|
19
|
+
expect(isObject(undefined)).toBe(false)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('returns false for array', () => {
|
|
23
|
+
expect(isObject([])).toBe(false)
|
|
24
|
+
expect(isObject([1, 2, 3])).toBe(false)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('returns false for string', () => {
|
|
28
|
+
expect(isObject('hello')).toBe(false)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('returns false for number', () => {
|
|
32
|
+
expect(isObject(42)).toBe(false)
|
|
33
|
+
expect(isObject(0)).toBe(false)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('returns false for boolean', () => {
|
|
37
|
+
expect(isObject(true)).toBe(false)
|
|
38
|
+
expect(isObject(false)).toBe(false)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('returns true for Date object', () => {
|
|
42
|
+
// This implementation returns true for Date since it is a simple typeof check
|
|
43
|
+
expect(isObject(new Date())).toBe(true)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('returns true for RegExp object', () => {
|
|
47
|
+
expect(isObject(/test/)).toBe(true)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('returns true for nested object', () => {
|
|
51
|
+
expect(isObject({ nested: { deep: true } })).toBe(true)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('returns true for Object.create(null)', () => {
|
|
55
|
+
expect(isObject(Object.create(null))).toBe(true)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('returns false for function', () => {
|
|
59
|
+
expect(isObject(() => {})).toBe(false)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('returns false for empty string', () => {
|
|
63
|
+
expect(isObject('')).toBe(false)
|
|
64
|
+
})
|
|
65
|
+
})
|
package/src/is-object.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns true if the provided value is a plain object.
|
|
3
|
+
* Optimized for JSON data validation: checks that value is truthy, typeof object,
|
|
4
|
+
* and not an array. This correctly handles all JSON value types.
|
|
5
|
+
*
|
|
6
|
+
* Examples:
|
|
7
|
+
* isObject({}) // true
|
|
8
|
+
* isObject({ a: 1 }) // true
|
|
9
|
+
* isObject([]) // false (Array)
|
|
10
|
+
* isObject(null) // false
|
|
11
|
+
* isObject(123) // false
|
|
12
|
+
* isObject('string') // false
|
|
13
|
+
*/
|
|
14
|
+
export const isObject = (value: unknown): value is Record<string, unknown> =>
|
|
15
|
+
!!value && typeof value === 'object' && !Array.isArray(value)
|