refactor(json-schema-2020-12): render format keyword as Constraint (#8646)

Refs #8513
This commit is contained in:
Vladimír Gorej
2023-05-10 16:42:07 +02:00
committed by GitHub
parent f549a1d610
commit de3e852569
10 changed files with 52 additions and 76 deletions

View File

@@ -69,7 +69,6 @@ const JSONSchema = forwardRef(
const KeywordConst = useComponent("KeywordConst") const KeywordConst = useComponent("KeywordConst")
const KeywordConstraint = useComponent("KeywordConstraint") const KeywordConstraint = useComponent("KeywordConstraint")
const KeywordDependentRequired = useComponent("KeywordDependentRequired") const KeywordDependentRequired = useComponent("KeywordDependentRequired")
const KeywordFormat = useComponent("KeywordFormat")
const KeywordContentSchema = useComponent("KeywordContentSchema") const KeywordContentSchema = useComponent("KeywordContentSchema")
const KeywordTitle = useComponent("KeywordTitle") const KeywordTitle = useComponent("KeywordTitle")
const KeywordDescription = useComponent("KeywordDescription") const KeywordDescription = useComponent("KeywordDescription")
@@ -133,11 +132,10 @@ const JSONSchema = forwardRef(
<KeywordTitle title={name} schema={schema} /> <KeywordTitle title={name} schema={schema} />
)} )}
<KeywordType schema={schema} isCircular={isCircular} /> <KeywordType schema={schema} isCircular={isCircular} />
<KeywordFormat schema={schema} />
{constraints.length > 0 && {constraints.length > 0 &&
constraints.map((constraint) => ( constraints.map((constraint) => (
<KeywordConstraint <KeywordConstraint
key={constraint} key={`${constraint.scope}-${constraint.value}`}
constraint={constraint} constraint={constraint}
/> />
))} ))}

View File

@@ -3,32 +3,24 @@
*/ */
import React from "react" import React from "react"
import PropTypes from "prop-types" import PropTypes from "prop-types"
import classNames from "classnames"
/** /**
* This component represents various constraint keywords * This component represents various constraint keywords
* from JSON Schema 2020-12 validation vocabulary. * from JSON Schema 2020-12 validation vocabulary.
*/ */
const Constraint = ({ constraint }) => { const Constraint = ({ constraint }) => (
const isStringRelated = <span
/^matches /.test(constraint) || // pattern keyword className={`json-schema-2020-12__constraint json-schema-2020-12__constraint--${constraint.scope}`}
/characters$/.test(constraint) || // minLength, maxLength keywords >
/^media type: /.test(constraint) || // contentMediaType keyword {constraint.value}
/^encoding: /.test(constraint) // contentEncoding keyword </span>
)
return (
<span
className={classNames("json-schema-2020-12__constraint", {
"json-schema-2020-12__constraint--string-related": isStringRelated,
})}
>
{constraint}
</span>
)
}
Constraint.propTypes = { Constraint.propTypes = {
constraint: PropTypes.string.isRequired, constraint: PropTypes.shape({
scope: PropTypes.oneOf(["number", "string", "array", "object"]).isRequired,
value: PropTypes.string.isRequired,
}).isRequired,
} }
export default React.memo(Constraint) export default React.memo(Constraint)

View File

@@ -7,7 +7,7 @@
background-color: #805AD5; background-color: #805AD5;
border-radius: 4px; border-radius: 4px;
&--string-related { &--string {
color: white; color: white;
background-color: #D69E2E; background-color: #D69E2E;
} }

View File

@@ -1,18 +0,0 @@
/**
* @prettier
*/
import React from "react"
import { schema } from "../../../prop-types"
const Format = ({ schema }) => {
if (!schema?.format) return null
return <span className="json-schema-2020-12__format">{schema.format}</span>
}
Format.propTypes = {
schema: schema.isRequired,
}
export default Format

View File

@@ -1,14 +0,0 @@
.json-schema-2020-12__format {
@include text_code();
margin-left: 10px;
line-height: 1.5;
padding: 1px 3px;
color: white;
background-color: #D69E2E;
border-radius: 4px;
text-transform: lowercase;
&::before {
content: "format: ";
}
}

