feat(json-schema-2020-12): add support for const keyword (#8622)

Refs #8513
This commit is contained in:
Vladimír Gorej
2023-05-07 16:49:23 +02:00
committed by GitHub
parent b4e5af901d
commit 8e4fde5ddd
7 changed files with 69 additions and 1 deletions

View File

@@ -63,6 +63,7 @@ const JSONSchema = forwardRef(({ schema, name, onExpand }, ref) => {
"KeywordUnevaluatedProperties"
)
const KeywordType = useComponent("KeywordType")
const KeywordConst = useComponent("KeywordConst")
const KeywordFormat = useComponent("KeywordFormat")
const KeywordTitle = useComponent("KeywordTitle")
const KeywordDescription = useComponent("KeywordDescription")
@@ -157,6 +158,7 @@ const JSONSchema = forwardRef(({ schema, name, onExpand }, ref) => {
<KeywordContains schema={schema} />
</>
)}
<KeywordConst schema={schema} />
<Keyword$schema schema={schema} />
<Keyword$vocabulary schema={schema} />
<Keyword$id schema={schema} />

View File

@@ -0,0 +1,30 @@
/**
* @prettier
*/
import React from "react"
import { schema } from "../../prop-types"
import { useFn } from "../../hooks"
const Const = ({ schema }) => {
const fn = useFn()
if (!fn.hasKeyword(schema, "const")) return null
return (
<div className="json-schema-2020-12-keyword json-schema-2020-12-keyword--const">
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--primary">
Const
</span>
<span className="json-schema-2020-12-keyword__value json-schema-2020-12-keyword__value--const">
{fn.stringify(schema.const)}
</span>
</div>
)
}
Const.propTypes = {
schema: schema.isRequired,
}
export default Const

View File

@@ -41,6 +41,18 @@
color: #6b6b6b;
font-style: italic;
}
&--const {
@include text_code();
color: #6b6b6b;
font-style: normal;
display: inline-block;
margin-left: 10px;
line-height: 1.5;
padding: 1px 4px 1px 4px;
border: 1px dashed #6b6b6b;
border-radius: 4px;
}
}
}
.json-schema-2020-12-keyword__name--secondary + .json-schema-2020-12-keyword__value--secondary::before {

View File

@@ -159,6 +159,22 @@ export const isExpandable = (schema) => {
fn.hasKeyword(schema, "propertyNames") ||
fn.hasKeyword(schema, "unevaluatedItems") ||
fn.hasKeyword(schema, "unevaluatedProperties") ||
schema?.description
schema?.description ||
fn.hasKeyword(schema, "const")
)
}
export const stringify = (value) => {
if (
value === null ||
["number", "bigint", "boolean"].includes(typeof value)
) {
return String(value)
}
if (Array.isArray(value)) {
return `[${value.map(stringify).join(", ")}]`
}
return JSON.stringify(value)
}

View File

@@ -31,6 +31,7 @@ import KeywordPropertyNames from "./components/keywords/PropertyNames"
import KeywordUnevaluatedItems from "./components/keywords/UnevaluatedItems"
import KeywordUnevaluatedProperties from "./components/keywords/UnevaluatedProperties"
import KeywordType from "./components/keywords/Type/Type"
import KeywordConst from "./components/keywords/Const"
import KeywordFormat from "./components/keywords/Format/Format"
import KeywordTitle from "./components/keywords/Title/Title"
import KeywordDescription from "./components/keywords/Description/Description"
@@ -45,6 +46,7 @@ import {
getType,
hasKeyword,
isExpandable,
stringify,
} from "./fn"
export const withJSONSchemaContext = (Component, overrides = {}) => {
@@ -78,6 +80,7 @@ export const withJSONSchemaContext = (Component, overrides = {}) => {
KeywordUnevaluatedItems,
KeywordUnevaluatedProperties,
KeywordType,
KeywordConst,
KeywordFormat,
KeywordTitle,
KeywordDescription,
@@ -106,6 +109,7 @@ export const withJSONSchemaContext = (Component, overrides = {}) => {
isBooleanJSONSchema,
hasKeyword,
isExpandable,
stringify,
...overrides.fn,
},
}

View File

@@ -29,6 +29,7 @@ import KeywordPropertyNames from "./components/keywords/PropertyNames"
import KeywordUnevaluatedItems from "./components/keywords/UnevaluatedItems"
import KeywordUnevaluatedProperties from "./components/keywords/UnevaluatedProperties"
import KeywordType from "./components/keywords/Type/Type"
import KeywordConst from "./components/keywords/Const"
import KeywordFormat from "./components/keywords/Format/Format"
import KeywordTitle from "./components/keywords/Title/Title"
import KeywordDescription from "./components/keywords/Description/Description"
@@ -68,6 +69,7 @@ const JSONSchema202012Plugin = () => ({
JSONSchema202012KeywordUnevaluatedItems: KeywordUnevaluatedItems,
JSONSchema202012KeywordUnevaluatedProperties: KeywordUnevaluatedProperties,
JSONSchema202012KeywordType: KeywordType,
JSONSchema202012KeywordConst: KeywordConst,
JSONSchema202012KeywordFormat: KeywordFormat,
JSONSchema202012KeywordTitle: KeywordTitle,
JSONSchema202012KeywordDescription: KeywordDescription,

View File

@@ -56,6 +56,7 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => {
"JSONSchema202012KeywordUnevaluatedProperties"
)
const KeywordType = getComponent("JSONSchema202012KeywordType")
const KeywordConst = getComponent("JSONSchema202012KeywordConst")
const KeywordFormat = getComponent("JSONSchema202012KeywordFormat")
const KeywordTitle = getComponent("JSONSchema202012KeywordTitle")
const KeywordDescription = getComponent(
@@ -101,6 +102,7 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => {
KeywordUnevaluatedItems,
KeywordUnevaluatedProperties,
KeywordType,
KeywordConst,
KeywordFormat,
KeywordTitle,
KeywordDescription,