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 KeywordContains = useComponent("KeywordContains")
const KeywordProperties = useComponent("KeywordProperties") const KeywordProperties = useComponent("KeywordProperties")
const KeywordPatternProperties = useComponent("KeywordPatternProperties") const KeywordPatternProperties = useComponent("KeywordPatternProperties")
const KeywordAdditionalProperties = useComponent(
"KeywordAdditionalProperties"
)
const KeywordType = useComponent("KeywordType") const KeywordType = useComponent("KeywordType")
const KeywordFormat = useComponent("KeywordFormat") const KeywordFormat = useComponent("KeywordFormat")
const KeywordTitle = useComponent("KeywordTitle") const KeywordTitle = useComponent("KeywordTitle")
@@ -118,19 +121,20 @@ const JSONSchema = ({ schema, name }) => {
<> <>
<KeywordProperties schema={schema} /> <KeywordProperties schema={schema} />
<KeywordPatternProperties 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$schema schema={schema} />
<Keyword$vocabulary schema={schema} /> <Keyword$vocabulary schema={schema} />
<Keyword$id 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 React from "react"
import { schema } from "../../prop-types" import { schema } from "../../prop-types"
import { useComponent } from "../../hooks" import { useFn, useComponent } from "../../hooks"
const Contains = ({ schema }) => { 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 JSONSchema = useComponent("JSONSchema")
const name = ( const name = (

View File

@@ -4,10 +4,12 @@
import React from "react" import React from "react"
import { schema } from "../../prop-types" import { schema } from "../../prop-types"
import { useComponent } from "../../hooks" import { useFn, useComponent } from "../../hooks"
const Else = ({ schema }) => { 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 JSONSchema = useComponent("JSONSchema")
const name = ( const name = (

View File

@@ -4,10 +4,12 @@
import React from "react" import React from "react"
import { schema } from "../../prop-types" import { schema } from "../../prop-types"
import { useComponent } from "../../hooks" import { useFn, useComponent } from "../../hooks"
const If = ({ schema }) => { 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 JSONSchema = useComponent("JSONSchema")
const name = ( const name = (

View File

@@ -4,10 +4,12 @@
import React from "react" import React from "react"
import { schema } from "../../prop-types" import { schema } from "../../prop-types"
import { useComponent } from "../../hooks" import { useFn, useComponent } from "../../hooks"
const Not = ({ schema }) => { 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 JSONSchema = useComponent("JSONSchema")
const name = ( const name = (

View File

@@ -4,10 +4,12 @@
import React from "react" import React from "react"
import { schema } from "../../prop-types" import { schema } from "../../prop-types"
import { useComponent } from "../../hooks" import { useFn, useComponent } from "../../hooks"
const Then = ({ schema }) => { 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 JSONSchema = useComponent("JSONSchema")
const name = ( const name = (

View File

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

View File

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

View File

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

View File

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