Skip to content

Commit d56181f

Browse files
authored
fix: include subtypes for complex types that include array (#85)
1 parent 178b746 commit d56181f

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/tree/__tests__/tree.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,46 @@ describe('SchemaTree', () => {
12851285
`);
12861286
});
12871287

1288+
test('given complex type that includes array and complex array subtype, should not ignore subtype', () => {
1289+
const schema: JSONSchema4 = {
1290+
type: 'object',
1291+
properties: {
1292+
items: {
1293+
type: ['null', 'array'],
1294+
items: {
1295+
type: ['string', 'number'],
1296+
},
1297+
description:
1298+
"This description can be long and should truncate once it reaches the end of the row. If it's not truncating then theres and issue that needs to be fixed. Help!",
1299+
},
1300+
},
1301+
};
1302+
1303+
const tree = new SchemaTree(schema, new SchemaTreeState(), {
1304+
expandedDepth: Infinity,
1305+
mergeAllOf: true,
1306+
resolveRef: void 0,
1307+
shouldResolveEagerly: true,
1308+
onPopulate: void 0,
1309+
});
1310+
1311+
tree.populate();
1312+
expect(printTree(tree)).toMatchInlineSnapshot(`
1313+
"└─ #
1314+
├─ type: object
1315+
└─ children
1316+
└─ 0
1317+
└─ #/properties/items
1318+
├─ type
1319+
│ ├─ 0: null
1320+
│ └─ 1: array
1321+
└─ subtype
1322+
├─ 0: string
1323+
└─ 1: number
1324+
"
1325+
`);
1326+
});
1327+
12881328
describe.each(['anyOf', 'oneOf'])('given %s combiner placed next to allOf', combiner => {
12891329
let schema: JSONSchema4;
12901330

src/utils/guards.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { IArrayNode, ICombinerNode, IRefNode, SchemaKind, SchemaNode } from '../
55
export const isArrayNodeWithItems = (
66
node: SchemaNode,
77
): node is Omit<IArrayNode, 'items'> & { items: JSONSchema4 | JSONSchema4[] } =>
8-
'type' in node && 'items' in node && node.type === SchemaKind.Array && _isObjectLike(node.items);
8+
'type' in node &&
9+
'items' in node &&
10+
(node.type === SchemaKind.Array || (Array.isArray(node.type) && node.type.includes(SchemaKind.Array))) &&
11+
_isObjectLike(node.items);
912

1013
export const isRefNode = (node: SchemaNode): node is IRefNode => '$ref' in node;
1114

0 commit comments

Comments
 (0)