@@ -3,8 +3,9 @@ export function getTypeInfo(schema) {
3
3
if ( ! schema ) {
4
4
return ;
5
5
}
6
-
7
6
let dataType = '' ;
7
+ let constrain = '' ;
8
+
8
9
if ( schema . $ref ) {
9
10
const n = schema . $ref . lastIndexOf ( '/' ) ;
10
11
const schemaNode = schema . $ref . substring ( n + 1 ) ;
@@ -43,52 +44,44 @@ export function getTypeInfo(schema) {
43
44
arrayType : '' ,
44
45
html : '' ,
45
46
} ;
47
+
46
48
if ( info . type === '{recursive}' ) {
47
49
info . description = schema . $ref . substring ( schema . $ref . lastIndexOf ( '/' ) + 1 ) ;
48
50
} else if ( info . type === '{missing-type-info}' ) {
49
51
info . description = info . description || '' ;
50
52
}
51
53
52
54
// Set Allowed Values
53
- if ( schema . enum ) {
54
- let opt = '' ;
55
- schema . enum . map ( ( v ) => {
56
- opt += `${ v } , ` ;
57
- } ) ;
58
- info . allowedValues = opt . slice ( 0 , - 2 ) ;
59
- }
55
+ info . allowedValues = Array . isArray ( schema . enum ) ? schema . enum . join ( '┃' ) : '' ;
56
+ if ( dataType === 'array' && schema . items ) {
57
+ const arrayItemType = schema . items ?. type ;
58
+ const arrayItemDefault = schema . items ?. default !== undefined ? schema . items . default : '' ;
60
59
61
- if ( schema . type === 'array' && schema . items ) {
62
- const arraySchema = schema . items ;
63
- info . arrayType = `${ schema . type } of ${ arraySchema . type } ` ;
64
- info . default = arraySchema . default === 0 ? '0 ' : ( arraySchema . default ? arraySchema . default : '' ) ;
65
- if ( arraySchema . enum ) {
66
- let opt = '' ;
67
- arraySchema . enum . map ( ( v ) => {
68
- opt += `${ v } , ` ;
69
- } ) ;
70
- info . allowedValues = opt . slice ( 0 , - 2 ) ;
60
+ info . arrayType = `${ schema . type } of ${ Array . isArray ( arrayItemType ) ? arrayItemType . join ( '' ) : arrayItemType } ` ;
61
+ info . default = arrayItemDefault ;
62
+ info . allowedValues = Array . isArray ( schema . items ?. enum ) ? schema . items . enum . join ( '┃' ) : '' ;
63
+ }
64
+ if ( dataType . match ( / i n t e g e r | n u m b e r / g) ) {
65
+ if ( schema . minimum !== undefined || schema . exclusiveMinimum !== undefined ) {
66
+ constrain += schema . minimum !== undefined ? `>= ${ schema . minimum } ` : `> ${ schema . exclusiveMinimum } ` ;
71
67
}
72
- } else if ( schema . type === 'integer' || schema . type === 'number' ) {
73
- if ( schema . minimum !== undefined && schema . maximum !== undefined ) {
74
- info . constrain = `${ schema . exclusiveMinimum ? '>' : '>=' } ${ schema . minimum } and ${ schema . exclusiveMaximum ? '<' : '<=' } ${ schema . maximum } ` ;
75
- } else if ( schema . minimum !== undefined && schema . maximum === undefined ) {
76
- info . constrain = `${ schema . exclusiveMinimum ? '>' : '>=' } ${ schema . minimum } ` ;
77
- } else if ( schema . minimum === undefined && schema . maximum !== undefined ) {
78
- info . constrain = `${ schema . exclusiveMaximum ? '<' : '<=' } ${ schema . maximum } ` ;
68
+ if ( schema . maximum !== undefined || schema . exclusiveMaximum !== undefined ) {
69
+ constrain += schema . maximum !== undefined ? `${ constrain ? '┃' : '' } <= ${ schema . maximum } ` : `${ constrain ? '┃' : '' } < ${ schema . exclusiveMaximum } ` ;
79
70
}
80
71
if ( schema . multipleOf !== undefined ) {
81
- info . constrain = `( multiple of ${ schema . multipleOf } ) ` ;
72
+ constrain + = `${ constrain ? '┃' : '' } multiple of ${ schema . multipleOf } ` ;
82
73
}
83
- } else if ( schema . type === 'string' ) {
74
+ }
75
+ if ( dataType . match ( / s t r i n g / g) ) {
84
76
if ( schema . minLength !== undefined && schema . maxLength !== undefined ) {
85
- info . constrain = `( ${ schema . minLength } to ${ schema . maxLength } chars) ` ;
86
- } else if ( schema . minLength !== undefined && schema . maxLength === undefined ) {
87
- info . constrain = `min ${ schema . minLength } chars` ;
88
- } else if ( schema . minLength === undefined && schema . maxLength !== undefined ) {
89
- info . constrain = `max ${ schema . maxLength } chars` ;
77
+ constrain + = `${ constrain ? '┃' : '' } ${ schema . minLength } to ${ schema . maxLength } chars` ;
78
+ } else if ( schema . minLength !== undefined ) {
79
+ constrain + = `${ constrain ? '┃' : '' } min ${ schema . minLength } chars` ;
80
+ } else if ( schema . maxLength !== undefined ) {
81
+ constrain + = `max ${ constrain ? '┃' : '' } ${ schema . maxLength } chars` ;
90
82
}
91
83
}
84
+ info . constrain = constrain ;
92
85
info . html = `${ info . type } ~|~${ info . readOrWriteOnly } ~|~${ info . constrain } ~|~${ info . default } ~|~${ info . allowedValues } ~|~${ info . pattern } ~|~${ info . description } ~|~${ schema . title || '' } ~|~${ info . deprecated ? 'deprecated' : '' } ` ;
93
86
return info ;
94
87
}
@@ -478,9 +471,14 @@ export function schemaInObjectNotation(schema, obj, level = 0, suffix = '') {
478
471
subSchema . type . forEach ( ( v ) => {
479
472
if ( v . match ( / i n t e g e r | n u m b e r | s t r i n g | n u l l | b o o l e a n / g) ) {
480
473
primitiveType . push ( v ) ;
481
- } else if ( v === 'array' && subSchema . items ?. type . match ( / i n t e g e r | n u m b e r | s t r i n g | n u l l | b o o l e a n / g) ) {
474
+ } else if ( v === 'array' && typeof subSchema . items ?. type === 'string' && subSchema . items ?. type . match ( / i n t e g e r | n u m b e r | s t r i n g | n u l l | b o o l e a n / g) ) {
482
475
// Array with primitive types should also be treated as primitive type
476
+
477
+ // if (subSchema.items.type === 'string' && subSchema.items.format) {
478
+ // primitiveType.push(`[${subSchema.items.format}]`);
479
+ // } else {
483
480
primitiveType . push ( `[${ subSchema . items . type } ]` ) ;
481
+ // }
484
482
} else {
485
483
complexTypes . push ( v ) ;
486
484
}
0 commit comments