Skip to content

Commit

Permalink
feat: json pointer for schema conditionals & schema composition
Browse files Browse the repository at this point in the history
  • Loading branch information
jy95 committed Dec 31, 2023
1 parent 3ee7ccc commit a886362
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import TabItem from "@theme-original/TabItem"
import Tabs from "@theme-original/Tabs"

import { CreateNodes } from "@theme/JSONSchemaViewer/components"
import {
SchemaHierarchyContextProvider,
useSchemaHierarchyContext,
} from "@theme/JSONSchemaViewer/contexts"

import { IfLabel, ThenLabel, ElseLabel } from "@theme/JSONSchemaViewer/labels"

Expand All @@ -16,6 +20,8 @@ type Props = {

// Handle if else then
export default function IfElseThen(props: Props): JSX.Element {
const { jsonPointer: currentJsonPointer, level: currentLevel } =
useSchemaHierarchyContext()
const { schema } = props

const hasThen = schema.then !== undefined
Expand Down Expand Up @@ -47,11 +53,38 @@ export default function IfElseThen(props: Props): JSX.Element {
) {
switch (value) {
case "schema_if":
return <CreateNodes schema={schema.if!} />
return (
<SchemaHierarchyContextProvider
value={{
level: currentLevel + 1,
jsonPointer: `${currentJsonPointer}/if`,
}}
>
<CreateNodes schema={schema.if!} />
</SchemaHierarchyContextProvider>
)
case "schema_then":
return <CreateNodes schema={schema.then!} />
return (
<SchemaHierarchyContextProvider
value={{
level: currentLevel + 1,
jsonPointer: `${currentJsonPointer}/then`,
}}
>
<CreateNodes schema={schema.then!} />
</SchemaHierarchyContextProvider>
)
case "schema_else":
return <CreateNodes schema={schema.else!} />
return (
<SchemaHierarchyContextProvider
value={{
level: currentLevel + 1,
jsonPointer: `${currentJsonPointer}/else`,
}}
>
<CreateNodes schema={schema.else!} />
</SchemaHierarchyContextProvider>
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import TabItem from "@theme-original/TabItem"
import Tabs from "@theme-original/Tabs"

import { CreateNodes } from "@theme/JSONSchemaViewer/components"
import {
SchemaHierarchyContextProvider,
useSchemaHierarchyContext,
} from "@theme/JSONSchemaViewer/contexts"

import { GenerateFriendlyName } from "@theme/JSONSchemaViewer/utils"

Expand All @@ -16,6 +20,8 @@ type Props = {
}

export default function AllOfSchema(props: Props): JSX.Element {
const { jsonPointer: currentJsonPointer, level: currentLevel } =
useSchemaHierarchyContext()
const { schema } = props

let typedSchema = schema.allOf!
Expand All @@ -33,7 +39,14 @@ export default function AllOfSchema(props: Props): JSX.Element {
value={`schema_${typeOf}_${index}`}
label={<GenerateFriendlyName schema={compositeSchema} />}
>
<CreateNodes schema={compositeSchema} />
<SchemaHierarchyContextProvider
value={{
level: currentLevel + 1,
jsonPointer: `${currentJsonPointer}/allOf/${index}`,
}}
>
<CreateNodes schema={compositeSchema} />
</SchemaHierarchyContextProvider>
</TabItem>
)
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import TabItem from "@theme-original/TabItem"
import Tabs from "@theme-original/Tabs"

import { CreateNodes } from "@theme/JSONSchemaViewer/components"
import {
SchemaHierarchyContextProvider,
useSchemaHierarchyContext,
} from "@theme/JSONSchemaViewer/contexts"

import { GenerateFriendlyName } from "@theme/JSONSchemaViewer/utils"

Expand All @@ -16,6 +20,8 @@ type Props = {
}

export default function AnyOfSchema(props: Props): JSX.Element {
const { jsonPointer: currentJsonPointer, level: currentLevel } =
useSchemaHierarchyContext()
const { schema } = props

let typedSchema = schema.anyOf!
Expand All @@ -33,7 +39,14 @@ export default function AnyOfSchema(props: Props): JSX.Element {
value={`schema_${typeOf}_${index}`}
label={<GenerateFriendlyName schema={compositeSchema} />}
>
<CreateNodes schema={compositeSchema} />
<SchemaHierarchyContextProvider
value={{
level: currentLevel + 1,
jsonPointer: `${currentJsonPointer}/anyOf/${index}`,
}}
>
<CreateNodes schema={compositeSchema} />
</SchemaHierarchyContextProvider>
</TabItem>
)
})}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import React from "react"

import { CreateNodes } from "@theme/JSONSchemaViewer/components"
import {
SchemaHierarchyContextProvider,
useSchemaHierarchyContext,
} from "@theme/JSONSchemaViewer/contexts"

import type { JSONSchema } from "@theme/JSONSchemaViewer/types"

Expand All @@ -10,6 +14,8 @@ type Props = {
}

export default function NotSchema(props: Props): JSX.Element {
const { jsonPointer: currentJsonPointer, level: currentLevel } =
useSchemaHierarchyContext()
const { schema } = props

let typedSchema = schema.not!
Expand All @@ -19,7 +25,14 @@ export default function NotSchema(props: Props): JSX.Element {
<div>
<span className="badge badge--danger">{typeOf}</span>
<br />
<CreateNodes schema={typedSchema} />
<SchemaHierarchyContextProvider
value={{
level: currentLevel + 1,
jsonPointer: `${currentJsonPointer}/not`,
}}
>
<CreateNodes schema={typedSchema} />
</SchemaHierarchyContextProvider>
</div>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import TabItem from "@theme-original/TabItem"
import Tabs from "@theme-original/Tabs"

import { CreateNodes } from "@theme/JSONSchemaViewer/components"
import {
SchemaHierarchyContextProvider,
useSchemaHierarchyContext,
} from "@theme/JSONSchemaViewer/contexts"

import { GenerateFriendlyName } from "@theme/JSONSchemaViewer/utils"

Expand All @@ -16,6 +20,8 @@ type Props = {
}

export default function OneOfSchema(props: Props): JSX.Element {
const { jsonPointer: currentJsonPointer, level: currentLevel } =
useSchemaHierarchyContext()
const { schema } = props

let typedSchema = schema.oneOf!
Expand All @@ -32,7 +38,14 @@ export default function OneOfSchema(props: Props): JSX.Element {
value={`schema_${typeOf}_${index}`}
label={<GenerateFriendlyName schema={compositeSchema} />}
>
<CreateNodes schema={compositeSchema} />
<SchemaHierarchyContextProvider
value={{
level: currentLevel + 1,
jsonPointer: `${currentJsonPointer}/oneOf/${index}`,
}}
>
<CreateNodes schema={compositeSchema} />
</SchemaHierarchyContextProvider>
</TabItem>
)
})}
Expand Down

0 comments on commit a886362

Please sign in to comment.