1
- import { Refs } from '../types' ;
1
+ import { JsType , Refs , SupportedEnumType } from '../types' ;
2
2
import { JSONSchema6 } from 'json-schema' ;
3
3
import { table } from './common' ;
4
4
import slugify from 'slugify' ;
5
5
import { concatNewLine } from '../../common' ;
6
+ import { SUPPORTED_ENUM_TYPES } from '../constants' ;
6
7
7
8
type TableRow = [ string , string , string ] ;
8
9
@@ -20,7 +21,7 @@ export function tableFromSchema(allRefs: Refs, schema: JSONSchema6): {content: s
20
21
const description = prepareComplexDescription ( '' , schema ) ;
21
22
const content = table ( [
22
23
[ 'Type' , 'Description' ] ,
23
- [ schema . type , description ] ,
24
+ [ inferType ( schema ) , description ] ,
24
25
] ) ;
25
26
return { content, tableRefs : [ ] } ;
26
27
}
@@ -64,7 +65,7 @@ export function prepareTableRowData(allRefs: Refs, value: JSONSchema6, key?: str
64
65
if ( ref ) {
65
66
return { type : anchor ( ref ) , description, ref} ;
66
67
}
67
- if ( value . type === 'array' ) {
68
+ if ( inferType ( value ) === 'array' ) {
68
69
if ( ! value . items || value . items === true || Array . isArray ( value . items ) ) {
69
70
throw Error ( `unsupported array items for ${ key } ` ) ;
70
71
}
@@ -76,7 +77,7 @@ export function prepareTableRowData(allRefs: Refs, value: JSONSchema6, key?: str
76
77
ref : inner . ref ,
77
78
} ;
78
79
}
79
- return { type : `${ value . type } ` , description : prepareComplexDescription ( description , value ) } ;
80
+ return { type : `${ inferType ( value ) } ` , description : prepareComplexDescription ( description , value ) } ;
80
81
}
81
82
82
83
function prepareComplexDescription ( baseDescription : string , value : JSONSchema6 ) : string {
@@ -144,7 +145,7 @@ function prepareSampleElement(key: string, v: OpenJSONSchemaDefinition, required
144
145
return undefined ;
145
146
}
146
147
const downCallstack = callstack . concat ( value ) ;
147
- switch ( value . type ) {
148
+ switch ( inferType ( value ) ) {
148
149
case 'object' :
149
150
return prepareSampleObject ( value , downCallstack ) ;
150
151
case 'array' :
@@ -234,3 +235,21 @@ function merge(value: OpenJSONSchemaDefinition): OpenJSONSchema {
234
235
function isRequired ( key : string , value : JSONSchema6 ) : boolean {
235
236
return value . required ?. includes ( key ) ?? false ;
236
237
}
238
+
239
+ function inferType ( value : OpenJSONSchema ) : JSONSchema6 [ 'type' ] {
240
+ if ( value . type ) {
241
+ return value . type ;
242
+ }
243
+ if ( value . enum ) {
244
+ const enumType = typeof value . enum [ 0 ] ;
245
+ if ( isSupportedEnumType ( enumType ) ) {
246
+ return enumType ;
247
+ }
248
+ throw new Error ( 'Not supported type enum' ) ;
249
+ }
250
+ return value . type ;
251
+ }
252
+
253
+ function isSupportedEnumType ( enumType : JsType ) : enumType is SupportedEnumType {
254
+ return SUPPORTED_ENUM_TYPES . some ( ( type ) => enumType === type ) ;
255
+ }
0 commit comments