Skip to content

Commit

Permalink
Merge pull request #398 from jy95/fix-empty
Browse files Browse the repository at this point in the history
fix: tab crash with empty examples array
  • Loading branch information
jy95 authored Jul 10, 2024
2 parents 01b8f70 + d55481f commit ed39bdc
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
5 changes: 1 addition & 4 deletions __tests__/JSONSchemaViewer/constructor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,7 @@ describe("JSONSchemaViewer - constructor", () => {
let root: ReactTestRenderer | undefined
await act(async () => {
root = create(
<JSONSchemaViewer
schema={fakeSchema}
className="jsv-custom"
/>,
<JSONSchemaViewer schema={fakeSchema} className="jsv-custom" />,
)
})

Expand Down
6 changes: 3 additions & 3 deletions src/theme/JSONSchemaViewer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type Props = {
/**
* To customize the styles of the viewer, to override docusaurus styles on a specific page
*/
className?: string;
className?: string
}

type InnerViewerProperties = {
Expand All @@ -43,7 +43,7 @@ type InnerViewerProperties = {
// Options for viewer
viewerOptions?: Omit<JSVOptions, "fullSchema">
// To customize the styles of the viewer, to override docusaurus styles on a specific page
className?: string;
className?: string
}

// Translated labels
Expand Down Expand Up @@ -85,7 +85,7 @@ function JSONSchemaInnerViewer(props: InnerViewerProperties): JSX.Element {
summary={<strong>{title}</strong>}
detailsProps={{
open: true,
className: props.className || "json-schema-viewer"
className: props.className || "json-schema-viewer",
}}
>
<CreateNodes schema={schema} />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from "react"

import * as QMS from "@theme/JSONSchemaViewer/utils/QualifierMessages"
import { hasUnresolvedRefs, isArrayNotEmpty } from "../detectTypes"

import type { JSONSchema, JSONSchemaNS } from "@theme/JSONSchemaViewer/types"
import type { JSVOptions } from "@theme/JSONSchemaViewer/contexts"
import { hasUnresolvedRefs } from "../detectTypes"

type Props = {
schema: Exclude<JSONSchema, true | false>
Expand Down Expand Up @@ -64,7 +64,7 @@ const CHECKS_MAP: Record<CheckKey, CheckInfo> = {
Component: () => <QMS.WriteOnlyQM key={"writeOnly"} />,
},
enum: {
match: ({ schema }) => schema.enum !== undefined,
match: ({ schema }) => isArrayNotEmpty(schema.enum),
Component: ({ schema }) => <QMS.EnumQM key={"enum"} schema={schema} />,
},
stringLength: {
Expand Down Expand Up @@ -148,7 +148,7 @@ const CHECKS_MAP: Record<CheckKey, CheckInfo> = {
},
examples: {
match: ({ schema, options }) =>
options.showExamples === true && schema.examples !== undefined,
options.showExamples === true && isArrayNotEmpty(schema.examples),
Component: ({ schema }) => (
<QMS.ExamplesQM key={"examples"} schema={schema} />
),
Expand Down
4 changes: 4 additions & 0 deletions src/theme/JSONSchemaViewer/utils/detectTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,7 @@ export function detectedTypes(
// If array is empty, it could mean that it is either "any" or "nothing"
return [...foundUndeclaredTypes(schema)]
}

// Not empty array ?
export const isArrayNotEmpty = (arr: any[] | readonly any[] | undefined) =>
arr !== undefined && arr.length > 0
6 changes: 3 additions & 3 deletions testsite/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ module.exports = {
projectName: "docusaurus-json-schema-plugin", // Usually your repo name.
themeConfig: {
algolia: {
appId: 'IQ028YCDJT',
apiKey: '8bd1d98fae984449f5cc44eed58ddb2d',
indexName: "jy95io"
appId: "IQ028YCDJT",
apiKey: "8bd1d98fae984449f5cc44eed58ddb2d",
indexName: "jy95io",
},
navbar: {
title: "JSON Schema viewer / editor",
Expand Down

0 comments on commit ed39bdc

Please sign in to comment.