import { isBooleanishNode, isReferenceNode, isRegularNode, RegularNode, SchemaCombinerName, SchemaNode, SchemaNodeKind, } from '@stoplight/json-schema-tree'; import { Box } from '@stoplight/mosaic'; import * as React from 'react'; import { printName } from '../../utils'; import { getApplicableFormats } from '../../utils/getApplicableFormats'; function shouldRenderName(type: SchemaNodeKind | SchemaCombinerName | '$ref'): boolean { return type === SchemaNodeKind.Array || type === SchemaNodeKind.Object || type === '$ref'; } function getTypes(schemaNode: RegularNode): Array { return [schemaNode.types, schemaNode.combiners].reduce>( (values, value) => { if (value === null) { return values; } values.push(...value); return values; }, [], ); } export const Types: React.FunctionComponent<{ schemaNode: SchemaNode }> = ({ schemaNode }) => { if (isReferenceNode(schemaNode)) { return ( {schemaNode.value ?? '$ref'} ); } if (isBooleanishNode(schemaNode)) { return ( {schemaNode.fragment ? 'any' : 'never'} ); } if (!isRegularNode(schemaNode)) { return null; } const formats = getApplicableFormats(schemaNode); const types = getTypes(schemaNode); if (types.length === 0) { return ( {formats === null ? 'any' : `<${formats[1]}>`} ); } const rendered = types.map((type, i, { length }) => { let printedName; if (shouldRenderName(type)) { printedName = printName(schemaNode); } printedName ??= type + (formats === null || formats[0] !== type ? '' : `<${formats[1]}>`); return ( {printedName} {i < length - 1 && ( {' or '} )} ); }); return rendered.length > 1 ? {rendered} : <>{rendered}; }; Types.displayName = 'JsonSchemaViewer.Types';