Skip to content

Commit 100faab

Browse files
authored
Merge pull request #242 from jy95/improve-const-view
feat: display constant values more prominently
2 parents cfe0722 + 838172d commit 100faab

File tree

11 files changed

+206
-55
lines changed

11 files changed

+206
-55
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,10 @@ exports[`JSONSchemaViewer - constructor Overwrite default ValueComponent value 1
435435
>
436436
object
437437
</span>
438+
 
439+
<strong>
440+
constant
441+
</strong>
438442
</summary>
439443
<div>
440444
<div>

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,15 +2220,13 @@ 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>
2226+
 
2227+
<strong>
2228+
constant
2229+
</strong>
22322230
</summary>
22332231
<div>
22342232
<div>

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

Lines changed: 114 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ exports[`JSONSchemaViewer - Generic keywords test annotations 1`] = `
128128
</details>
129129
`;
130130

131-
exports[`JSONSchemaViewer - Generic keywords test const 1`] = `
131+
exports[`JSONSchemaViewer - Generic keywords test const (as single enum value) 1`] = `
132132
<details
133133
open={true}
134134
>
@@ -165,15 +165,120 @@ exports[`JSONSchemaViewer - Generic keywords test const 1`] = `
165165
country
166166
</strong>
167167
 
168-
<span
169-
style={
170-
{
171-
"opacity": "0.6",
168+
<code>
169+
"US"
170+
</code>
171+
 
172+
<strong>
173+
constant
174+
</strong>
175+
</summary>
176+
<div>
177+
<div>
178+
<strong>
179+
type
180+
</strong>
181+
 : 
182+
<span
183+
style={
184+
{
185+
"opacity": "0.6",
186+
}
172187
}
173-
}
174-
>
175-
string
176-
</span>
188+
>
189+
string
190+
</span>
191+
<div
192+
style={
193+
{
194+
"marginTop": "var(--ifm-table-cell-padding)",
195+
}
196+
}
197+
>
198+
<div>
199+
<strong>
200+
Possible values :
201+
</strong>
202+
 
203+
<ul>
204+
<li>
205+
<code>
206+
US
207+
</code>
208+
</li>
209+
</ul>
210+
</div>
211+
</div>
212+
<div
213+
style={
214+
{
215+
"marginTop": "var(--ifm-table-cell-padding)",
216+
}
217+
}
218+
>
219+
const (as single enum value)
220+
</div>
221+
</div>
222+
</div>
223+
</details>
224+
</li>
225+
</ul>
226+
<div
227+
style={
228+
{
229+
"marginTop": "var(--ifm-table-cell-padding)",
230+
}
231+
}
232+
/>
233+
</div>
234+
</div>
235+
</details>
236+
`;
237+
238+
exports[`JSONSchemaViewer - Generic keywords test const 1`] = `
239+
<details
240+
open={true}
241+
>
242+
<summary>
243+
<strong>
244+
Schema
245+
</strong>
246+
</summary>
247+
<div>
248+
<div>
249+
<strong>
250+
type
251+
</strong>
252+
 : 
253+
<span
254+
style={
255+
{
256+
"opacity": "0.6",
257+
}
258+
}
259+
>
260+
object
261+
</span>
262+
<ul>
263+
<li
264+
data-level={1}
265+
id="/properties/country"
266+
>
267+
<details
268+
open={false}
269+
>
270+
<summary>
271+
<strong>
272+
country
273+
</strong>
274+
 
275+
<code>
276+
"United States of America"
277+
</code>
278+
 
279+
<strong>
280+
constant
281+
</strong>
177282
</summary>
178283
<div>
179284
<div>

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

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -330,15 +330,13 @@ 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>
336+
 
337+
<strong>
338+
constant
339+
</strong>
342340
</summary>
343341
<div>
344342
<div>
@@ -589,19 +587,17 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else (multiple) 1`
589587
country
590588
</strong>
591589
 
592-
<span
593-
style={
594-
{
595-
"opacity": "0.6",
596-
}
597-
}
598-
>
599-
string
600-
</span>
590+
<code>
591+
"Canada"
592+
</code>
601593
 
602594
<strong>
603595
required
604596
</strong>
597+
 
598+
<strong>
599+
constant
600+
</strong>
605601
</summary>
606602
<div>
607603
<div>
@@ -852,19 +848,17 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else (multiple) 1`
852848
country
853849
</strong>
854850
 
855-
<span
856-
style={
857-
{
858-
"opacity": "0.6",
859-
}
860-
}
861-
>
862-
string
863-
</span>
851+
<code>
852+
"Netherlands"
853+
</code>
864854
 
865855
<strong>
866856
required
867857
</strong>
858+
 
859+
<strong>
860+
constant
861+
</strong>
868862
</summary>
869863
<div>
870864
<div>
@@ -1253,15 +1247,13 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else 1`] = `
12531247
country
12541248
</strong>
12551249
 
1256-
<span
1257-
style={
1258-
{
1259-
"opacity": "0.6",
1260-
}
1261-
}
1262-
>
1263-
string
1264-
</span>
1250+
<code>
1251+
"United States of America"
1252+
</code>
1253+
 
1254+
<strong>
1255+
constant
1256+
</strong>
12651257
</summary>
12661258
<div>
12671259
<div>

__tests__/JSONSchemaViewer/generic_keywords.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ const testcases: [string, JSONSchema][] = [
4343
examples: ["red"],
4444
},
4545
],
46+
[
47+
"const (as single enum value)",
48+
{
49+
properties: {
50+
country: {
51+
enum: ["US"],
52+
description: "const (as single enum value)",
53+
},
54+
},
55+
},
56+
],
4657
]
4758

