Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/components/__tests__/Property.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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(<Property node={node as SchemaNodeWithMeta} />);
expect(wrapper).not.toBeEmptyRender();
});
});
6 changes: 2 additions & 4 deletions src/components/shared/Property.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,10 +13,7 @@ export interface IProperty {

export const Property: React.FunctionComponent<IProperty> = ({ 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<number | null>(
() => {
Expand Down
8 changes: 8 additions & 0 deletions src/utils/guards.ts
Original file line number Diff line number Diff line change
@@ -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<IArrayNode, 'items'> & { items: JSONSchema4 | JSONSchema4[] } =>
'type' in node && 'items' in node && node.type === SchemaKind.Array && _isObjectLike(node.items);