From 2eb6acd9e2b5858b949a858db0654fe1b094936c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Mon, 26 Apr 2021 22:39:15 +0200 Subject: [PATCH 1/2] feat: accept JSON Schema Draft 6 & Draft 7 --- package.json | 4 +-- src/__stories__/JsonSchemaViewer.tsx | 23 +++++++------ .../__snapshots__/index.spec.tsx.snap | 18 +++++------ src/__tests__/index.spec.tsx | 25 +++++++-------- src/components/JsonSchemaViewer.tsx | 4 +-- src/components/__tests__/SchemaRow.spec.tsx | 8 ++--- src/components/shared/ChildStack.tsx | 2 +- src/components/shared/Validations.tsx | 7 +--- .../shared/__tests__/Format.spec.tsx | 4 +-- .../shared/__tests__/Property.spec.tsx | 32 +++++++++---------- src/components/shared/__tests__/utils.ts | 5 +-- src/types.ts | 3 ++ yarn.lock | 29 +++++++++++++---- 13 files changed, 89 insertions(+), 75 deletions(-) diff --git a/package.json b/package.json index 7f3cd30a..2da432f3 100644 --- a/package.json +++ b/package.json @@ -43,9 +43,10 @@ "dependencies": { "@fortawesome/free-solid-svg-icons": "^5.15.2", "@stoplight/json": "^3.10.0", - "@stoplight/json-schema-tree": "^1.1.3", + "@stoplight/json-schema-tree": "^2.0.0", "@stoplight/mosaic": "1.0.0-beta.46", "@stoplight/react-error-boundary": "^1.0.0", + "@types/json-schema": "^7.0.7", "classnames": "^2.2.6", "lodash": "^4.17.19" }, @@ -61,7 +62,6 @@ "@types/classnames": "^2.2.11", "@types/enzyme": "^3.10.8", "@types/jest": "^26.0.18", - "@types/json-schema": "^7.0.6", "@types/lodash": "^4.14.149", "@types/node": "^12.7.2", "@types/react": "16.9.2", diff --git a/src/__stories__/JsonSchemaViewer.tsx b/src/__stories__/JsonSchemaViewer.tsx index c586d6d7..bb64ca73 100644 --- a/src/__stories__/JsonSchemaViewer.tsx +++ b/src/__stories__/JsonSchemaViewer.tsx @@ -2,10 +2,9 @@ import { Button, Flex, InvertTheme, subscribeTheme } from '@stoplight/mosaic'; import { action } from '@storybook/addon-actions'; import { boolean, number, object, select, withKnobs } from '@storybook/addon-knobs'; import { storiesOf } from '@storybook/react'; -import { JSONSchema4 } from 'json-schema'; import * as React from 'react'; -import { JsonSchemaViewer, RowAddonRenderer } from '../'; +import { JSONSchema, JsonSchemaViewer, RowAddonRenderer } from '../'; import { Wrapper } from './utils/Wrapper'; const allOfSchema = require('../__fixtures__/combiners/allOfs/base.json'); @@ -25,7 +24,7 @@ storiesOf('JsonSchemaViewer', module) .addDecorator(storyFn => {storyFn()}) .add('default', () => ( @@ -74,28 +73,28 @@ storiesOf('JsonSchemaViewer', module) )) .add('stress-test schema', () => ( )) .add('allOf-schema', () => ( )) .add('anyOf-array-schema', () => ( )) .add('anyOf-array-schema2', () => ( @@ -151,7 +150,7 @@ storiesOf('JsonSchemaViewer', module)
@@ -161,21 +160,21 @@ storiesOf('JsonSchemaViewer', module) }) .add('refs/normal', () => ( )) .add('refs/nullish', () => ( )) .add('refs/broken', () => ( diff --git a/src/__tests__/__snapshots__/index.spec.tsx.snap b/src/__tests__/__snapshots__/index.spec.tsx.snap index 0fbc2602..5e959e3c 100644 --- a/src/__tests__/__snapshots__/index.spec.tsx.snap +++ b/src/__tests__/__snapshots__/index.spec.tsx.snap @@ -1067,15 +1067,15 @@ exports[`HTML Output should match arrays/of-complex-objects.json 1`] = `
+
+ Allowed value: + \\"Constant name\\" +
Example values: \\"Example name\\" \\"Different name\\"
-
- Allowed value: - \\"Constant name\\" -
@@ -2816,15 +2816,15 @@ exports[`HTML Output should match default-schema.json 1`] = ` +
+ Allowed value: + \\"Constant name\\" +
Example values: \\"Example name\\" \\"Different name\\"
-
- Allowed value: - \\"Constant name\\" -
@@ -3671,7 +3671,7 @@ exports[`HTML Output should match tickets.schema.json 1`] = ` -
example
+
examples
diff --git a/src/__tests__/index.spec.tsx b/src/__tests__/index.spec.tsx index 6e8bb3c0..0a36502d 100644 --- a/src/__tests__/index.spec.tsx +++ b/src/__tests__/index.spec.tsx @@ -3,12 +3,11 @@ import 'jest-enzyme'; import { mount, ReactWrapper } from 'enzyme'; import * as fastGlob from 'fast-glob'; import * as fs from 'fs'; -import { JSONSchema4 } from 'json-schema'; import * as path from 'path'; import * as React from 'react'; import { JsonSchemaViewer } from '../components'; -import { ViewMode } from '../types'; +import { JSONSchema, ViewMode } from '../types'; import { dumpDom } from './utils/dumpDom'; describe('HTML Output', () => { @@ -24,7 +23,7 @@ describe('HTML Output', () => { }); describe.each(['anyOf', 'oneOf'])('given %s combiner placed next to allOf', combiner => { - let schema: JSONSchema4; + let schema: JSONSchema; beforeEach(() => { schema = { @@ -84,7 +83,7 @@ describe('HTML Output', () => { }); it('given array with oneOf containing items, should merge it correctly', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { oneOf: [ { items: { @@ -104,7 +103,7 @@ describe('HTML Output', () => { }); it.each(['standalone', 'read', 'write'])('given %s mode, should populate proper nodes', mode => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { type: ['string', 'object'], properties: { id: { @@ -124,7 +123,7 @@ describe('HTML Output', () => { }); it('given multiple object and string type, should process properties', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { type: ['string', 'object'], properties: { ids: { @@ -140,7 +139,7 @@ describe('HTML Output', () => { }); it('given complex type that includes array and complex array subtype, should not ignore subtype', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { type: 'object', properties: { items: { @@ -158,7 +157,7 @@ describe('HTML Output', () => { }); it('given visible $ref node, should try to inject the title immediately', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { type: 'object', properties: { foo: { @@ -202,7 +201,7 @@ describe('Expanded depth', () => { }); describe('merged array with object', () => { - let schema: JSONSchema4; + let schema: JSONSchema; beforeEach(() => { schema = { @@ -358,7 +357,7 @@ describe('Expanded depth', () => { }); describe('merged array with object #2', () => { - let schema: JSONSchema4; + let schema: JSONSchema; beforeEach(() => { schema = { @@ -578,7 +577,7 @@ describe('Expanded depth', () => { }); describe('nested object', () => { - let schema: JSONSchema4; + let schema: JSONSchema; beforeEach(() => { schema = { @@ -694,7 +693,7 @@ describe('Expanded depth', () => { describe('$ref resolving', () => { it('should render caret for schema with top-level $ref pointing at complex type', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { $ref: '#/definitions/foo', definitions: { foo: { @@ -725,7 +724,7 @@ describe('$ref resolving', () => { }); it('should render caret for top-level array with $ref items', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { type: 'array', items: { $ref: '#/foo', diff --git a/src/components/JsonSchemaViewer.tsx b/src/components/JsonSchemaViewer.tsx index 47d3557a..126a68be 100644 --- a/src/components/JsonSchemaViewer.tsx +++ b/src/components/JsonSchemaViewer.tsx @@ -2,15 +2,15 @@ import { isRegularNode, SchemaTree as JsonSchemaTree, SchemaTreeRefDereferenceFn import { Box, Provider as MosaicProvider } from '@stoplight/mosaic'; import { ErrorBoundaryForwardedProps, FallbackProps, withErrorBoundary } from '@stoplight/react-error-boundary'; import cn from 'classnames'; -import type { JSONSchema4 } from 'json-schema'; import * as React from 'react'; import { JSVOptions, JSVOptionsContextProvider } from '../contexts'; +import type { JSONSchema } from '../types'; import { TopLevelSchemaRow } from './SchemaRow'; import { ChildStack } from './shared/ChildStack'; export type JsonSchemaProps = Partial & { - schema: JSONSchema4; + schema: JSONSchema; emptyText?: string; className?: string; resolveRef?: SchemaTreeRefDereferenceFn; diff --git a/src/components/__tests__/SchemaRow.spec.tsx b/src/components/__tests__/SchemaRow.spec.tsx index 2f818dff..d2cb2e76 100644 --- a/src/components/__tests__/SchemaRow.spec.tsx +++ b/src/components/__tests__/SchemaRow.spec.tsx @@ -3,9 +3,9 @@ import 'jest-enzyme'; import { RootNode } from '@stoplight/json-schema-tree'; import { Icon } from '@stoplight/mosaic'; import { mount } from 'enzyme'; -import { JSONSchema4 } from 'json-schema'; import * as React from 'react'; +import { JSONSchema } from '../../types'; import { SchemaRow } from '../SchemaRow'; import { buildTree, findNodeWithPath } from '../shared/__tests__/utils'; import { Properties } from '../shared/Properties'; @@ -13,7 +13,7 @@ import { Properties } from '../shared/Properties'; describe('SchemaRow component', () => { describe('resolving error', () => { let tree: RootNode; - let schema: JSONSchema4; + let schema: JSONSchema; beforeEach(() => { schema = { @@ -50,9 +50,9 @@ describe('SchemaRow component', () => { }); describe('required property', () => { - let schema: JSONSchema4; + let schema: JSONSchema; - function isRequired(schema: JSONSchema4, nodePath: readonly string[], value: boolean) { + function isRequired(schema: JSONSchema, nodePath: readonly string[], value: boolean) { const tree = buildTree(schema); const schemaNode = findNodeWithPath(tree, nodePath); diff --git a/src/components/shared/ChildStack.tsx b/src/components/shared/ChildStack.tsx index 643b1061..76efab5c 100644 --- a/src/components/shared/ChildStack.tsx +++ b/src/components/shared/ChildStack.tsx @@ -1,4 +1,4 @@ -import { SchemaNode } from '@stoplight/json-schema-tree'; +import type { SchemaNode } from '@stoplight/json-schema-tree'; import * as React from 'react'; import { NESTING_OFFSET } from '../../consts'; diff --git a/src/components/shared/Validations.tsx b/src/components/shared/Validations.tsx index f697f4b4..71879635 100644 --- a/src/components/shared/Validations.tsx +++ b/src/components/shared/Validations.tsx @@ -25,7 +25,7 @@ export const numberValidationNames = [ 'exclusiveMaximum', ]; -const exampleValidationNames = ['examples', 'example', 'x-example']; +const exampleValidationNames = ['examples']; const excludedValidations = ['exclusiveMinimum', 'exclusiveMaximum', 'readOnly', 'writeOnly']; @@ -58,11 +58,8 @@ const createValidationsFormatter = (name: string, options?: { exact?: boolean; n }; const validationFormatters: Record ValidationFormat | null> = { - ['const']: createValidationsFormatter('Allowed'), enum: createValidationsFormatter('Allowed'), examples: createValidationsFormatter('Example'), - example: createValidationsFormatter('Example'), - ['x-example']: createValidationsFormatter('Example'), multipleOf: createValidationsFormatter('Multiple of', { exact: true }), pattern: createValidationsFormatter('Match pattern', { exact: true, nowrap: true }), default: createValidationsFormatter('Default'), @@ -208,8 +205,6 @@ export function getValidationsFromSchema(schemaNode: RegularNode) { ? { ...(schemaNode.annotations.default ? { default: schemaNode.annotations.default } : null), ...(schemaNode.annotations.examples ? { examples: schemaNode.annotations.examples } : null), - ...(schemaNode.annotations.const ? { const: schemaNode.annotations.const } : null), - ...(schemaNode.annotations['x-example'] ? { ['x-example']: schemaNode.annotations['x-example'] } : null), } : null), ...getFilteredValidations(schemaNode), diff --git a/src/components/shared/__tests__/Format.spec.tsx b/src/components/shared/__tests__/Format.spec.tsx index 465e5a3b..c669f275 100644 --- a/src/components/shared/__tests__/Format.spec.tsx +++ b/src/components/shared/__tests__/Format.spec.tsx @@ -1,15 +1,15 @@ import 'jest-enzyme'; import { mount } from 'enzyme'; -import { JSONSchema4 } from 'json-schema'; import * as React from 'react'; +import { JSONSchema } from '../../../types'; import { SchemaRow } from '../../SchemaRow'; import { Format } from '../Format'; import { buildTree, findNodeWithPath } from './utils'; describe('Format component', () => { - const schema: JSONSchema4 = require('../../../__fixtures__/formats-schema.json'); + const schema: JSONSchema = require('../../../__fixtures__/formats-schema.json'); let tree = buildTree(schema); it('should render next to a single type', () => { diff --git a/src/components/shared/__tests__/Property.spec.tsx b/src/components/shared/__tests__/Property.spec.tsx index f3e4cf02..b37b5e50 100644 --- a/src/components/shared/__tests__/Property.spec.tsx +++ b/src/components/shared/__tests__/Property.spec.tsx @@ -2,16 +2,16 @@ import 'jest-enzyme'; import { Provider as MosaicProvider } from '@stoplight/mosaic'; import { mount, ReactWrapper } from 'enzyme'; -import { JSONSchema4 } from 'json-schema'; import * as React from 'react'; import { SchemaRow, Types } from '../..'; +import { JSONSchema } from '../../../types'; import { buildTree, findNodeWithPath } from './utils'; describe('Property component', () => { const toUnmount: ReactWrapper[] = []; - function render(schema: JSONSchema4, nodePath?: readonly string[]) { + function render(schema: JSONSchema, nodePath?: readonly string[]) { const tree = buildTree(schema); const node = nodePath ? findNodeWithPath(tree, nodePath) : tree.children[0]; @@ -37,7 +37,7 @@ describe('Property component', () => { }); it('should render Types with proper type and subtype', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { type: 'array', items: { type: 'string', @@ -51,7 +51,7 @@ describe('Property component', () => { }); it('should handle nullish items', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { type: 'array', items: null as any, }; @@ -63,7 +63,7 @@ describe('Property component', () => { }); it('should handle nullish $ref', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { $ref: null as any, }; @@ -73,7 +73,7 @@ describe('Property component', () => { describe('properties names', () => { it('given an object, should display the names of its properties', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { properties: { foo: { type: 'string', @@ -88,7 +88,7 @@ describe('Property component', () => { }); it('given an object among other types, should still display its properties', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { type: ['string', 'object'], properties: { foo: { @@ -107,7 +107,7 @@ describe('Property component', () => { }); it('given an array of objects, should display names of those properties', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { type: 'array', items: { properties: { @@ -125,7 +125,7 @@ describe('Property component', () => { }); it('given an array with a combiner inside, should merge it', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { type: 'array', items: { oneOf: [ @@ -146,7 +146,7 @@ describe('Property component', () => { }); it('given an array with a mergeable combiner inside, should merge it', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { type: 'array', items: { oneOf: [ @@ -169,7 +169,7 @@ describe('Property component', () => { }); it('given an array with an allOf inside and enabled allOf merging, should display the name of properties', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { type: 'object', properties: { 'array-all-objects': { @@ -209,7 +209,7 @@ describe('Property component', () => { }); it('given a ref pointing at complex type, should display property name', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { properties: { foo: { $ref: '#/properties/bar', @@ -232,7 +232,7 @@ describe('Property component', () => { describe('properties titles', () => { it('given object type, should render title', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { title: 'User', type: 'object', properties: { @@ -249,7 +249,7 @@ describe('Property component', () => { }); it('given array type with non-array items, should render title', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { type: 'array', items: { title: 'User', @@ -269,7 +269,7 @@ describe('Property component', () => { }); it('given array with no items, should render title', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { type: 'array', title: 'User', }; @@ -281,7 +281,7 @@ describe('Property component', () => { }); it('given array with defined items, should not render title', () => { - const schema: JSONSchema4 = { + const schema: JSONSchema = { type: 'array', items: [ { diff --git a/src/components/shared/__tests__/utils.ts b/src/components/shared/__tests__/utils.ts index 657889a9..79580552 100644 --- a/src/components/shared/__tests__/utils.ts +++ b/src/components/shared/__tests__/utils.ts @@ -5,10 +5,11 @@ import { SchemaTree as JsonSchemaTree, SchemaTreeOptions, } from '@stoplight/json-schema-tree'; -import { JSONSchema4 } from 'json-schema'; import { isEqual } from 'lodash/fp'; -export function buildTree(schema: JSONSchema4, options: Partial = {}) { +import { JSONSchema } from '../../../types'; + +export function buildTree(schema: JSONSchema, options: Partial = {}) { const jsonSchemaTree = new JsonSchemaTree(schema, { mergeAllOf: true, ...options, diff --git a/src/types.ts b/src/types.ts index ccef917e..54a9cd35 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,5 @@ import type { ReferenceNode, SchemaNode } from '@stoplight/json-schema-tree'; +import { JSONSchema4, JSONSchema6, JSONSchema7 } from 'json-schema'; import * as React from 'react'; export type GoToRefHandler = (node: ReferenceNode) => void; @@ -11,3 +12,5 @@ export interface SchemaRowProps { export type RowAddonRenderer = (props: SchemaRowProps) => React.ReactNode; export type ViewMode = 'read' | 'write' | 'standalone'; + +export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7; diff --git a/yarn.lock b/yarn.lock index c99c32b6..d9c02909 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2365,14 +2365,15 @@ json-schema-compare "^0.2.2" lodash "^4.17.4" -"@stoplight/json-schema-tree@^1.1.3": - version "1.1.3" - resolved "https://registry.yarnpkg.com/@stoplight/json-schema-tree/-/json-schema-tree-1.1.3.tgz#c2aafa1b10c506e7ebec7a4904ff3bd325f7102f" - integrity sha512-n/mmhLEu4qrZO/UxQBYyPNid2v4Zq6fuzUK9pBMpNxQtxXmmge62lFWDnc3Q8nzXwRio0OPuMmZS9RO0Q0hmOg== +"@stoplight/json-schema-tree@^2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@stoplight/json-schema-tree/-/json-schema-tree-2.0.0.tgz#00d54cb6aa2a791c34be91fc30cc92e6d448258c" + integrity sha512-vnzcb0TC07xh89lAVGjBTJ2CWvCqmDJDIs3u+gvgvjDPY86CQ0Wl4D2Cmb0iuqd986aiDPc8vDQf1N0dSq5+9A== dependencies: - "@stoplight/json" "^3.10.0" + "@stoplight/json" "^3.12.0" "@stoplight/json-schema-merge-allof" "^0.7.5" "@stoplight/lifecycle" "^2.3.2" + "@types/json-schema" "^7.0.7" magic-error "^0.0.0" "@stoplight/json@^3.10.0", "@stoplight/json@^3.6": @@ -2386,6 +2387,17 @@ lodash "^4.17.15" safe-stable-stringify "^1.1" +"@stoplight/json@^3.12.0": + version "3.12.0" + resolved "https://registry.yarnpkg.com/@stoplight/json/-/json-3.12.0.tgz#26c8d32da78eac6a760ba2c9cca6ae717dc417b4" + integrity sha512-c0bvFOGICk8QWIat72Td2GG6Bdvq/6O2jQcDZ8rEjh56YOdC/YPn1S8ihKu3AntJCtvqC9eTfadWBqkNK9HAjw== + dependencies: + "@stoplight/ordered-object-literal" "^1.0.1" + "@stoplight/types" "^11.9.0" + jsonc-parser "~2.2.1" + lodash "^4.17.15" + safe-stable-stringify "^1.1" + "@stoplight/lifecycle@^2.3.2": version "2.3.2" resolved "https://registry.yarnpkg.com/@stoplight/lifecycle/-/lifecycle-2.3.2.tgz#d61dff9ba20648241432e2daaef547214dc8976e" @@ -3200,11 +3212,16 @@ jest-diff "^26.0.0" pretty-format "^26.0.0" -"@types/json-schema@^7.0.3", "@types/json-schema@^7.0.4", "@types/json-schema@^7.0.5", "@types/json-schema@^7.0.6": +"@types/json-schema@^7.0.3", "@types/json-schema@^7.0.4", "@types/json-schema@^7.0.5": version "7.0.6" resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.6.tgz#f4c7ec43e81b319a9815115031709f26987891f0" integrity sha512-3c+yGKvVP5Y9TYBEibGNR+kLtijnj7mYrXRg+WpFb2X9xm04g/DXYkfg4hmzJQosc9snFNUPkbYIhu+KAm6jJw== +"@types/json-schema@^7.0.7": + version "7.0.7" + resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.7.tgz#98a993516c859eb0d5c4c8f098317a9ea68db9ad" + integrity sha512-cxWFQVseBm6O9Gbw1IWb8r6OS4OhSt3hPZLkFApLjM8TEXROBuQGLAH2i2gZpcXdLBIrpXuTDhH7Vbm1iXmNGA== + "@types/json5@^0.0.29": version "0.0.29" resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" From 380e3dc51a480ba153692952386ac2bfeeb0d74f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Tue, 27 Apr 2021 22:33:17 +0200 Subject: [PATCH 2/2] revert: reduce diff --- src/__stories__/JsonSchemaViewer.tsx | 23 ++++++------- src/__tests__/index.spec.tsx | 25 ++++++++------- src/components/__tests__/SchemaRow.spec.tsx | 8 ++--- src/components/shared/ChildStack.tsx | 2 +- .../shared/__tests__/Format.spec.tsx | 4 +-- .../shared/__tests__/Property.spec.tsx | 32 +++++++++---------- src/components/shared/__tests__/utils.ts | 5 ++- 7 files changed, 50 insertions(+), 49 deletions(-) diff --git a/src/__stories__/JsonSchemaViewer.tsx b/src/__stories__/JsonSchemaViewer.tsx index bb64ca73..c586d6d7 100644 --- a/src/__stories__/JsonSchemaViewer.tsx +++ b/src/__stories__/JsonSchemaViewer.tsx @@ -2,9 +2,10 @@ import { Button, Flex, InvertTheme, subscribeTheme } from '@stoplight/mosaic'; import { action } from '@storybook/addon-actions'; import { boolean, number, object, select, withKnobs } from '@storybook/addon-knobs'; import { storiesOf } from '@storybook/react'; +import { JSONSchema4 } from 'json-schema'; import * as React from 'react'; -import { JSONSchema, JsonSchemaViewer, RowAddonRenderer } from '../'; +import { JsonSchemaViewer, RowAddonRenderer } from '../'; import { Wrapper } from './utils/Wrapper'; const allOfSchema = require('../__fixtures__/combiners/allOfs/base.json'); @@ -24,7 +25,7 @@ storiesOf('JsonSchemaViewer', module) .addDecorator(storyFn => {storyFn()}) .add('default', () => ( @@ -73,28 +74,28 @@ storiesOf('JsonSchemaViewer', module) )) .add('stress-test schema', () => ( )) .add('allOf-schema', () => ( )) .add('anyOf-array-schema', () => ( )) .add('anyOf-array-schema2', () => ( @@ -150,7 +151,7 @@ storiesOf('JsonSchemaViewer', module)
@@ -160,21 +161,21 @@ storiesOf('JsonSchemaViewer', module) }) .add('refs/normal', () => ( )) .add('refs/nullish', () => ( )) .add('refs/broken', () => ( diff --git a/src/__tests__/index.spec.tsx b/src/__tests__/index.spec.tsx index 0a36502d..6e8bb3c0 100644 --- a/src/__tests__/index.spec.tsx +++ b/src/__tests__/index.spec.tsx @@ -3,11 +3,12 @@ import 'jest-enzyme'; import { mount, ReactWrapper } from 'enzyme'; import * as fastGlob from 'fast-glob'; import * as fs from 'fs'; +import { JSONSchema4 } from 'json-schema'; import * as path from 'path'; import * as React from 'react'; import { JsonSchemaViewer } from '../components'; -import { JSONSchema, ViewMode } from '../types'; +import { ViewMode } from '../types'; import { dumpDom } from './utils/dumpDom'; describe('HTML Output', () => { @@ -23,7 +24,7 @@ describe('HTML Output', () => { }); describe.each(['anyOf', 'oneOf'])('given %s combiner placed next to allOf', combiner => { - let schema: JSONSchema; + let schema: JSONSchema4; beforeEach(() => { schema = { @@ -83,7 +84,7 @@ describe('HTML Output', () => { }); it('given array with oneOf containing items, should merge it correctly', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { oneOf: [ { items: { @@ -103,7 +104,7 @@ describe('HTML Output', () => { }); it.each(['standalone', 'read', 'write'])('given %s mode, should populate proper nodes', mode => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { type: ['string', 'object'], properties: { id: { @@ -123,7 +124,7 @@ describe('HTML Output', () => { }); it('given multiple object and string type, should process properties', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { type: ['string', 'object'], properties: { ids: { @@ -139,7 +140,7 @@ describe('HTML Output', () => { }); it('given complex type that includes array and complex array subtype, should not ignore subtype', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { type: 'object', properties: { items: { @@ -157,7 +158,7 @@ describe('HTML Output', () => { }); it('given visible $ref node, should try to inject the title immediately', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { type: 'object', properties: { foo: { @@ -201,7 +202,7 @@ describe('Expanded depth', () => { }); describe('merged array with object', () => { - let schema: JSONSchema; + let schema: JSONSchema4; beforeEach(() => { schema = { @@ -357,7 +358,7 @@ describe('Expanded depth', () => { }); describe('merged array with object #2', () => { - let schema: JSONSchema; + let schema: JSONSchema4; beforeEach(() => { schema = { @@ -577,7 +578,7 @@ describe('Expanded depth', () => { }); describe('nested object', () => { - let schema: JSONSchema; + let schema: JSONSchema4; beforeEach(() => { schema = { @@ -693,7 +694,7 @@ describe('Expanded depth', () => { describe('$ref resolving', () => { it('should render caret for schema with top-level $ref pointing at complex type', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { $ref: '#/definitions/foo', definitions: { foo: { @@ -724,7 +725,7 @@ describe('$ref resolving', () => { }); it('should render caret for top-level array with $ref items', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { type: 'array', items: { $ref: '#/foo', diff --git a/src/components/__tests__/SchemaRow.spec.tsx b/src/components/__tests__/SchemaRow.spec.tsx index d2cb2e76..2f818dff 100644 --- a/src/components/__tests__/SchemaRow.spec.tsx +++ b/src/components/__tests__/SchemaRow.spec.tsx @@ -3,9 +3,9 @@ import 'jest-enzyme'; import { RootNode } from '@stoplight/json-schema-tree'; import { Icon } from '@stoplight/mosaic'; import { mount } from 'enzyme'; +import { JSONSchema4 } from 'json-schema'; import * as React from 'react'; -import { JSONSchema } from '../../types'; import { SchemaRow } from '../SchemaRow'; import { buildTree, findNodeWithPath } from '../shared/__tests__/utils'; import { Properties } from '../shared/Properties'; @@ -13,7 +13,7 @@ import { Properties } from '../shared/Properties'; describe('SchemaRow component', () => { describe('resolving error', () => { let tree: RootNode; - let schema: JSONSchema; + let schema: JSONSchema4; beforeEach(() => { schema = { @@ -50,9 +50,9 @@ describe('SchemaRow component', () => { }); describe('required property', () => { - let schema: JSONSchema; + let schema: JSONSchema4; - function isRequired(schema: JSONSchema, nodePath: readonly string[], value: boolean) { + function isRequired(schema: JSONSchema4, nodePath: readonly string[], value: boolean) { const tree = buildTree(schema); const schemaNode = findNodeWithPath(tree, nodePath); diff --git a/src/components/shared/ChildStack.tsx b/src/components/shared/ChildStack.tsx index 76efab5c..643b1061 100644 --- a/src/components/shared/ChildStack.tsx +++ b/src/components/shared/ChildStack.tsx @@ -1,4 +1,4 @@ -import type { SchemaNode } from '@stoplight/json-schema-tree'; +import { SchemaNode } from '@stoplight/json-schema-tree'; import * as React from 'react'; import { NESTING_OFFSET } from '../../consts'; diff --git a/src/components/shared/__tests__/Format.spec.tsx b/src/components/shared/__tests__/Format.spec.tsx index c669f275..465e5a3b 100644 --- a/src/components/shared/__tests__/Format.spec.tsx +++ b/src/components/shared/__tests__/Format.spec.tsx @@ -1,15 +1,15 @@ import 'jest-enzyme'; import { mount } from 'enzyme'; +import { JSONSchema4 } from 'json-schema'; import * as React from 'react'; -import { JSONSchema } from '../../../types'; import { SchemaRow } from '../../SchemaRow'; import { Format } from '../Format'; import { buildTree, findNodeWithPath } from './utils'; describe('Format component', () => { - const schema: JSONSchema = require('../../../__fixtures__/formats-schema.json'); + const schema: JSONSchema4 = require('../../../__fixtures__/formats-schema.json'); let tree = buildTree(schema); it('should render next to a single type', () => { diff --git a/src/components/shared/__tests__/Property.spec.tsx b/src/components/shared/__tests__/Property.spec.tsx index b37b5e50..f3e4cf02 100644 --- a/src/components/shared/__tests__/Property.spec.tsx +++ b/src/components/shared/__tests__/Property.spec.tsx @@ -2,16 +2,16 @@ import 'jest-enzyme'; import { Provider as MosaicProvider } from '@stoplight/mosaic'; import { mount, ReactWrapper } from 'enzyme'; +import { JSONSchema4 } from 'json-schema'; import * as React from 'react'; import { SchemaRow, Types } from '../..'; -import { JSONSchema } from '../../../types'; import { buildTree, findNodeWithPath } from './utils'; describe('Property component', () => { const toUnmount: ReactWrapper[] = []; - function render(schema: JSONSchema, nodePath?: readonly string[]) { + function render(schema: JSONSchema4, nodePath?: readonly string[]) { const tree = buildTree(schema); const node = nodePath ? findNodeWithPath(tree, nodePath) : tree.children[0]; @@ -37,7 +37,7 @@ describe('Property component', () => { }); it('should render Types with proper type and subtype', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { type: 'array', items: { type: 'string', @@ -51,7 +51,7 @@ describe('Property component', () => { }); it('should handle nullish items', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { type: 'array', items: null as any, }; @@ -63,7 +63,7 @@ describe('Property component', () => { }); it('should handle nullish $ref', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { $ref: null as any, }; @@ -73,7 +73,7 @@ describe('Property component', () => { describe('properties names', () => { it('given an object, should display the names of its properties', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { properties: { foo: { type: 'string', @@ -88,7 +88,7 @@ describe('Property component', () => { }); it('given an object among other types, should still display its properties', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { type: ['string', 'object'], properties: { foo: { @@ -107,7 +107,7 @@ describe('Property component', () => { }); it('given an array of objects, should display names of those properties', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { type: 'array', items: { properties: { @@ -125,7 +125,7 @@ describe('Property component', () => { }); it('given an array with a combiner inside, should merge it', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { type: 'array', items: { oneOf: [ @@ -146,7 +146,7 @@ describe('Property component', () => { }); it('given an array with a mergeable combiner inside, should merge it', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { type: 'array', items: { oneOf: [ @@ -169,7 +169,7 @@ describe('Property component', () => { }); it('given an array with an allOf inside and enabled allOf merging, should display the name of properties', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { type: 'object', properties: { 'array-all-objects': { @@ -209,7 +209,7 @@ describe('Property component', () => { }); it('given a ref pointing at complex type, should display property name', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { properties: { foo: { $ref: '#/properties/bar', @@ -232,7 +232,7 @@ describe('Property component', () => { describe('properties titles', () => { it('given object type, should render title', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { title: 'User', type: 'object', properties: { @@ -249,7 +249,7 @@ describe('Property component', () => { }); it('given array type with non-array items, should render title', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { type: 'array', items: { title: 'User', @@ -269,7 +269,7 @@ describe('Property component', () => { }); it('given array with no items, should render title', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { type: 'array', title: 'User', }; @@ -281,7 +281,7 @@ describe('Property component', () => { }); it('given array with defined items, should not render title', () => { - const schema: JSONSchema = { + const schema: JSONSchema4 = { type: 'array', items: [ { diff --git a/src/components/shared/__tests__/utils.ts b/src/components/shared/__tests__/utils.ts index 79580552..657889a9 100644 --- a/src/components/shared/__tests__/utils.ts +++ b/src/components/shared/__tests__/utils.ts @@ -5,11 +5,10 @@ import { SchemaTree as JsonSchemaTree, SchemaTreeOptions, } from '@stoplight/json-schema-tree'; +import { JSONSchema4 } from 'json-schema'; import { isEqual } from 'lodash/fp'; -import { JSONSchema } from '../../../types'; - -export function buildTree(schema: JSONSchema, options: Partial = {}) { +export function buildTree(schema: JSONSchema4, options: Partial = {}) { const jsonSchemaTree = new JsonSchemaTree(schema, { mergeAllOf: true, ...options,