Skip to content

Commit de7a986

Browse files
authored
fix: handle nullish items (#65)
* fix: handle nullish items * chore: add a type guard
1 parent af1d6e8 commit de7a986

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/components/__tests__/Property.spec.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { SchemaNodeWithMeta } from '../../types';
55
import { Property, Types } from '../shared';
66

77
describe('Property component', () => {
8-
it('should render Types with propper type and subtype', () => {
8+
it('should render Types with proper type and subtype', () => {
99
const node: SchemaNodeWithMeta = {
1010
id: '1',
1111
type: 'array',
@@ -24,4 +24,20 @@ describe('Property component', () => {
2424
expect(wrapper.find(Types)).toHaveProp('type', 'array');
2525
expect(wrapper.find(Types)).toHaveProp('subtype', 'string');
2626
});
27+
28+
it('should handle nullish items', () => {
29+
const node = {
30+
id: '1',
31+
type: 'array',
32+
items: null,
33+
annotations: {
34+
examples: {},
35+
},
36+
validations: {},
37+
path: [],
38+
};
39+
40+
const wrapper = shallow(<Property node={node as SchemaNodeWithMeta} />);
41+
expect(wrapper).not.toBeEmptyRender();
42+
});
2743
});

src/components/shared/Property.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { size as _size } from 'lodash-es';
22
import * as React from 'react';
33
import { GoToRefHandler, IArrayNode, IObjectNode, SchemaKind, SchemaNodeWithMeta } from '../../types';
4+
import { isArrayNodeWithItems } from '../../utils/guards';
45
import { inferType } from '../../utils/inferType';
56
import { isCombinerNode, isRefNode } from '../../utils/nodes';
67
import { Types } from './Types';
@@ -12,10 +13,7 @@ export interface IProperty {
1213

1314
export const Property: React.FunctionComponent<IProperty> = ({ node, onGoToRef }) => {
1415
const type = isRefNode(node) ? '$ref' : isCombinerNode(node) ? node.combiner : node.type;
15-
const subtype =
16-
type === SchemaKind.Array && (node as IArrayNode).items !== undefined
17-
? inferType((node as IArrayNode).items!)
18-
: undefined;
16+
const subtype = isArrayNodeWithItems(node) ? inferType(node.items) : undefined;
1917

2018
const childrenCount = React.useMemo<number | null>(
2119
() => {

src/utils/guards.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { JSONSchema4 } from 'json-schema';
2+
import { isObjectLike as _isObjectLike } from 'lodash-es';
3+
import { IArrayNode, SchemaKind, SchemaNode } from '../types';
4+
5+
export const isArrayNodeWithItems = (
6+
node: SchemaNode,
7+
): node is Omit<IArrayNode, 'items'> & { items: JSONSchema4 | JSONSchema4[] } =>
8+
'type' in node && 'items' in node && node.type === SchemaKind.Array && _isObjectLike(node.items);

0 commit comments

Comments
 (0)