feat(json-schema-2020-12): add support for oneOf keyword

Refs #8513
This commit is contained in:
Vladimir Gorej
2023-04-21 08:38:47 +02:00
committed by Vladimír Gorej
parent ff2d4b2311
commit 15830794f1
9 changed files with 81 additions and 8 deletions

View File

@@ -43,6 +43,7 @@ const JSONSchema = ({ schema, name }) => {
const Keyword$comment = useComponent("Keyword$comment")
const KeywordAllOf = useComponent("KeywordAllOf")
const KeywordAnyOf = useComponent("KeywordAnyOf")
const KeywordOneOf = useComponent("KeywordOneOf")
const KeywordProperties = useComponent("KeywordProperties")
const KeywordType = useComponent("KeywordType")
const KeywordFormat = useComponent("KeywordFormat")
@@ -109,6 +110,7 @@ const JSONSchema = ({ schema, name }) => {
)}
<KeywordAllOf schema={schema} />
<KeywordAnyOf schema={schema} />
<KeywordOneOf schema={schema} />
<Keyword$schema schema={schema} />
<Keyword$vocabulary schema={schema} />
<Keyword$id schema={schema} />

View File

@@ -10,6 +10,7 @@
@import './keywords/$defs/$defs';
@import './keywords/AllOf/all-of';
@import './keywords/AnyOf/any-of';
@import './keywords/OneOf/one-of';
@import './keywords/Type/type';
@import './keywords/Format/format';
@import './keywords/Description/description';

View File

@@ -1,17 +1,11 @@
.json-schema-2020-12 {
&__anyOf {
margin: 5px 0 5px 0;
& > ul {
@include expansion-border;
padding: 0;
}
@extend .json-schema-2020-12__allOf;
}
&-core-keyword {
&--anyOf {
color: $text-code-default-font-color;
font-style: normal;
@extend .json-schema-2020-12-core-keyword--allOf;
}
}
}

View File

@@ -0,0 +1,56 @@
/**
* @prettier
*/
import React, { useCallback, useState } from "react"
import { schema } from "../../../prop-types"
import { useFn, useComponent, useIsExpandedDeeply } from "../../../hooks"
const OneOf = ({ schema }) => {
const oneOf = schema?.oneOf || []
if (!Array.isArray(oneOf) || oneOf.length === 0) {
return null
}
const fn = useFn()
const isExpandedDeeply = useIsExpandedDeeply()
const [expanded, setExpanded] = useState(isExpandedDeeply)
const Accordion = useComponent("Accordion")
const JSONSchema = useComponent("JSONSchema")
const handleExpansion = useCallback(() => {
setExpanded((prev) => !prev)
}, [])
return (
<div className="json-schema-2020-12__oneOf">
<Accordion expanded={expanded} onChange={handleExpansion}>
<span className="json-schema-2020-12-core-keyword json-schema-2020-12-core-keyword--oneOf">
OneOf
</span>
<span className="json-schema-2020-12__type">
{fn.getType({ oneOf })}
</span>
</Accordion>
{expanded && (
<ul>
{oneOf.map((schema, index) => (
<li key={`#${index}`} className="json-schema-2020-12-property">
<JSONSchema
name={`#${index} ${fn.getTitle(schema)}`}
schema={schema}
/>
</li>
))}
</ul>
)}
</div>
)
}
OneOf.propTypes = {
schema: schema.isRequired,
}
export default OneOf

View File

@@ -0,0 +1,13 @@
.json-schema-2020-12 {
&__oneOf {
@extend .json-schema-2020-12__allOf;
}
&-core-keyword {
&--oneOf {
@extend .json-schema-2020-12-core-keyword--allOf;
}
}
}

View File

@@ -124,6 +124,7 @@ export const isExpandable = (schema) => {
schema?.$comment ||
schema?.allOf ||
schema?.anyOf ||
schema?.oneOf ||
schema?.description ||
schema?.properties
)

View File

@@ -15,6 +15,7 @@ import Keyword$defs from "./components/keywords/$defs/$defs"
import Keyword$comment from "./components/keywords/$comment"
import KeywordAllOf from "./components/keywords/AllOf/AllOf"
import KeywordAnyOf from "./components/keywords/AnyOf/AnyOf"
import KeywordOneOf from "./components/keywords/OneOf/OneOf"
import KeywordProperties from "./components/keywords/Properties/Properties"
import KeywordType from "./components/keywords/Type/Type"
import KeywordFormat from "./components/keywords/Format/Format"
@@ -47,6 +48,7 @@ export const withJSONSchemaContext = (Component, overrides = {}) => {
Keyword$comment,
KeywordAllOf,
KeywordAnyOf,
KeywordOneOf,
KeywordProperties,
KeywordType,
KeywordFormat,

View File

@@ -14,6 +14,7 @@ import Keyword$defs from "./components/keywords/$defs/$defs"
import Keyword$comment from "./components/keywords/$comment"
import KeywordAllOf from "./components/keywords/AllOf/AllOf"
import KeywordAnyOf from "./components/keywords/AnyOf/AnyOf"
import KeywordOneOf from "./components/keywords/OneOf/OneOf"
import KeywordType from "./components/keywords/Type/Type"
import KeywordFormat from "./components/keywords/Format/Format"
import KeywordTitle from "./components/keywords/Title/Title"
@@ -38,6 +39,7 @@ const JSONSchema202012Plugin = () => ({
JSONSchema202012Keyword$comment: Keyword$comment,
JSONSchema202012KeywordAllOf: KeywordAllOf,
JSONSchema202012KeywordAnyOf: KeywordAnyOf,
JSONSchema202012KeywordOneOf: KeywordOneOf,
JSONSchema202012KeywordProperties: KeywordProperties,
JSONSchema202012KeywordType: KeywordType,
JSONSchema202012KeywordFormat: KeywordFormat,

View File

@@ -22,6 +22,7 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => {
const Keyword$comment = getComponent("JSONSchema202012Keyword$comment")
const KeywordAllOf = getComponent("JSONSchema202012KeywordAllOf")
const KeywordAnyOf = getComponent("JSONSchema202012KeywordAnyOf")
const KeywordOneOf = getComponent("JSONSchema202012KeywordOneOf")
const KeywordProperties = getComponent("JSONSchema202012KeywordProperties")
const KeywordType = getComponent("JSONSchema202012KeywordType")
const KeywordFormat = getComponent("JSONSchema202012KeywordFormat")
@@ -52,6 +53,7 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => {
Keyword$comment,
KeywordAllOf,
KeywordAnyOf,
KeywordOneOf,
KeywordProperties,
KeywordType,
KeywordFormat,