4859
describe("JSONSchemaViewer - Generic keywords", () => {

src/theme/JSONSchemaViewer/components/SchemaItem/SchemaItem.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
DeprecatedLabel,
99
WriteOnlyLabel,
1010
ReadOnlyLabel,
11+
ConstantLabel,
1112
} from "@theme/JSONSchemaViewer/labels"
1213

1314
import { GenerateFriendlyName } from "@theme/JSONSchemaViewer/utils"
@@ -43,6 +44,10 @@ export default function SchemaItem({
4344
typeof typedSchema !== "boolean" && typedSchema.readOnly === true
4445
let isWriteOnly =
4546
typeof typedSchema !== "boolean" && typedSchema.writeOnly === true
47+
let isConstant =
48+
typeof typedSchema !== "boolean" &&
49+
(typedSchema.const !== undefined ||
50+
(Array.isArray(typedSchema.enum) && typedSchema.enum.length === 1))
4651
let isRequired = !isDeprecated && required
4752

4853
// Header
@@ -58,6 +63,8 @@ export default function SchemaItem({
5863
{isReadOnly && <ReadOnlyLabel />}
5964
{isWriteOnly && <>&nbsp;</>}
6065
{isWriteOnly && <WriteOnlyLabel />}
66+
{isConstant && <>&nbsp;</>}
67+
{isConstant && <ConstantLabel />}
6168
</>
6269
)
6370

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from "react"
2+
import Translate from "@docusaurus/Translate"
3+
4+
import styles from "./styles.module.css"
5+
6+
export default function ConstantLabel(): JSX.Element {
7+
return (
8+
<strong className={styles.constant}>
9+
<Translate
10+
values={{
11+
id: "json-schema.keywords.const",
12+
}}
13+
>
14+
{"constant"}
15+
</Translate>
16+
</strong>
17+
)
18+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.constant {
2+
font-size: var(--ifm-code-font-size);
3+
color: var(--ifm-color-secondary-darkest);
4+
}

src/theme/JSONSchemaViewer/labels/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ export { default as AndLabel } from "@theme/JSONSchemaViewer/labels/And"
2222
export { default as OrLabel } from "@theme/JSONSchemaViewer/labels/Or"
2323
export { default as NotLabel } from "@theme/JSONSchemaViewer/labels/Not"
2424
export { default as XorLabel } from "@theme/JSONSchemaViewer/labels/Xor"
25+
export { default as ConstantLabel } from "@theme/JSONSchemaViewer/labels/Constant/Constant"

src/theme/JSONSchemaViewer/utils/generateFriendlyName.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ function CustomizeType({ schema, type }: CustomizeProps): JSX.Element {
137137
return <CustomizeArray schema={schema as JSONSchemaNS.Array} />
138138
}
139139

140+
// For constant values
141+
if (!["array", "object"].includes(type)) {
142+
if (schema.const !== undefined) {
143+
return <code>{`${JSON.stringify(schema.const)}`}</code>
144+
}
145+
if (schema.enum !== undefined && schema.enum.length === 1) {
146+
return <code>{`${JSON.stringify(schema.enum[0])}`}</code>
147+
}
148+
}
149+
140150
// By default, render the type as it
141151
return <TypeLabelSwitch type={type} />
142152
}

0 commit comments

Comments
 (0)