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

Refs #8513
This commit is contained in:
Vladimir Gorej
2023-04-24 17:18:20 +02:00
committed by Vladimír Gorej
parent 66d55034b8
commit 64ee5fa639
11 changed files with 106 additions and 27 deletions

View File

@@ -54,6 +54,9 @@ const JSONSchema = ({ schema, name }) => {
const KeywordContains = useComponent("KeywordContains")
const KeywordProperties = useComponent("KeywordProperties")
const KeywordPatternProperties = useComponent("KeywordPatternProperties")
const KeywordAdditionalProperties = useComponent(
"KeywordAdditionalProperties"
)
const KeywordType = useComponent("KeywordType")
const KeywordFormat = useComponent("KeywordFormat")
const KeywordTitle = useComponent("KeywordTitle")
@@ -118,19 +121,20 @@ const JSONSchema = ({ schema, name }) => {
<>
<KeywordProperties schema={schema} />
<KeywordPatternProperties schema={schema} />
<KeywordAdditionalProperties schema={schema} />
<KeywordAllOf schema={schema} />
<KeywordAnyOf schema={schema} />
<KeywordOneOf schema={schema} />
<KeywordNot schema={schema} />
<KeywordIf schema={schema} />
<KeywordThen schema={schema} />
<KeywordElse schema={schema} />
<KeywordDependentSchemas schema={schema} />
<KeywordPrefixItems schema={schema} />
<KeywordItems schema={schema} />
<KeywordContains schema={schema} />
</>
)}
<KeywordAllOf schema={schema} />
<KeywordAnyOf schema={schema} />
<KeywordOneOf schema={schema} />
<KeywordNot schema={schema} />
<KeywordIf schema={schema} />
<KeywordThen schema={schema} />
<KeywordElse schema={schema} />
<KeywordDependentSchemas schema={schema} />
<KeywordPrefixItems schema={schema} />
<KeywordItems schema={schema} />
<KeywordContains schema={schema} />
<Keyword$schema schema={schema} />
<Keyword$vocabulary schema={schema} />
<Keyword$id schema={schema} />

View File

@@ -0,0 +1,45 @@
/**
* @prettier
*/
import React from "react"
import { schema } from "../../prop-types"
import { useFn, useComponent } from "../../hooks"
const AdditionalProperties = ({ schema }) => {
const fn = useFn()
if (!fn.hasKeyword(schema, "additionalProperties")) return null
const { additionalProperties } = schema
const JSONSchema = useComponent("JSONSchema")
const name = (
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--primary">
Additional Properties
</span>
)
return (
<div className="json-schema-2020-12-keyword json-schema-2020-12-keyword--additionalProperties">
{additionalProperties === true ? (
<>
{name}
<span className="json-schema-2020-12__type">allowed</span>
</>
) : additionalProperties === false ? (
<>
{name}
<span className="json-schema-2020-12__type">forbidden</span>
</>
) : (
<JSONSchema name={name} schema={additionalProperties} />
)}
</div>
)
}
AdditionalProperties.propTypes = {
schema: schema.isRequired,
}
export default AdditionalProperties

View File