View File

@@ -67,7 +67,6 @@
@import './$vocabulary/$vocabulary'; @import './$vocabulary/$vocabulary';
@import './Type/type'; @import './Type/type';
@import './Format/format';
@import './Description/description'; @import './Description/description';
@import './Title/title'; @import './Title/title';
@import './Properties/properties'; @import './Properties/properties';

View File

@@ -258,9 +258,18 @@ export const stringifyConstraints = (schema) => {
// validation Keywords for Numeric Instances (number and integer) // validation Keywords for Numeric Instances (number and integer)
const multipleOf = stringifyConstraintMultipleOf(schema) const multipleOf = stringifyConstraintMultipleOf(schema)
if (multipleOf !== null) constraints.push(multipleOf) if (multipleOf !== null) {
constraints.push({ scope: "number", value: multipleOf })
}
const numberRange = stringifyConstraintNumberRange(schema) const numberRange = stringifyConstraintNumberRange(schema)
if (numberRange !== null) constraints.push(numberRange) if (numberRange !== null) {
constraints.push({ scope: "number", value: numberRange })
}
// vocabularies for Semantic Content With "format"
if (schema?.format) {
constraints.push({ scope: "string", value: schema.format })
}
// validation Keywords for Strings // validation Keywords for Strings
const stringRange = stringifyConstraintRange( const stringRange = stringifyConstraintRange(
@@ -268,8 +277,26 @@ export const stringifyConstraints = (schema) => {
schema?.minLength, schema?.minLength,
schema?.maxLength schema?.maxLength
) )
if (stringRange !== null) constraints.push(stringRange) if (stringRange !== null) {
if (schema?.pattern) constraints.push(`matches ${schema?.pattern}`) constraints.push({ scope: "string", value: stringRange })
}
if (schema?.pattern) {
constraints.push({ scope: "string", value: `matches ${schema?.pattern}` })
}
// vocabulary for the Contents of String-Encoded Data
if (schema?.contentMediaType) {
constraints.push({
scope: "string",
value: `media type: ${schema.contentMediaType}`,
})
}
if (schema?.contentEncoding) {
constraints.push({
scope: "string",
value: `encoding: ${schema.contentEncoding}`,
})
}
// validation Keywords for Arrays // validation Keywords for Arrays
const arrayRange = stringifyConstraintRange( const arrayRange = stringifyConstraintRange(
@@ -277,13 +304,17 @@ export const stringifyConstraints = (schema) => {
schema?.minItems, schema?.minItems,
schema?.maxItems schema?.maxItems
) )
if (arrayRange !== null) constraints.push(arrayRange) if (arrayRange !== null) {
constraints.push({ scope: "array", value: arrayRange })
}
const containsRange = stringifyConstraintRange( const containsRange = stringifyConstraintRange(
"contained items", "contained items",
schema?.minContains, schema?.minContains,
schema?.maxContains schema?.maxContains
) )
if (containsRange !== null) constraints.push(containsRange) if (containsRange !== null) {
constraints.push({ scope: "array", value: containsRange })
}
// validation Keywords for Objects // validation Keywords for Objects
const objectRange = stringifyConstraintRange( const objectRange = stringifyConstraintRange(
@@ -291,14 +322,8 @@ export const stringifyConstraints = (schema) => {
schema?.minProperties, schema?.minProperties,
schema?.maxProperties schema?.maxProperties
) )
if (objectRange !== null) constraints.push(objectRange) if (objectRange !== null) {
constraints.push({ scope: "object", value: objectRange })
// a Vocabulary for the Contents of String-Encoded Data
if (schema?.contentMediaType) {
constraints.push(`media type: ${schema.contentMediaType}`)
}
if (schema?.contentEncoding) {
constraints.push(`encoding: ${schema.contentEncoding}`)
} }
return constraints return constraints

View File

@@ -35,7 +35,6 @@ import KeywordEnum from "./components/keywords/Enum/Enum"
import KeywordConst from "./components/keywords/Const" import KeywordConst from "./components/keywords/Const"
import KeywordConstraint from "./components/keywords/Constraint/Constraint" import KeywordConstraint from "./components/keywords/Constraint/Constraint"
import KeywordDependentRequired from "./components/keywords/DependentRequired/DependentRequired" import KeywordDependentRequired from "./components/keywords/DependentRequired/DependentRequired"
import KeywordFormat from "./components/keywords/Format/Format"
import KeywordContentSchema from "./components/keywords/ContentSchema" import KeywordContentSchema from "./components/keywords/ContentSchema"
import KeywordTitle from "./components/keywords/Title/Title" import KeywordTitle from "./components/keywords/Title/Title"
import KeywordDescription from "./components/keywords/Description/Description" import KeywordDescription from "./components/keywords/Description/Description"
@@ -90,7 +89,6 @@ export const withJSONSchemaContext = (Component, overrides = {}) => {
KeywordConst, KeywordConst,
KeywordConstraint, KeywordConstraint,
KeywordDependentRequired, KeywordDependentRequired,
KeywordFormat,
KeywordContentSchema, KeywordContentSchema,
KeywordTitle, KeywordTitle,
KeywordDescription, KeywordDescription,

View File

@@ -33,7 +33,6 @@ import KeywordEnum from "./components/keywords/Enum/Enum"
import KeywordConst from "./components/keywords/Const" import KeywordConst from "./components/keywords/Const"
import KeywordConstraint from "./components/keywords/Constraint/Constraint" import KeywordConstraint from "./components/keywords/Constraint/Constraint"
import KeywordDependentRequired from "./components/keywords/DependentRequired/DependentRequired" import KeywordDependentRequired from "./components/keywords/DependentRequired/DependentRequired"
import KeywordFormat from "./components/keywords/Format/Format"
import KeywordContentSchema from "./components/keywords/ContentSchema" import KeywordContentSchema from "./components/keywords/ContentSchema"
import KeywordTitle from "./components/keywords/Title/Title" import KeywordTitle from "./components/keywords/Title/Title"
import KeywordDescription from "./components/keywords/Description/Description" import KeywordDescription from "./components/keywords/Description/Description"
@@ -77,7 +76,6 @@ const JSONSchema202012Plugin = () => ({
JSONSchema202012KeywordConst: KeywordConst, JSONSchema202012KeywordConst: KeywordConst,
JSONSchema202012KeywordConstraint: KeywordConstraint, JSONSchema202012KeywordConstraint: KeywordConstraint,
JSONSchema202012KeywordDependentRequired: KeywordDependentRequired, JSONSchema202012KeywordDependentRequired: KeywordDependentRequired,
JSONSchema202012KeywordFormat: KeywordFormat,
JSONSchema202012KeywordContentSchema: KeywordContentSchema, JSONSchema202012KeywordContentSchema: KeywordContentSchema,
JSONSchema202012KeywordTitle: KeywordTitle, JSONSchema202012KeywordTitle: KeywordTitle,
JSONSchema202012KeywordDescription: KeywordDescription, JSONSchema202012KeywordDescription: KeywordDescription,

View File

@@ -62,7 +62,6 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => {
const KeywordDependentRequired = getComponent( const KeywordDependentRequired = getComponent(
"JSONSchema202012KeywordDependentRequired" "JSONSchema202012KeywordDependentRequired"
) )
const KeywordFormat = getComponent("JSONSchema202012KeywordFormat")
const KeywordContentSchema = getComponent( const KeywordContentSchema = getComponent(
"JSONSchema202012KeywordContentSchema" "JSONSchema202012KeywordContentSchema"
) )
@@ -114,7 +113,6 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => {
KeywordConst, KeywordConst,
KeywordConstraint, KeywordConstraint,
KeywordDependentRequired, KeywordDependentRequired,
KeywordFormat,
KeywordContentSchema, KeywordContentSchema,
KeywordTitle, KeywordTitle,
KeywordDescription, KeywordDescription,