Skip to content

Commit f65812a

Browse files
committed
feat: accept JSON Schema Draft 6 & Draft 7
1 parent b7d90bb commit f65812a

File tree

11 files changed

+58
-61
lines changed

11 files changed

+58
-61
lines changed

src/__stories__/JsonSchemaViewer.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import { Button, Flex, InvertTheme, subscribeTheme } from '@stoplight/mosaic';
22
import { action } from '@storybook/addon-actions';
33
import { number, object, select, withKnobs } from '@storybook/addon-knobs';
44
import { storiesOf } from '@storybook/react';
5-
import { JSONSchema4 } from 'json-schema';
65
import * as React from 'react';
76

8-
import { JsonSchemaViewer, RowAddonRenderer } from '../';
7+
import { JSONSchema, JsonSchemaViewer, RowAddonRenderer } from '../';
98
import { Wrapper } from './utils/Wrapper';
109

1110
const allOfSchema = require('../__fixtures__/combiners/allOfs/base.json');
@@ -24,7 +23,7 @@ storiesOf('JsonSchemaViewer', module)
2423
.addDecorator(storyFn => <Wrapper>{storyFn()}</Wrapper>)
2524
.add('default', () => (
2625
<JsonSchemaViewer
27-
schema={schema as JSONSchema4}
26+
schema={schema as JSONSchema}
2827
defaultExpandedDepth={number('defaultExpandedDepth', 0)}
2928
viewMode={select(
3029
'viewMode',
@@ -57,36 +56,36 @@ storiesOf('JsonSchemaViewer', module)
5756

5857
return (
5958
<JsonSchemaViewer
60-
schema={object('schema', schema as JSONSchema4)}
59+
schema={object('schema', schema as JSONSchema)}
6160
onGoToRef={action('onGoToRef')}
6261
renderRowAddon={customRowAddonRenderer}
6362
/>
6463
);
6564
})
6665
.add('stress-test schema', () => (
6766
<JsonSchemaViewer
68-
schema={stressSchema as JSONSchema4}
67+
schema={stressSchema as JSONSchema}
6968
defaultExpandedDepth={number('defaultExpandedDepth', 2)}
7069
onGoToRef={action('onGoToRef')}
7170
/>
7271
))
7372
.add('allOf-schema', () => (
7473
<JsonSchemaViewer
75-
schema={allOfSchema as JSONSchema4}
74+
schema={allOfSchema as JSONSchema}
7675
defaultExpandedDepth={number('defaultExpandedDepth', 2)}
7776
onGoToRef={action('onGoToRef')}
7877
/>
7978
))
8079
.add('anyOf-array-schema', () => (
8180
<JsonSchemaViewer
82-
schema={oneOfWithArraySchema as JSONSchema4}
81+
schema={oneOfWithArraySchema as JSONSchema}
8382
defaultExpandedDepth={number('defaultExpandedDepth', 2)}
8483
onGoToRef={action('onGoToRef')}
8584
/>
8685
))
8786
.add('anyOf-array-schema2', () => (
8887
<JsonSchemaViewer
89-
schema={oneOfWithArraySchema2 as JSONSchema4}
88+
schema={oneOfWithArraySchema2 as JSONSchema}
9089
defaultExpandedDepth={number('defaultExpandedDepth', 2)}
9190
onGoToRef={action('onGoToRef')}
9291
/>
@@ -142,7 +141,7 @@ storiesOf('JsonSchemaViewer', module)
142141
<InvertTheme>
143142
<div style={{ height: '100vh' }}>
144143
<JsonSchemaViewer
145-
schema={schema as JSONSchema4}
144+
schema={schema as JSONSchema}
146145
defaultExpandedDepth={number('defaultExpandedDepth', 2)}
147146
onGoToRef={action('onGoToRef')}
148147
/>
@@ -152,21 +151,21 @@ storiesOf('JsonSchemaViewer', module)
152151
})
153152
.add('refs/normal', () => (
154153
<JsonSchemaViewer
155-
schema={refSchema as JSONSchema4}
154+
schema={refSchema as JSONSchema}
156155
defaultExpandedDepth={number('defaultExpandedDepth', 2)}
157156
onGoToRef={action('onGoToRef')}
158157
/>
159158
))
160159
.add('refs/nullish', () => (
161160
<JsonSchemaViewer
162-
schema={nullRefSchema as JSONSchema4}
161+
schema={nullRefSchema as JSONSchema}
163162
defaultExpandedDepth={number('defaultExpandedDepth', 2)}
164163
onGoToRef={action('onGoToRef')}
165164
/>
166165
))
167166
.add('refs/broken', () => (
168167
<JsonSchemaViewer
169-
schema={brokenRefArraySchema as JSONSchema4}
168+
schema={brokenRefArraySchema as JSONSchema}
170169
defaultExpandedDepth={number('defaultExpandedDepth', 2)}
171170
onGoToRef={action('onGoToRef')}
172171
/>

src/__tests__/__snapshots__/index.spec.tsx.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,15 +2669,15 @@ exports[`HTML Output should match default-schema.json 1`] = `
26692669
</div>
26702670
</div>
26712671
</div>
2672+
<div>
2673+
<span>Allowed value:</span>
2674+
<span style=\\"background-color: rgb(237, 242, 247)\\">\\"Constant name\\"</span>
2675+
</div>
26722676
<div>
26732677
<span>Example values:</span>
26742678
<span style=\\"background-color: rgb(237, 242, 247)\\">\\"Example name\\"</span>
26752679
<span style=\\"background-color: rgb(237, 242, 247)\\">\\"Different name\\"</span>
26762680
</div>
2677-
<div>
2678-
<span>Allowed value:</span>
2679-
<span style=\\"background-color: rgb(237, 242, 247)\\">\\"Constant name\\"</span>
2680-
</div>
26812681
</div>
26822682
<div></div>
26832683
</div>

src/__tests__/index.spec.tsx

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import 'jest-enzyme';
33
import { mount, ReactWrapper } from 'enzyme';
44
import * as fastGlob from 'fast-glob';
55
import * as fs from 'fs';
6-
import { JSONSchema4 } from 'json-schema';
76
import * as path from 'path';
87
import * as React from 'react';
98

109
import { JsonSchemaViewer } from '../components';
11-
import { ViewMode } from '../types';
10+
import { JSONSchema, ViewMode } from '../types';
1211
import { dumpDom } from './utils/dumpDom';
1312

1413
describe('HTML Output', () => {
@@ -24,7 +23,7 @@ describe('HTML Output', () => {
2423
});
2524

2625
describe.each(['anyOf', 'oneOf'])('given %s combiner placed next to allOf', combiner => {
27-
let schema: JSONSchema4;
26+
let schema: JSONSchema;
2827

2928
beforeEach(() => {
3029
schema = {
@@ -84,7 +83,7 @@ describe('HTML Output', () => {
8483
});
8584

8685
it('given array with oneOf containing items, should merge it correctly', () => {
87-
const schema: JSONSchema4 = {
86+
const schema: JSONSchema = {
8887
oneOf: [
8988
{
9089
items: {
@@ -104,7 +103,7 @@ describe('HTML Output', () => {
104103
});
105104

106105
it.each<ViewMode>(['standalone', 'read', 'write'])('given %s mode, should populate proper nodes', mode => {
107-
const schema: JSONSchema4 = {
106+
const schema: JSONSchema = {
108107
type: ['string', 'object'],
109108
properties: {
110109
id: {
@@ -124,7 +123,7 @@ describe('HTML Output', () => {
124123
});
125124

126125
it('given multiple object and string type, should process properties', () => {
127-
const schema: JSONSchema4 = {
126+
const schema: JSONSchema = {
128127
type: ['string', 'object'],
129128
properties: {
130129
ids: {
@@ -140,7 +139,7 @@ describe('HTML Output', () => {
140139
});
141140

142141
it('given complex type that includes array and complex array subtype, should not ignore subtype', () => {
143-
const schema: JSONSchema4 = {
142+
const schema: JSONSchema = {
144143
type: 'object',
145144
properties: {
146145
items: {
@@ -158,7 +157,7 @@ describe('HTML Output', () => {
158157
});
159158

160159
it('given visible $ref node, should try to inject the title immediately', () => {
161-
const schema: JSONSchema4 = {
160+
const schema: JSONSchema = {
162161
type: 'object',
163162
properties: {
164163
foo: {
@@ -202,7 +201,7 @@ describe('Expanded depth', () => {
202201
});
203202

204203
describe('merged array with object', () => {
205-
let schema: JSONSchema4;
204+
let schema: JSONSchema;
206205

207206
beforeEach(() => {
208207
schema = {
@@ -364,7 +363,7 @@ describe('Expanded depth', () => {
364363
});
365364

366365
describe('merged array with object #2', () => {
367-
let schema: JSONSchema4;
366+
let schema: JSONSchema;
368367

369368
beforeEach(() => {
370369
schema = {
@@ -574,7 +573,7 @@ describe('Expanded depth', () => {
574573
});
575574

576575
describe('nested object', () => {
577-
let schema: JSONSchema4;
576+
let schema: JSONSchema;
578577

579578
beforeEach(() => {
580579
schema = {
@@ -666,7 +665,7 @@ describe('Expanded depth', () => {
666665

667666
describe('$ref resolving', () => {
668667
it('should render caret for schema with top-level $ref pointing at complex type', () => {
669-
const schema: JSONSchema4 = {
668+
const schema: JSONSchema = {
670669
$ref: '#/definitions/foo',
671670
definitions: {
672671
foo: {
@@ -697,7 +696,7 @@ describe('$ref resolving', () => {
697696
});
698697

699698
it('should render caret for top-level array with $ref items', () => {
700-
const schema: JSONSchema4 = {
699+
const schema: JSONSchema = {
701700
type: 'array',
702701
items: {
703702
$ref: '#/foo',

src/components/JsonSchemaViewer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { isRegularNode, SchemaTree as JsonSchemaTree, SchemaTreeRefDereferenceFn
22
import { Box, Provider as MosaicProvider } from '@stoplight/mosaic';
33
import { ErrorBoundaryForwardedProps, FallbackProps, withErrorBoundary } from '@stoplight/react-error-boundary';
44
import cn from 'classnames';
5-
import type { JSONSchema4 } from 'json-schema';
65
import * as React from 'react';
76

87
import { JSVOptions, JSVOptionsContextProvider } from '../contexts';
8+
import type { JSONSchema } from '../types';
99
import { ChildStack } from './shared/ChildStack';
1010

1111
export type JsonSchemaProps = Partial<JSVOptions> & {
12-
schema: JSONSchema4;
12+
schema: JSONSchema;
1313
emptyText?: string;
1414
className?: string;
1515
resolveRef?: SchemaTreeRefDereferenceFn;

src/components/__tests__/SchemaRow.spec.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import 'jest-enzyme';
33
import { RootNode } from '@stoplight/json-schema-tree';
44
import { Icon } from '@stoplight/mosaic';
55
import { mount } from 'enzyme';
6-
import { JSONSchema4 } from 'json-schema';
76
import * as React from 'react';
87

8+
import { JSONSchema } from '../../types';
99
import { SchemaRow } from '../SchemaRow';
1010
import { buildTree, findNodeWithPath } from '../shared/__tests__/utils';
1111
import { Properties } from '../shared/Properties';
1212

1313
describe('SchemaRow component', () => {
1414
describe('resolving error', () => {
1515
let tree: RootNode;
16-
let schema: JSONSchema4;
16+
let schema: JSONSchema;
1717

1818
beforeEach(() => {
1919
schema = {
@@ -50,9 +50,9 @@ describe('SchemaRow component', () => {
5050
});
5151

5252
describe('required property', () => {
53-
let schema: JSONSchema4;
53+
let schema: JSONSchema;
5454

55-
function isRequired(schema: JSONSchema4, nodePath: readonly string[], value: boolean) {
55+
function isRequired(schema: JSONSchema, nodePath: readonly string[], value: boolean) {
5656
const tree = buildTree(schema);
5757

5858
const schemaNode = findNodeWithPath(tree, nodePath);

src/components/shared/ChildStack.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SchemaNode } from '@stoplight/json-schema-tree';
1+
import type { SchemaNode } from '@stoplight/json-schema-tree';
22
import * as React from 'react';
33

44
import { SchemaRow } from '../SchemaRow';

src/components/shared/Validations.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,8 @@ const createValidationsFormatter = (name: string, options?: { exact?: boolean; n
5555
};
5656

5757
const validationFormatters: Record<string, (value: unknown) => ValidationFormat | null> = {
58-
['const']: createValidationsFormatter('Allowed'),
5958
enum: createValidationsFormatter('Allowed'),
6059
examples: createValidationsFormatter('Example'),
61-
example: createValidationsFormatter('Example'),
62-
['x-example']: createValidationsFormatter('Example'),
6360
multipleOf: createValidationsFormatter('Multiple of', { exact: true }),
6461
pattern: createValidationsFormatter('Match pattern', { exact: true, nowrap: true }),
6562
default: createValidationsFormatter('Default'),
@@ -204,8 +201,6 @@ export function getValidationsFromSchema(schemaNode: RegularNode) {
204201
? {
205202
...(schemaNode.annotations.default ? { default: schemaNode.annotations.default } : null),
206203
...(schemaNode.annotations.examples ? { examples: schemaNode.annotations.examples } : null),
207-
...(schemaNode.annotations.const ? { const: schemaNode.annotations.const } : null),
208-
...(schemaNode.annotations['x-example'] ? { ['x-example']: schemaNode.annotations['x-example'] } : null),
209204
}
210205
: null),
211206
...getFilteredValidations(schemaNode),

src/components/shared/__tests__/Format.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import 'jest-enzyme';
22

33
import { mount } from 'enzyme';
4-
import { JSONSchema4 } from 'json-schema';
54
import * as React from 'react';
65

6+
import { JSONSchema } from '../../../types';
77
import { SchemaRow } from '../../SchemaRow';
88
import { Format } from '../Format';
99
import { buildTree, findNodeWithPath } from './utils';
1010

1111
describe('Format component', () => {
12-
const schema: JSONSchema4 = require('../../../__fixtures__/formats-schema.json');
12+
const schema: JSONSchema = require('../../../__fixtures__/formats-schema.json');
1313
let tree = buildTree(schema);
1414

1515
it('should render next to a single type', () => {

0 commit comments

Comments
 (0)