Skip to content

Commit

Permalink
feat: dependentRequired
Browse files Browse the repository at this point in the history
  • Loading branch information
jy95 committed Jan 30, 2023
1 parent 1ec2abc commit b8192b1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from "react"

import Translate from "@docusaurus/Translate"

import type { JSONSchema, JSONSchemaNS } from "../../types"

type Props = {
schema: JSONSchema
[x: string]: any
}

function DependentRequired(props: Props): JSX.Element {
const { schema } = props

// Fast fail
if (typeof schema === "boolean") {
return <></>
}

let dependentRequired = (schema as JSONSchemaNS.Object)?.dependentRequired!

let items = Object.entries(dependentRequired).map(
([property1, property2]) => ({
id: property1,
label: (
<Translate
values={{
id: "json-schema.labels.dependentRequired",
ifProperty: property1,
count: property2.length,
otherProperty: property2.join(" "),
}}
>
{
"If {ifProperty} property is provided, then {count} propertie(s) must also be present: {otherProperty}"
}
</Translate>
),
})
)

return (
<ul>
{items.map((item) => (
<li key={item.id}>{item.label}</li>
))}
</ul>
)
}

export default DependentRequired
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export { default as IfElseThen } from "./if-else-then"

export { default as DependentRequired } from "./dependentRequired"

// main entry point
export { default as SchemaConditional } from "./schemaConditional"
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import React from "react"
import Translate from "@docusaurus/Translate"
//import Details from "@theme-original/Details";

import { IfElseThen } from "./index"
import { IfElseThen, DependentRequired } from "./index"
import { Collapsible } from "../../components/index"

import type { JSONSchema /*, JSONSchemaNS*/ } from "../../types"
import type { JSONSchema, JSONSchemaNS } from "../../types"

type Props = {
schema: JSONSchema
Expand All @@ -23,13 +23,13 @@ function SchemaConditional(props: Props): JSX.Element {

// Checks
const isIfThenElse = schema?.if !== undefined
/*

const isDependentRequired =
(schema as JSONSchemaNS.Object)?.dependentRequired !== undefined
const isDependentSchemas =
(schema as JSONSchemaNS.Object)?.dependentSchemas !== undefined
const isDependenncies = schema?.dependencies
*/
const isDependenncies = schema?.dependencies !== undefined

return (
<Collapsible
summary={
Expand All @@ -44,7 +44,11 @@ function SchemaConditional(props: Props): JSX.Element {
</strong>
}
>
{/* Handles if-then-else case */}
{isIfThenElse && <IfElseThen schema={schema} />}
{/* Handles dependentRequired case */}
{isDependentRequired && <DependentRequired schema={schema} />}
{/* Handles dependentSchemas case */}
</Collapsible>
)
}
Expand Down

0 comments on commit b8192b1

Please sign in to comment.