Skip to content

Commit

Permalink
feat: improve CustomizeType to render basic constant values
Browse files Browse the repository at this point in the history
  • Loading branch information
jy95 committed Jan 8, 2024
1 parent 27ebcc9 commit 3a40253
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2220,15 +2220,9 @@ exports[`JSONSchemaViewer - generateFriendlyName cases test Prefer title 1`] = `
text
</strong>
 
<span
style={
{
"opacity": "0.6",
}
}
>
string
</span>
<code>
Hello World
</code>
 
<strong>
constant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,9 @@ exports[`JSONSchemaViewer - Generic keywords test const 1`] = `
country
</strong>
 
<span
style={
{
"opacity": "0.6",
}
}
>
string
</span>
<code>
United States of America
</code>
 
<strong>
constant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,9 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else (multiple) 1`
country
</strong>
 
<span
style={
{
"opacity": "0.6",
}
}
>
string
</span>
<code>
United States of America
</code>
 
<strong>
constant
Expand Down Expand Up @@ -593,15 +587,9 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else (multiple) 1`
country
</strong>
 
<span
style={
{
"opacity": "0.6",
}
}
>
string
</span>
<code>
Canada
</code>
 
<strong>
required
Expand Down Expand Up @@ -860,15 +848,9 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else (multiple) 1`
country
</strong>
 
<span
style={
{
"opacity": "0.6",
}
}
>
string
</span>
<code>
Netherlands
</code>
 
<strong>
required
Expand Down Expand Up @@ -1265,15 +1247,9 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else 1`] = `
country
</strong>
 
<span
style={
{
"opacity": "0.6",
}
}
>
string
</span>
<code>
United States of America
</code>
 
<strong>
constant
Expand Down
11 changes: 11 additions & 0 deletions src/theme/JSONSchemaViewer/utils/generateFriendlyName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
JSONSchemaNS,
TypeValues,
} from "@theme/JSONSchemaViewer/types"
import { printSchemaType } from "@theme/JSONSchemaViewer/utils/QualifierMessages"

// common function I need below
function shouldAddSeparator(idx: number, length: number): boolean {
Expand Down Expand Up @@ -137,6 +138,16 @@ function CustomizeType({ schema, type }: CustomizeProps): JSX.Element {
return <CustomizeArray schema={schema as JSONSchemaNS.Array} />
}

// For constant values
if (!["array", "object"].includes(type)) {
if (schema.const !== undefined) {
return printSchemaType(schema.const)
}
if (schema.enum !== undefined && schema.enum.length === 1) {
return printSchemaType(schema.enum[0])
}
}

// By default, render the type as it
return <TypeLabelSwitch type={type} />
}
Expand Down

0 comments on commit 3a40253

Please sign in to comment.