import { Dictionary, Optional } from '@stoplight/types'; import cn from 'classnames'; import { JSONSchema4TypeName } from 'json-schema'; import * as React from 'react'; import { JSONSchema4CombinerName, SchemaKind } from '../../types'; /** * TYPE */ export interface IType { type: JSONSchema4TypeName | JSONSchema4CombinerName | 'binary' | '$ref'; subtype: Optional | '$ref'; className?: string; title: Optional; } function shouldRenderTitle(type: string): boolean { return type === SchemaKind.Array || type === SchemaKind.Object || type === '$ref'; } function getPrintableArrayType(subtype: IType['subtype'], title: IType['title']): string { if (!subtype) return SchemaKind.Array; if (Array.isArray(subtype)) { return `${SchemaKind.Array}[${subtype.join(',')}]`; } if (title && shouldRenderTitle(subtype)) { return `${title}[]`; } if (subtype !== SchemaKind.Array && subtype !== '$ref') { return `${SchemaKind.Array}[${subtype}]`; } return SchemaKind.Array; } function getPrintableType(type: IType['type'], subtype: IType['subtype'], title: IType['title']): string { if (type === SchemaKind.Array) { return getPrintableArrayType(subtype, title); } else if (title && shouldRenderTitle(type)) { return title; } else { return type; } } export const Type: React.FunctionComponent = ({ className, title, type, subtype }) => { return ( {getPrintableType(type, subtype, title)} ); }; Type.displayName = 'JsonSchemaViewer.Type'; /** * TYPES */ interface ITypes { className?: string; type: Optional; subtype: Optional; title: Optional; } export const Types: React.FunctionComponent = ({ className, title, type, subtype }) => { if (type === void 0) return null; if (!Array.isArray(type)) { return ; } return (
<> {type.map((name, i, { length }) => ( {i < length - 1 && ( {' or '} )} ))}
); }; Types.displayName = 'JsonSchemaViewer.Types'; /** * HELPERS */ export const PropertyTypeColors: Dictionary = { object: 'text-blue-6 dark:text-blue-4', any: 'text-blue-5', array: 'text-green-6 dark:text-green-4', allOf: 'text-orange-5', anyOf: 'text-orange-5', oneOf: 'text-orange-5', null: 'text-orange-5', integer: 'text-red-7 dark:text-red-6', number: 'text-red-7 dark:text-red-6', boolean: 'text-red-4', binary: 'text-green-4', string: 'text-green-7 dark:text-green-5', $ref: 'text-purple-6 dark:text-purple-4', };