Skip to content

Commit

Permalink
feat(json-schema-2020-12): add support for enum keyword (#8623)
Browse files Browse the repository at this point in the history
Refs #8513
  • Loading branch information
char0n committed May 7, 2023
1 parent 8e4fde5 commit 3b940d0
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const JSONSchema = forwardRef(({ schema, name, onExpand }, ref) => {
"KeywordUnevaluatedProperties"
)
const KeywordType = useComponent("KeywordType")
const KeywordEnum = useComponent("KeywordEnum")
const KeywordConst = useComponent("KeywordConst")
const KeywordFormat = useComponent("KeywordFormat")
const KeywordTitle = useComponent("KeywordTitle")
Expand Down Expand Up @@ -158,6 +159,7 @@ const JSONSchema = forwardRef(({ schema, name, onExpand }, ref) => {
<KeywordContains schema={schema} />
</>
)}
<KeywordEnum schema={schema} />
<KeywordConst schema={schema} />
<Keyword$schema schema={schema} />
<Keyword$vocabulary schema={schema} />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @prettier
*/
import React from "react"

import { schema } from "../../../prop-types"
import { useFn } from "../../../hooks"

const Enum = ({ schema }) => {
const fn = useFn()

if (!Array.isArray(schema?.enum)) return null

return (
<div className="json-schema-2020-12-keyword json-schema-2020-12-keyword--enum">
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--primary">
Allowed values
</span>
<ul>
{schema.enum.map((element) => {
const strigifiedElement = fn.stringify(element)

return (
<li key={strigifiedElement}>
<span className="json-schema-2020-12-keyword__value json-schema-2020-12-keyword__value--const">
{strigifiedElement}
</span>
</li>
)
})}
</ul>
</div>
)
}

Enum.propTypes = {
schema: schema.isRequired,
}

export default Enum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.json-schema-2020-12-keyword--enum {
& > ul {
display: inline-block;
padding: 0;
margin: 0;

li {
display: inline;
list-style-type: none;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@
@import './Title/title';
@import './Properties/properties';
@import './PatternProperties/pattern-properties';
@import './Enum/enum';
1 change: 1 addition & 0 deletions src/core/plugins/json-schema-2020-12/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export const isExpandable = (schema) => {
fn.hasKeyword(schema, "unevaluatedItems") ||
fn.hasKeyword(schema, "unevaluatedProperties") ||
schema?.description ||
schema?.enum ||
fn.hasKeyword(schema, "const")
)
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/plugins/json-schema-2020-12/hoc.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 KeywordEnum from "./components/keywords/Enum/Enum"
import KeywordConst from "./components/keywords/Const"
import KeywordFormat from "./components/keywords/Format/Format"
import KeywordTitle from "./components/keywords/Title/Title"
Expand Down Expand Up @@ -80,6 +81,7 @@ export const withJSONSchemaContext = (Component, overrides = {}) => {
KeywordUnevaluatedItems,
KeywordUnevaluatedProperties,
KeywordType,
KeywordEnum,
KeywordConst,
KeywordFormat,
KeywordTitle,
Expand Down
2 changes: 2 additions & 0 deletions src/core/plugins/json-schema-2020-12/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 KeywordEnum from "./components/keywords/Enum/Enum"
import KeywordConst from "./components/keywords/Const"
import KeywordFormat from "./components/keywords/Format/Format"
import KeywordTitle from "./components/keywords/Title/Title"
Expand Down Expand Up @@ -69,6 +70,7 @@ const JSONSchema202012Plugin = () => ({
JSONSchema202012KeywordUnevaluatedItems: KeywordUnevaluatedItems,
JSONSchema202012KeywordUnevaluatedProperties: KeywordUnevaluatedProperties,
JSONSchema202012KeywordType: KeywordType,
JSONSchema202012KeywordEnum: KeywordEnum,
JSONSchema202012KeywordConst: KeywordConst,
JSONSchema202012KeywordFormat: KeywordFormat,
JSONSchema202012KeywordTitle: KeywordTitle,
Expand Down
2 changes: 2 additions & 0 deletions src/core/plugins/oas31/wrap-components/models.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => {
"JSONSchema202012KeywordUnevaluatedProperties"
)
const KeywordType = getComponent("JSONSchema202012KeywordType")
const KeywordEnum = getComponent("JSONSchema202012KeywordEnum")
const KeywordConst = getComponent("JSONSchema202012KeywordConst")
const KeywordFormat = getComponent("JSONSchema202012KeywordFormat")
const KeywordTitle = getComponent("JSONSchema202012KeywordTitle")
Expand Down Expand Up @@ -102,6 +103,7 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => {
KeywordUnevaluatedItems,
KeywordUnevaluatedProperties,
KeywordType,
KeywordEnum,
KeywordConst,
KeywordFormat,
KeywordTitle,
Expand Down

0 comments on commit 3b940d0

Please sign in to comment.