Skip to content

Commit f7df910

Browse files
authored
feat: primary type (#18)
* feat: type inference * fix: subtype validations * revert: "fix: subtype validations" This reverts commit 25463f3. * chore: lodash imports * fix: array of
1 parent c50ec21 commit f7df910

File tree

16 files changed

+162
-104
lines changed

16 files changed

+162
-104
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "array",
3+
"items": {
4+
"$ref": "./models/todo-full.json"
5+
}
6+
}

src/components/SchemaTree.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { TreeList, TreeListMouseEventHandler, TreeStore } from '@stoplight/tree-
22
import { Omit } from '@stoplight/types';
33
import { Box, IBox, ThemeZone } from '@stoplight/ui-kit';
44
import { JSONSchema4 } from 'json-schema';
5-
import _isEmpty = require('lodash/isEmpty');
5+
import { isEmpty as _isEmpty } from 'lodash';
66
import * as React from 'react';
77
import { useMetadata } from '../hooks';
88
import { useTheme } from '../theme';

src/components/Type.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Box, IBoxCSS } from '@stoplight/ui-kit';
22
import { JSONSchema4TypeName } from 'json-schema';
33
import * as React from 'react';
44
import { IJsonSchemaViewerTheme, useTheme } from '../theme';
5-
import { JSONSchema4CombinerName } from '../types';
5+
import { ITreeNodeMeta, JSONSchema4CombinerName } from '../types';
66

77
export const Type: React.FunctionComponent<IType> = ({ type, subtype, children }) => {
88
const theme = useTheme() as IJsonSchemaViewerTheme;
@@ -18,7 +18,7 @@ export const Type: React.FunctionComponent<IType> = ({ type, subtype, children }
1818

1919
export interface IType {
2020
type: JSONSchema4TypeName | JSONSchema4CombinerName | '$ref';
21-
subtype?: JSONSchema4TypeName | JSONSchema4TypeName[];
21+
subtype?: ITreeNodeMeta['subtype'];
2222
}
2323

2424
export const rowStyles = (theme: IJsonSchemaViewerTheme, { type }: IType): IBoxCSS => {

src/components/Types.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { JSONSchema4TypeName } from 'json-schema';
22
import * as React from 'react';
3-
import { JSONSchema4CombinerName } from '../types';
3+
import { ITreeNodeMeta, JSONSchema4CombinerName } from '../types';
44
import { MutedText } from './common/MutedText';
55
import { Type } from './Type';
66

77
interface ITypes {
88
type?: JSONSchema4TypeName | JSONSchema4TypeName[] | JSONSchema4CombinerName;
9-
subtype?: JSONSchema4TypeName | JSONSchema4TypeName[];
9+
subtype?: ITreeNodeMeta['subtype'];
1010
}
1111

1212
export const Types: React.FunctionComponent<ITypes> = ({ type, subtype }) => {

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export interface ITreeNodeMeta {
5656
additional?: IArrayNode['additionalItems'] | IObjectNode['additionalProperties'];
5757
path: JsonPath;
5858
divider?: string;
59-
subtype?: IBaseNode['type'];
59+
subtype?: IBaseNode['type'] | string;
6060
expanded?: boolean;
6161
required?: boolean;
6262
inheritedFrom?: string;

src/utils/__tests__/__snapshots__/renderSchema.spec.ts.snap

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ Array [
4949
],
5050
"required": false,
5151
"subtype": "object",
52-
"type": "array",
52+
"type": Array [
53+
"array",
54+
],
5355
"validations": Object {},
5456
},
5557
"name": "",
@@ -77,6 +79,27 @@ Array [
7779
]
7880
`;
7981

82+
exports[`renderSchema util should match array-of-refs.json 1`] = `
83+
Array [
84+
Object {
85+
"id": "random-id",
86+
"level": 0,
87+
"metadata": Object {
88+
"additionalItems": undefined,
89+
"annotations": Object {},
90+
"enum": undefined,
91+
"id": "random-id",
92+
"items": undefined,
93+
"path": Array [],
94+
"subtype": "$ref( ./models/todo-full.json )",
95+
"type": "array",
96+
"validations": Object {},
97+
},
98+
"name": "",
99+
},
100+
]
101+
`;
102+
80103
exports[`renderSchema util should match combiner-schema.json 1`] = `
81104
Array [
82105
Object {
@@ -479,9 +502,11 @@ Array [
479502
"id": "random-id",
480503
"level": 1,
481504
"metadata": Object {
505+
"additionalItems": undefined,
482506
"annotations": Object {},
483507
"enum": undefined,
484508
"id": "random-id",
509+
"items": undefined,
485510
"name": "items",
486511
"path": Array [
487512
"properties",

src/utils/__tests__/renderSchema.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('renderSchema util', () => {
1414
['ref/original.json', 'ref/resolved.json'],
1515
['combiner-schema.json', ''],
1616
['array-of-objects.json', ''],
17+
['array-of-refs.json', ''],
1718
])('should match %s', (schema, dereferenced) => {
1819
expect(
1920
Array.from(

src/utils/getAnnotations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { JSONSchema4 } from 'json-schema';
2-
import _pick = require('lodash/pick');
2+
import { pick as _pick } from 'lodash';
33
import { JSONSchema4Annotations } from '../types';
44

55
const ANNOTATIONS: JSONSchema4Annotations[] = ['title', 'description', 'default', 'examples'];

src/utils/getMetadata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { JSONSchema4 } from 'json-schema';
2-
import _pick = require('lodash/pick');
2+
import { pick as _pick } from 'lodash';
33
import { JSONSchema4Metadata } from '../types';
44

55
const METADATA: JSONSchema4Metadata[] = ['id', '$schema'];

src/utils/getPrimaryType.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { JSONSchema4 } from 'json-schema';
2+
import { SchemaKind } from '../types';
3+
import { inferType } from './inferType';
4+
5+
export function getPrimaryType(node: JSONSchema4) {
6+
if (node.type !== undefined) {
7+
if (Array.isArray(node.type)) {
8+
if (node.type.includes(SchemaKind.Object)) {
9+
return SchemaKind.Object;
10+
}
11+
12+
if (node.type.includes(SchemaKind.Array)) {
13+
return SchemaKind.Array;
14+
}
15+
}
16+
17+
return node.type;
18+
}
19+
20+
return inferType(node);
21+
}

0 commit comments

Comments
 (0)