-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from ethdebug/convention-cleanup
Cleanup schema explorer override code for polymorphic discriminators
- Loading branch information
Showing
6 changed files
with
179 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
...b/src/theme/JSONSchemaViewer/JSONSchemaElements/schemaComposition/DiscriminatorSchema.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import React from 'react'; | ||
import type { JSONSchema } from "json-schema-typed/draft-2020-12" | ||
import Tabs from "@theme/Tabs"; | ||
import TabItem from "@theme/TabItem"; | ||
|
||
import { | ||
SchemaHierarchyContextProvider, | ||
useSchemaHierarchyContext, | ||
} from "@theme-original/JSONSchemaViewer/contexts" | ||
|
||
import { CreateNodes } from "@theme-original/JSONSchemaViewer/components" | ||
|
||
export interface Discriminator { | ||
propertyName: string; | ||
schemasByConst: { | ||
[value: string]: { | ||
schema: JSONSchema; | ||
index: number; | ||
} | ||
} | ||
} | ||
|
||
export interface DiscriminatorSchemaProps extends Discriminator { | ||
} | ||
|
||
export default function DiscriminatorSchema({ | ||
propertyName, | ||
schemasByConst | ||
}: DiscriminatorSchemaProps): JSX.Element { | ||
const { jsonPointer: currentJsonPointer, level: currentLevel } = | ||
useSchemaHierarchyContext() | ||
|
||
return ( | ||
<div> | ||
<hr /> | ||
<span className="badge badge--info">polymorphic discriminator</span> | ||
The value of the <strong>{propertyName}</strong> field | ||
determines which sub-schema applies: | ||
|
||
<Tabs>{ | ||
Object.entries(schemasByConst) | ||
.map(([value, { schema, index }]) => ( | ||
<TabItem | ||
key={value} | ||
label={value} | ||
value={value} | ||
> | ||
<SchemaHierarchyContextProvider | ||
value={{ | ||
level: currentLevel + 1, | ||
jsonPointer: `${currentJsonPointer}/allOf/${index}/then`, | ||
}} | ||
> | ||
<CreateNodes schema={schema} /> | ||
</SchemaHierarchyContextProvider> | ||
</TabItem> | ||
)) | ||
}</Tabs> | ||
</div> | ||
); | ||
} | ||
|
||
export function detectDiscriminator(schema: { | ||
allOf: JSONSchema[] | ||
}): Discriminator | undefined { | ||
const { allOf } = schema; | ||
|
||
const allIfThen = allOf.every( | ||
(clause: JSONSchema): clause is { "if": JSONSchema; then: JSONSchema } => { | ||
if (typeof clause === "boolean") { | ||
return false; | ||
} | ||
|
||
const { title, description, "if": if_, then, ...others } = clause; | ||
|
||
return !!if_ && !!then && Object.keys(others).length === 0; | ||
} | ||
) | ||
|
||
if (!allIfThen) { | ||
return; | ||
} | ||
|
||
const allIfsHaveSinglePropertyWithConst = allOf.every( | ||
(ifThen: { "if": JSONSchema; then: JSONSchema }): ifThen is { | ||
"if": { | ||
properties: { | ||
[propertyName: string]: { | ||
"const": string | ||
} | ||
} | ||
}; | ||
then: JSONSchema; | ||
} => { | ||
const { "if": if_ } = ifThen; | ||
|
||
if ( | ||
typeof if_ === "boolean" || | ||
!("properties" in if_) || | ||
!if_.properties | ||
) { | ||
return false; | ||
} | ||
|
||
const ifProperties = if_.properties; | ||
|
||
if (Object.keys(ifProperties).length !== 1) { | ||
return false; | ||
} | ||
|
||
const propertyName = Object.keys(ifProperties)[0]; | ||
const propertySchema = ifProperties[propertyName]; | ||
|
||
return ( | ||
typeof propertySchema === "object" && | ||
"const" in propertySchema && | ||
typeof propertySchema.const === "string" && | ||
!!propertySchema.const | ||
) ; | ||
} | ||
); | ||
|
||
if (!allIfsHaveSinglePropertyWithConst) { | ||
return; | ||
} | ||
|
||
const propertyName = Object.keys(allOf[0]["if"].properties)[0]; | ||
|
||
const schemasByConst = allOf | ||
.map(({ "if": if_, then }, index) => { | ||
const value = if_.properties[propertyName]["const"]; | ||
|
||
return { | ||
[value]: { | ||
schema: then, | ||
index | ||
} | ||
}; | ||
}) | ||
.reduce((a, b) => ({ ...a, ...b }), {}); | ||
|
||
|
||
const isUniquelyDiscriminating = | ||
Object.keys(schemasByConst).length === allOf.length; | ||
|
||
if (!isUniquelyDiscriminating) { | ||
return; | ||
} | ||
|
||
return { | ||
propertyName, | ||
schemasByConst | ||
}; | ||
} |
146 changes: 12 additions & 134 deletions
146
packages/web/src/theme/JSONSchemaViewer/JSONSchemaElements/schemaComposition/allOfSchema.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,147 +1,25 @@ | ||
import React from 'react'; | ||
import type { JSONSchema } from "json-schema-typed/draft-2020-12" | ||
import AllOfSchema from '@theme-original/JSONSchemaViewer/JSONSchemaElements/schemaComposition/allOfSchema'; | ||
import Tabs from "@theme/Tabs"; | ||
import TabItem from "@theme/TabItem"; | ||
|
||
import { CreateNodes } from "@theme-original/JSONSchemaViewer/components" | ||
import { | ||
SchemaHierarchyContextProvider, | ||
useSchemaHierarchyContext, | ||
} from "@theme-original/JSONSchemaViewer/contexts" | ||
import { Collapsible } from "@theme-original/JSONSchemaViewer/components"; | ||
import DiscriminatorSchema, { | ||
detectDiscriminator | ||
} from "./DiscriminatorSchema"; | ||
|
||
export default function allOfSchemaWrapper(props) { | ||
export default function allOfSchemaWrapper(props: { | ||
schema: Exclude<JSONSchema, boolean> & { allOf: JSONSchema[]; } | ||
}) { | ||
const { schema } = props; | ||
const { jsonPointer: currentJsonPointer, level: currentLevel } = | ||
useSchemaHierarchyContext() | ||
|
||
const discriminator = detectDiscriminator(schema); | ||
if (!discriminator) { | ||
return ( | ||
<> | ||
<AllOfSchema {...props} /> | ||
</> | ||
); | ||
if (discriminator) { | ||
return <DiscriminatorSchema {...discriminator} /> | ||
} | ||
|
||
const { propertyName, schemasByConst } = discriminator; | ||
|
||
return ( | ||
<div> | ||
<hr /> | ||
<span className="badge badge--info">polymorphic discriminator</span> | ||
The value of the <strong>{propertyName}</strong> field | ||
determines which sub-schema applies: | ||
|
||
<Tabs>{ | ||
Object.entries(schemasByConst) | ||
.map(([value, { schema, index }]) => ( | ||
<TabItem | ||
key={value} | ||
label={value} | ||
value={value} | ||
> | ||
<SchemaHierarchyContextProvider | ||
value={{ | ||
level: currentLevel + 1, | ||
jsonPointer: `${currentJsonPointer}/allOf/${index}`, | ||
}} | ||
> | ||
<CreateNodes schema={schema} /> | ||
</SchemaHierarchyContextProvider> | ||
</TabItem> | ||
)) | ||
}</Tabs> | ||
</div> | ||
<> | ||
<AllOfSchema {...props} /> | ||
</> | ||
); | ||
} | ||
|
||
function detectDiscriminator(schema: { | ||
allOf: any[] | ||
}): { | ||
propertyName: string; | ||
schemasByConst: { | ||
[value: string]: { | ||
schema: object; | ||
index: number; | ||
} | ||
} | ||
} | undefined { | ||
const { allOf } = schema; | ||
|
||
const allIfThen = allOf.every( | ||
(clause: any): clause is { "if": any; then: any } => { | ||
const { title, description, "if": if_, then, ...others } = clause; | ||
|
||
return if_ && then && Object.keys(others).length === 0; | ||
} | ||
) | ||
|
||
if (!allIfThen) { | ||
return; | ||
} | ||
|
||
const allIfsHaveSinglePropertyWithConst = allOf.every( | ||
(ifThen: { "if": any; then: any }): ifThen is { | ||
"if": { | ||
properties: { | ||
[propertyName: string]: { | ||
"const": string | ||
} | ||
} | ||
}; | ||
then: any; | ||
} => { | ||
const { "if": if_ } = ifThen; | ||
|
||
if (!("properties" in if_)) { | ||
return false; | ||
} | ||
|
||
const ifProperties = if_.properties; | ||
|
||
if (Object.keys(ifProperties).length !== 1) { | ||
return false; | ||
} | ||
|
||
const propertyName = Object.keys(ifProperties)[0]; | ||
|
||
const { "const": const_ } = ifProperties[propertyName]; | ||
|
||
return typeof const_ === "string" && !!const_; | ||
} | ||
); | ||
|
||
if (!allIfsHaveSinglePropertyWithConst) { | ||
return; | ||
} | ||
|
||
const propertyName = Object.keys(allOf[0]["if"].properties)[0]; | ||
|
||
const schemasByConst = allOf | ||
.map(({ "if": if_, then }, index) => { | ||
const value = if_.properties[propertyName]["const"]; | ||
|
||
return { | ||
[value]: { | ||
schema: then, | ||
index | ||
} | ||
}; | ||
}) | ||
.reduce((a, b) => ({ ...a, ...b }), {}); | ||
|
||
|
||
const isUniquelyDiscriminating = | ||
Object.keys(schemasByConst).length === allOf.length; | ||
|
||
if (!isUniquelyDiscriminating) { | ||
return; | ||
} | ||
|
||
return { | ||
propertyName, | ||
schemasByConst | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters