Skip to content

Commit

Permalink
feat: hide examples option (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
mallachari committed Apr 14, 2021
1 parent b7d90bb commit a22e20f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/__stories__/JsonSchemaViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button, Flex, InvertTheme, subscribeTheme } from '@stoplight/mosaic';
import { action } from '@storybook/addon-actions';
import { number, object, select, withKnobs } from '@storybook/addon-knobs';
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';
Expand Down Expand Up @@ -35,6 +35,7 @@ storiesOf('JsonSchemaViewer', module)
},
'standalone',
)}
hideExamples={boolean('hideExamples', false)}
onGoToRef={action('onGoToRef')}
/>
))
Expand Down
4 changes: 3 additions & 1 deletion src/components/JsonSchemaViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const JsonSchemaViewerComponent: React.FC<JsonSchemaProps & ErrorBoundaryForward
defaultExpandedDepth = 2,
onGoToRef,
renderRowAddon,
hideExamples,
}) => {
const jsonSchemaTreeRoot = React.useMemo(() => {
const jsonSchemaTree = new JsonSchemaTree(schema, {
Expand All @@ -49,11 +50,12 @@ const JsonSchemaViewerComponent: React.FC<JsonSchemaProps & ErrorBoundaryForward
jsonSchemaTreeRoot,
]);

const options = React.useMemo(() => ({ defaultExpandedDepth, viewMode, onGoToRef, renderRowAddon }), [
const options = React.useMemo(() => ({ defaultExpandedDepth, viewMode, onGoToRef, renderRowAddon, hideExamples }), [
defaultExpandedDepth,
viewMode,
onGoToRef,
renderRowAddon,
hideExamples,
]);

if (isEmpty) {
Expand Down
7 changes: 5 additions & 2 deletions src/components/SchemaRow/SchemaRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface SchemaRowProps {
export const SchemaRow: React.FunctionComponent<SchemaRowProps> = ({ schemaNode, nestingLevel }) => {
const description = isRegularNode(schemaNode) ? schemaNode.annotations.description : null;

const { defaultExpandedDepth, renderRowAddon, onGoToRef } = useJSVOptionsContext();
const { defaultExpandedDepth, renderRowAddon, onGoToRef, hideExamples } = useJSVOptionsContext();

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

<Validations validations={isRegularNode(schemaNode) ? getValidationsFromSchema(schemaNode) : {}} />
<Validations
validations={isRegularNode(schemaNode) ? getValidationsFromSchema(schemaNode) : {}}
hideExamples={hideExamples}
/>

{isBrokenRef && (
// TODO (JJ): Add mosaic tooltip showing ref error
Expand Down
6 changes: 5 additions & 1 deletion src/components/shared/Validations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as React from 'react';

export interface IValidations {
validations: Dictionary<unknown>;
hideExamples?: boolean;
}

type ValidationFormat = {
Expand All @@ -24,6 +25,8 @@ export const numberValidationNames = [
'exclusiveMaximum',
];

const exampleValidationNames = ['examples', 'example', 'x-example'];

const excludedValidations = ['exclusiveMinimum', 'exclusiveMaximum', 'readOnly', 'writeOnly'];

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

export const Validations: React.FunctionComponent<IValidations> = ({ validations }) => {
export const Validations: React.FunctionComponent<IValidations> = ({ validations, hideExamples }) => {
const numberValidations = pick(validations, numberValidationNames);
const booleanValidations = omit(
pickBy(validations, v => ['true', 'false'].includes(String(v))),
Expand All @@ -111,6 +114,7 @@ export const Validations: React.FunctionComponent<IValidations> = ({ validations
...keys(numberValidations),
...keys(booleanValidations),
...excludedValidations,
...(hideExamples ? exampleValidationNames : []),
]);

return (
Expand Down
14 changes: 14 additions & 0 deletions src/components/shared/__tests__/Validations.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ describe('Validations component', () => {
expect(wrapper).toIncludeText('Allowed value:"bar"');
});

it('should not render hidden example validations', () => {
const node = new RegularNode({
type: 'number',
example: 42,
examples: [4, 2],
});

const validations = getValidationsFromSchema(node);
const wrapper = mount(<Validations validations={validations} hideExamples />);

expect(wrapper).not.toIncludeText('Example value:42');
expect(wrapper).not.toIncludeText('Example values:42');
});

describe('OAS formats', () => {
it('given default range, should not render any validation', () => {
const node = new RegularNode({
Expand Down
7 changes: 6 additions & 1 deletion src/contexts/jsvOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ export type JSVOptions = {
viewMode: ViewMode;
onGoToRef?: GoToRefHandler;
renderRowAddon?: RowAddonRenderer;
hideExamples?: boolean;
};

const JSVOptionsContext = React.createContext<JSVOptions>({ defaultExpandedDepth: 0, viewMode: 'standalone' });
const JSVOptionsContext = React.createContext<JSVOptions>({
defaultExpandedDepth: 0,
viewMode: 'standalone',
hideExamples: false,
});

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

Expand Down

0 comments on commit a22e20f

Please sign in to comment.