@@ -9,7 +9,10 @@ import {
99import { Box } from '@stoplight/mosaic' ;
1010import * as React from 'react' ;
1111
12+ import { COMMON_JSON_SCHEMA_AND_OAS_FORMATS } from '../../consts' ;
13+ import { isPrimitiveArray } from '../../tree' ;
1214import { printName } from '../../utils' ;
15+ import { Format } from './Format' ;
1316
1417function shouldRenderName ( type : SchemaNodeKind | SchemaCombinerName | '$ref' ) : boolean {
1518 return type === SchemaNodeKind . Array || type === SchemaNodeKind . Object || type === '$ref' ;
@@ -29,6 +32,32 @@ function getTypes(schemaNode: RegularNode): Array<SchemaNodeKind | SchemaCombine
2932 ) ;
3033}
3134
35+ function getFormats ( schemaNode : RegularNode ) : Partial < Record < SchemaNodeKind , string > > {
36+ const formats : Partial < Record < SchemaNodeKind , string > > = { } ;
37+
38+ if ( isPrimitiveArray ( schemaNode ) && schemaNode . children [ 0 ] . format !== null ) {
39+ formats . array = schemaNode . children [ 0 ] . format ;
40+ }
41+
42+ if ( schemaNode . format === null ) {
43+ return formats ;
44+ }
45+
46+ const types = getTypes ( schemaNode ) ;
47+
48+ for ( const type of types ) {
49+ if ( ! ( type in COMMON_JSON_SCHEMA_AND_OAS_FORMATS ) ) continue ;
50+
51+ if ( COMMON_JSON_SCHEMA_AND_OAS_FORMATS [ type ] . includes ( schemaNode . format ) ) {
52+ formats [ type ] = schemaNode . format ;
53+ return formats ;
54+ }
55+ }
56+
57+ formats . string = schemaNode . format ;
58+ return formats ;
59+ }
60+
3261export const Types : React . FunctionComponent < { schemaNode : SchemaNode } > = ( { schemaNode } ) => {
3362 if ( isReferenceNode ( schemaNode ) ) {
3463 return (
@@ -43,14 +72,20 @@ export const Types: React.FunctionComponent<{ schemaNode: SchemaNode }> = ({ sch
4372 }
4473
4574 const types = getTypes ( schemaNode ) ;
75+ const formats = getFormats ( schemaNode ) ;
4676
47- if ( types . length === 0 ) return null ;
77+ if ( types . length === 0 ) {
78+ return formats . string !== void 0 ? < Format format = { formats . string } /> : null ;
79+ }
4880
4981 const rendered = types . map ( ( type , i , { length } ) => (
5082 < React . Fragment key = { type } >
5183 < Box as = "span" textOverflow = "truncate" color = "muted" >
5284 { shouldRenderName ( type ) ? printName ( schemaNode ) ?? type : type }
5385 </ Box >
86+
87+ { type in formats ? < Format format = { formats [ type ] } /> : null }
88+
5489 { i < length - 1 && (
5590 < Box as = "span" key = { `${ i } -sep` } color = "muted" >
5691 { ' or ' }
0 commit comments