Skip to content

Commit

Permalink
Merge pull request #242 from jy95/improve-const-view
Browse files Browse the repository at this point in the history
feat: display constant values more prominently
  • Loading branch information
jy95 authored Jan 8, 2024
2 parents cfe0722 + 838172d commit 100faab
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,10 @@ exports[`JSONSchemaViewer - constructor Overwrite default ValueComponent value 1
>
object
</span>
 
<strong>
constant
</strong>
</summary>
<div>
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2220,15 +2220,13 @@ exports[`JSONSchemaViewer - generateFriendlyName cases test Prefer title 1`] = `
text
</strong>
 
<span
style={
{
"opacity": "0.6",
}
}
>
string
</span>
<code>
"Hello World"
</code>
 
<strong>
constant
</strong>
</summary>
<div>
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ exports[`JSONSchemaViewer - Generic keywords test annotations 1`] = `
</details>
`;

exports[`JSONSchemaViewer - Generic keywords test const 1`] = `
exports[`JSONSchemaViewer - Generic keywords test const (as single enum value) 1`] = `
<details
open={true}
>
Expand Down Expand Up @@ -165,15 +165,120 @@ exports[`JSONSchemaViewer - Generic keywords test const 1`] = `
country
</strong>
 
<span
style={
{
"opacity": "0.6",
<code>
"US"
</code>
 
<strong>
constant
</strong>
</summary>
<div>
<div>
<strong>
type
</strong>
 : 
<span
style={
{
"opacity": "0.6",
}
}
}
>
string
</span>
>
string
</span>
<div
style={
{
"marginTop": "var(--ifm-table-cell-padding)",
}
}
>
<div>
<strong>
Possible values :
</strong>
 
<ul>
<li>
<code>
US
</code>
</li>
</ul>
</div>
</div>
<div
style={
{
"marginTop": "var(--ifm-table-cell-padding)",
}
}
>
const (as single enum value)
</div>
</div>
</div>
</details>
</li>
</ul>
<div
style={
{
"marginTop": "var(--ifm-table-cell-padding)",
}
}
/>
</div>
</div>
</details>
`;

exports[`JSONSchemaViewer - Generic keywords test const 1`] = `
<details
open={true}
>
<summary>
<strong>
Schema
</strong>
</summary>
<div>
<div>
<strong>
type
</strong>
 : 
<span
style={
{
"opacity": "0.6",
}
}
>
object
</span>
<ul>
<li
data-level={1}
id="/properties/country"
>
<details
open={false}
>
<summary>
<strong>
country
</strong>
 
<code>
"United States of America"
</code>
 
<strong>
constant
</strong>
</summary>
<div>
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,13 @@ 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
</strong>
</summary>
<div>
<div>
Expand Down Expand Up @@ -589,19 +587,17 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else (multiple) 1`
country
</strong>
 
<span
style={
{
"opacity": "0.6",
}
}
>
string
</span>
<code>
"Canada"
</code>
 
<strong>
required
</strong>
 
<strong>
constant
</strong>
</summary>
<div>
<div>
Expand Down Expand Up @@ -852,19 +848,17 @@ exports[`JSONSchemaViewer - schema conditionally test If Then Else (multiple) 1`
country
</strong>
 
<span
style={
{
"opacity": "0.6",
}
}
>
string
</span>
<code>
"Netherlands"
</code>
 
<strong>
required
</strong>
 
<strong>
constant
</strong>
</summary>
<div>
<div>
Expand Down Expand Up @@ -1253,15 +1247,13 @@ 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
</strong>
</summary>
<div>
<div>
Expand Down
11 changes: 11 additions & 0 deletions __tests__/JSONSchemaViewer/generic_keywords.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ const testcases: [string, JSONSchema][] = [
examples: ["red"],
},
],
[
"const (as single enum value)",
{
properties: {
country: {
enum: ["US"],
description: "const (as single enum value)",
},
},
},
],
]

describe("JSONSchemaViewer - Generic keywords", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DeprecatedLabel,
WriteOnlyLabel,
ReadOnlyLabel,
ConstantLabel,
} from "@theme/JSONSchemaViewer/labels"

import { GenerateFriendlyName } from "@theme/JSONSchemaViewer/utils"
Expand Down Expand Up @@ -43,6 +44,10 @@ export default function SchemaItem({
typeof typedSchema !== "boolean" && typedSchema.readOnly === true
let isWriteOnly =
typeof typedSchema !== "boolean" && typedSchema.writeOnly === true
let isConstant =
typeof typedSchema !== "boolean" &&
(typedSchema.const !== undefined ||
(Array.isArray(typedSchema.enum) && typedSchema.enum.length === 1))
let isRequired = !isDeprecated && required

// Header
Expand All @@ -58,6 +63,8 @@ export default function SchemaItem({
{isReadOnly && <ReadOnlyLabel />}
{isWriteOnly && <>&nbsp;</>}
{isWriteOnly && <WriteOnlyLabel />}
{isConstant && <>&nbsp;</>}
{isConstant && <ConstantLabel />}
</>
)

Expand Down
18 changes: 18 additions & 0 deletions src/theme/JSONSchemaViewer/labels/Constant/Constant.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react"
import Translate from "@docusaurus/Translate"

import styles from "./styles.module.css"

export default function ConstantLabel(): JSX.Element {
return (
<strong className={styles.constant}>
<Translate
values={{
id: "json-schema.keywords.const",
}}
>
{"constant"}
</Translate>
</strong>
)
}
4 changes: 4 additions & 0 deletions src/theme/JSONSchemaViewer/labels/Constant/styles.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.constant {
font-size: var(--ifm-code-font-size);
color: var(--ifm-color-secondary-darkest);
}
1 change: 1 addition & 0 deletions src/theme/JSONSchemaViewer/labels/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export { default as AndLabel } from "@theme/JSONSchemaViewer/labels/And"
export { default as OrLabel } from "@theme/JSONSchemaViewer/labels/Or"
export { default as NotLabel } from "@theme/JSONSchemaViewer/labels/Not"
export { default as XorLabel } from "@theme/JSONSchemaViewer/labels/Xor"
export { default as ConstantLabel } from "@theme/JSONSchemaViewer/labels/Constant/Constant"
10 changes: 10 additions & 0 deletions src/theme/JSONSchemaViewer/utils/generateFriendlyName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,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 <code>{`${JSON.stringify(schema.const)}`}</code>
}
if (schema.enum !== undefined && schema.enum.length === 1) {
return <code>{`${JSON.stringify(schema.enum[0])}`}</code>
}
}

// By default, render the type as it
return <TypeLabelSwitch type={type} />
}
Expand Down
3 changes: 2 additions & 1 deletion testsite/static/schemas/examples/object/required.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"email": { "type": "string" },
"address": { "type": "string" },
"telephone": { "type": "string", "deprecated": true },
"password": { "type": "string", "writeOnly": true }
"password": { "type": "string", "writeOnly": true },
"kind": { "type": "string", "const": "USER" }
},
"required": ["name", "email"]
}

0 comments on commit 100faab

Please sign in to comment.