@@ -4,10 +4,12 @@
import React from "react"
import { schema } from "../../prop-types"
import { useComponent } from "../../hooks"
import { useFn, useComponent } from "../../hooks"
const Contains = ({ schema }) => {
if (!Object.hasOwn(schema, "contains")) return null
const fn = useFn()
if (!fn.hasKeyword(schema, "contains")) return null
const JSONSchema = useComponent("JSONSchema")
const name = (

View File

@@ -4,10 +4,12 @@
import React from "react"
import { schema } from "../../prop-types"
import { useComponent } from "../../hooks"
import { useFn, useComponent } from "../../hooks"
const Else = ({ schema }) => {
if (!Object.hasOwn(schema, "contains")) return null
const fn = useFn()
if (!fn.hasKeyword(schema, "else")) return null
const JSONSchema = useComponent("JSONSchema")
const name = (

View File

@@ -4,10 +4,12 @@
import React from "react"
import { schema } from "../../prop-types"
import { useComponent } from "../../hooks"
import { useFn, useComponent } from "../../hooks"
const If = ({ schema }) => {
if (!Object.hasOwn(schema, "if")) return null
const fn = useFn()
if (!fn.hasKeyword(schema, "if")) return null
const JSONSchema = useComponent("JSONSchema")
const name = (

View File

@@ -4,10 +4,12 @@
import React from "react"
import { schema } from "../../prop-types"
import { useComponent } from "../../hooks"
import { useFn, useComponent } from "../../hooks"
const Not = ({ schema }) => {
if (!Object.hasOwn(schema, "contains")) return null
const fn = useFn()
if (!fn.hasKeyword(schema, "not")) return null
const JSONSchema = useComponent("JSONSchema")
const name = (

View File

@@ -4,10 +4,12 @@
import React from "react"
import { schema } from "../../prop-types"
import { useComponent } from "../../hooks"
import { useFn, useComponent } from "../../hooks"
const Then = ({ schema }) => {
if (!Object.hasOwn(schema, "then")) return null
const fn = useFn()
if (!fn.hasKeyword(schema, "then")) return null
const JSONSchema = useComponent("JSONSchema")
const name = (

View File

@@ -1,6 +1,8 @@
/**
* @prettier
*/
import { useFn } from "./hooks"
export const upperFirst = (value) => {
if (typeof value === "string") {
return `${value.charAt(0).toUpperCase()}${value.slice(1)}`
@@ -17,11 +19,13 @@ export const getTitle = (schema) => {
}
export const getType = (schema, processedSchemas = new WeakSet()) => {
const fn = useFn()
if (schema == null) {
return "any"
}
if (typeof schema === "boolean") {
if (fn.isBooleanJSONSchema(schema)) {
return schema ? "any" : "never"
}
@@ -122,7 +126,12 @@ export const getType = (schema, processedSchemas = new WeakSet()) => {
export const isBooleanJSONSchema = (schema) => typeof schema === "boolean"
export const hasKeyword = (schema, keyword) =>
typeof schema === "object" && Object.hasOwn(schema, keyword)
export const isExpandable = (schema) => {
const fn = useFn()
return (
schema?.$schema ||
schema?.$vocabulary ||
@@ -136,16 +145,17 @@ export const isExpandable = (schema) => {
schema?.allOf ||
schema?.anyOf ||
schema?.oneOf ||
Object.hasOwn(schema, "not") ||
Object.hasOwn(schema, "if") ||
Object.hasOwn(schema, "then") ||
Object.hasOwn(schema, "else") ||
fn.hasKeyword(schema, "not") ||
fn.hasKeyword(schema, "if") ||
fn.hasKeyword(schema, "then") ||
fn.hasKeyword(schema, "else") ||
schema?.dependentSchemas ||
schema?.prefixItems ||
schema?.items ||
Object.hasOwn(schema, "contains") ||
fn.hasKeyword(schema, "contains") ||
schema?.properties ||
schema?.patternProperties ||
fn.hasKeyword(schema, "additionalProperties") ||
schema?.description
)
}

View File

@@ -26,6 +26,7 @@ import KeywordItems from "./components/keywords/Items"
import KeywordContains from "./components/keywords/Contains"
import KeywordProperties from "./components/keywords/Properties/Properties"
import KeywordPatternProperties from "./components/keywords/PatternProperties/PatternProperties"
import KeywordAdditionalProperties from "./components/keywords/AdditionalProperties"
import KeywordType from "./components/keywords/Type/Type"
import KeywordFormat from "./components/keywords/Format/Format"
import KeywordTitle from "./components/keywords/Title/Title"
@@ -39,6 +40,7 @@ import {
isBooleanJSONSchema,
upperFirst,
getType,
hasKeyword,
isExpandable,
} from "./fn"
@@ -68,6 +70,7 @@ export const withJSONSchemaContext = (Component, overrides = {}) => {
KeywordContains,
KeywordProperties,
KeywordPatternProperties,
KeywordAdditionalProperties,
KeywordType,
KeywordFormat,
KeywordTitle,
@@ -86,6 +89,7 @@ export const withJSONSchemaContext = (Component, overrides = {}) => {
getTitle,
getType,
isBooleanJSONSchema,
hasKeyword,
isExpandable,
...overrides.fn,
},

View File

@@ -24,6 +24,7 @@ import KeywordItems from "./components/keywords/Items"
import KeywordContains from "./components/keywords/Contains"
import KeywordProperties from "./components/keywords/Properties/Properties"
import KeywordPatternProperties from "./components/keywords/PatternProperties/PatternProperties"
import KeywordAdditionalProperties from "./components/keywords/AdditionalProperties"
import KeywordType from "./components/keywords/Type/Type"
import KeywordFormat from "./components/keywords/Format/Format"
import KeywordTitle from "./components/keywords/Title/Title"
@@ -59,6 +60,7 @@ const JSONSchema202012Plugin = () => ({
JSONSchema202012KeywordContains: KeywordContains,
JSONSchema202012KeywordProperties: KeywordProperties,
JSONSchema202012KeywordPatternProperties: KeywordPatternProperties,
JSONSchema202012KeywordAdditionalProperties: KeywordAdditionalProperties,
JSONSchema202012KeywordType: KeywordType,
JSONSchema202012KeywordFormat: KeywordFormat,
JSONSchema202012KeywordTitle: KeywordTitle,

View File

@@ -37,6 +37,9 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => {
const KeywordPatternProperties = getComponent(
"JSONSchema202012KeywordPatternProperties"
)
const KeywordAdditionalProperties = getComponent(
"JSONSchema202012KeywordAdditionalProperties"
)
const KeywordType = getComponent("JSONSchema202012KeywordType")
const KeywordFormat = getComponent("JSONSchema202012KeywordFormat")
const KeywordTitle = getComponent("JSONSchema202012KeywordTitle")
@@ -77,6 +80,7 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => {
KeywordContains,
KeywordProperties,
KeywordPatternProperties,
KeywordAdditionalProperties,
KeywordType,
KeywordFormat,
KeywordTitle,