Skip to content

Commit a22e20f

Browse files
author
Jakub Jankowski
authored
feat: hide examples option (#134)
1 parent b7d90bb commit a22e20f

File tree

6 files changed

+35
-6
lines changed

6 files changed

+35
-6
lines changed

src/__stories__/JsonSchemaViewer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Button, Flex, InvertTheme, subscribeTheme } from '@stoplight/mosaic';
22
import { action } from '@storybook/addon-actions';
3-
import { number, object, select, withKnobs } from '@storybook/addon-knobs';
3+
import { boolean, number, object, select, withKnobs } from '@storybook/addon-knobs';
44
import { storiesOf } from '@storybook/react';
55
import { JSONSchema4 } from 'json-schema';
66
import * as React from 'react';
@@ -35,6 +35,7 @@ storiesOf('JsonSchemaViewer', module)
3535
},
3636
'standalone',
3737
)}
38+
hideExamples={boolean('hideExamples', false)}
3839
onGoToRef={action('onGoToRef')}
3940
/>
4041
))

src/components/JsonSchemaViewer.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const JsonSchemaViewerComponent: React.FC<JsonSchemaProps & ErrorBoundaryForward
2424
defaultExpandedDepth = 2,
2525
onGoToRef,
2626
renderRowAddon,
27+
hideExamples,
2728
}) => {
2829
const jsonSchemaTreeRoot = React.useMemo(() => {
2930
const jsonSchemaTree = new JsonSchemaTree(schema, {
@@ -49,11 +50,12 @@ const JsonSchemaViewerComponent: React.FC<JsonSchemaProps & ErrorBoundaryForward
4950
jsonSchemaTreeRoot,
5051
]);
5152

52-
const options = React.useMemo(() => ({ defaultExpandedDepth, viewMode, onGoToRef, renderRowAddon }), [
53+
const options = React.useMemo(() => ({ defaultExpandedDepth, viewMode, onGoToRef, renderRowAddon, hideExamples }), [
5354
defaultExpandedDepth,
5455
viewMode,
5556
onGoToRef,
5657
renderRowAddon,
58+
hideExamples,
5759
]);
5860

5961
if (isEmpty) {

src/components/SchemaRow/SchemaRow.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface SchemaRowProps {
2929
export const SchemaRow: React.FunctionComponent<SchemaRowProps> = ({ schemaNode, nestingLevel }) => {
3030
const description = isRegularNode(schemaNode) ? schemaNode.annotations.description : null;
3131

32-
const { defaultExpandedDepth, renderRowAddon, onGoToRef } = useJSVOptionsContext();
32+
const { defaultExpandedDepth, renderRowAddon, onGoToRef, hideExamples } = useJSVOptionsContext();
3333

3434
const [isExpanded, setExpanded] = React.useState<boolean>(
3535
!isMirroredNode(schemaNode) && nestingLevel <= defaultExpandedDepth,
@@ -148,7 +148,10 @@ export const SchemaRow: React.FunctionComponent<SchemaRowProps> = ({ schemaNode,
148148
)}
149149
</div>
150150

151-
<Validations validations={isRegularNode(schemaNode) ? getValidationsFromSchema(schemaNode) : {}} />
151+
<Validations
152+
validations={isRegularNode(schemaNode) ? getValidationsFromSchema(schemaNode) : {}}
153+
hideExamples={hideExamples}
154+
/>
152155

153156
{isBrokenRef && (
154157
// TODO (JJ): Add mosaic tooltip showing ref error

src/components/shared/Validations.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as React from 'react';
66

77
export interface IValidations {
88
validations: Dictionary<unknown>;
9+
hideExamples?: boolean;
910
}
1011

1112
type ValidationFormat = {
@@ -24,6 +25,8 @@ export const numberValidationNames = [
2425
'exclusiveMaximum',
2526
];
2627

28+
const exampleValidationNames = ['examples', 'example', 'x-example'];
29+
2730
const excludedValidations = ['exclusiveMinimum', 'exclusiveMaximum', 'readOnly', 'writeOnly'];
2831

2932
const numberValidationFormatters: Record<string, (value: unknown) => string> = {
@@ -101,7 +104,7 @@ function filterOutOasFormatValidations(format: string, values: Dictionary<unknow
101104
return newValues;
102105
}
103106

104-
export const Validations: React.FunctionComponent<IValidations> = ({ validations }) => {
107+
export const Validations: React.FunctionComponent<IValidations> = ({ validations, hideExamples }) => {
105108
const numberValidations = pick(validations, numberValidationNames);
106109
const booleanValidations = omit(
107110
pickBy(validations, v => ['true', 'false'].includes(String(v))),
@@ -111,6 +114,7 @@ export const Validations: React.FunctionComponent<IValidations> = ({ validations
111114
...keys(numberValidations),
112115
...keys(booleanValidations),
113116
...excludedValidations,
117+
...(hideExamples ? exampleValidationNames : []),
114118
]);
115119

116120
return (

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ describe('Validations component', () => {
4848
expect(wrapper).toIncludeText('Allowed value:"bar"');
4949
});
5050

51+
it('should not render hidden example validations', () => {
52+
const node = new RegularNode({
53+
type: 'number',
54+
example: 42,
55+
examples: [4, 2],
56+
});
57+
58+
const validations = getValidationsFromSchema(node);
59+
const wrapper = mount(<Validations validations={validations} hideExamples />);
60+
61+
expect(wrapper).not.toIncludeText('Example value:42');
62+
expect(wrapper).not.toIncludeText('Example values:42');
63+
});
64+
5165
describe('OAS formats', () => {
5266
it('given default range, should not render any validation', () => {
5367
const node = new RegularNode({

src/contexts/jsvOptions.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ export type JSVOptions = {
77
viewMode: ViewMode;
88
onGoToRef?: GoToRefHandler;
99
renderRowAddon?: RowAddonRenderer;
10+
hideExamples?: boolean;
1011
};
1112

12-
const JSVOptionsContext = React.createContext<JSVOptions>({ defaultExpandedDepth: 0, viewMode: 'standalone' });
13+
const JSVOptionsContext = React.createContext<JSVOptions>({
14+
defaultExpandedDepth: 0,
15+
viewMode: 'standalone',
16+
hideExamples: false,
17+
});
1318

1419
export const useJSVOptionsContext = () => React.useContext(JSVOptionsContext);
1520

0 commit comments

Comments
 (0)