diff --git a/src/components/__tests__/Property.spec.tsx b/src/components/__tests__/Property.spec.tsx index 3331148b..f0d5a214 100644 --- a/src/components/__tests__/Property.spec.tsx +++ b/src/components/__tests__/Property.spec.tsx @@ -5,7 +5,7 @@ import { SchemaNodeWithMeta } from '../../types'; import { Property, Types } from '../shared'; describe('Property component', () => { - it('should render Types with propper type and subtype', () => { + it('should render Types with proper type and subtype', () => { const node: SchemaNodeWithMeta = { id: '1', type: 'array', @@ -24,4 +24,20 @@ describe('Property component', () => { expect(wrapper.find(Types)).toHaveProp('type', 'array'); expect(wrapper.find(Types)).toHaveProp('subtype', 'string'); }); + + it('should handle nullish items', () => { + const node = { + id: '1', + type: 'array', + items: null, + annotations: { + examples: {}, + }, + validations: {}, + path: [], + }; + + const wrapper = shallow(); + expect(wrapper).not.toBeEmptyRender(); + }); }); diff --git a/src/components/shared/Property.tsx b/src/components/shared/Property.tsx index 37d23bf6..7ab39a03 100644 --- a/src/components/shared/Property.tsx +++ b/src/components/shared/Property.tsx @@ -1,6 +1,7 @@ import { size as _size } from 'lodash-es'; import * as React from 'react'; import { GoToRefHandler, IArrayNode, IObjectNode, SchemaKind, SchemaNodeWithMeta } from '../../types'; +import { isArrayNodeWithItems } from '../../utils/guards'; import { inferType } from '../../utils/inferType'; import { isCombinerNode, isRefNode } from '../../utils/nodes'; import { Types } from './Types'; @@ -12,10 +13,7 @@ export interface IProperty { export const Property: React.FunctionComponent = ({ node, onGoToRef }) => { const type = isRefNode(node) ? '$ref' : isCombinerNode(node) ? node.combiner : node.type; - const subtype = - type === SchemaKind.Array && (node as IArrayNode).items !== undefined - ? inferType((node as IArrayNode).items!) - : undefined; + const subtype = isArrayNodeWithItems(node) ? inferType(node.items) : undefined; const childrenCount = React.useMemo( () => { diff --git a/src/utils/guards.ts b/src/utils/guards.ts new file mode 100644 index 00000000..c3c404c3 --- /dev/null +++ b/src/utils/guards.ts @@ -0,0 +1,8 @@ +import { JSONSchema4 } from 'json-schema'; +import { isObjectLike as _isObjectLike } from 'lodash-es'; +import { IArrayNode, SchemaKind, SchemaNode } from '../types'; + +export const isArrayNodeWithItems = ( + node: SchemaNode, +): node is Omit & { items: JSONSchema4 | JSONSchema4[] } => + 'type' in node && 'items' in node && node.type === SchemaKind.Array && _isObjectLike(node.items);