Skip to content

Commit 3a40253

Browse files
committed
feat: improve CustomizeType to render basic constant values
1 parent 27ebcc9 commit 3a40253

File tree

4 files changed

+29
-54
lines changed

4 files changed

+29
-54
lines changed

__tests__/JSONSchemaViewer/__snapshots__/generateFriendlyName.test.tsx.snap

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,15 +2220,9 @@ exports[`JSONSchemaViewer - generateFriendlyName cases test Prefer title 1`] = `
22202220
text
22212221
</strong>
22222222
 
2223-
<span
2224-
style={
2225-
{
2226-
"opacity": "0.6",
2227-
}
2228-
}
2229-
>
2230-
string
2231-
</span>
2223+
<code>
2224+
Hello World
2225+
</code>
22322226
 
22332227
<strong>
22342228
constant

__tests__/JSONSchemaViewer/__snapshots__/generic_keywords.test.tsx.snap

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,9 @@ exports[`JSONSchemaViewer - Generic keywords test const 1`] = `
165165
country
166166
</strong>
167167
 
168-
<span
169-
style={
170-
{
171-
"opacity": "0.6",
172-
}
173-
}
174-
>
175-
string
176-
</span>
168+
<code>
169+
United States of America
170+
</code>
177171
 
178172
<strong>
179173
constant

__tests__/JSONSchemaViewer/__snapshots__/schema_conditionally.test.tsx.snap

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -330,15 +330,9 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else (multiple) 1`
330330
country
331331
</strong>
332332
 
333-
<span
334-
style={
335-
{
336-
"opacity": "0.6",
337-
}
338-
}
339-
>
340-
string
341-
</span>
333+
<code>
334+
United States of America
335+
</code>
342336
 
343337
<strong>
344338
constant
@@ -593,15 +587,9 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else (multiple) 1`
593587
country
594588
</strong>
595589
 
596-
<span
597-
style={
598-
{
599-
"opacity": "0.6",
600-
}
601-
}
602-
>
603-
string
604-
</span>
590+
<code>
591+
Canada
592+
</code>
605593
 
606594
<strong>
607595
required
@@ -860,15 +848,9 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else (multiple) 1`
860848
country
861849
</strong>
862850
 
863-
<span
864-
style={
865-
{
866-
"opacity": "0.6",
867-
}
868-
}
869-
>
870-
string
871-
</span>
851+
<code>
852+
Netherlands
853+
</code>
872854
 
873855
<strong>
874856
required
@@ -1265,15 +1247,9 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else 1`] = `
12651247
country
12661248
</strong>
12671249
 
1268-
<span
1269-
style={
1270-
{
1271-
"opacity": "0.6",
1272-
}
1273-
}
1274-
>
1275-
string
1276-
</span>
1250+
<code>
1251+
United States of America
1252+
</code>
12771253
 
12781254
<strong>
12791255
constant

src/theme/JSONSchemaViewer/utils/generateFriendlyName.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
JSONSchemaNS,
1818
TypeValues,
1919
} from "@theme/JSONSchemaViewer/types"
20+
import { printSchemaType } from "@theme/JSONSchemaViewer/utils/QualifierMessages"
2021

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

141+
// For constant values
142+
if (!["array", "object"].includes(type)) {
143+
if (schema.const !== undefined) {
144+
return printSchemaType(schema.const)
145+
}
146+
if (schema.enum !== undefined && schema.enum.length === 1) {
147+
return printSchemaType(schema.enum[0])
148+
}
149+
}
150+
140151
// By default, render the type as it
141152
return <TypeLabelSwitch type={type} />
142153
}

0 commit comments

Comments
 (0)