@@ -3,7 +3,8 @@ import cn from 'classnames';
33import * as React from 'react' ;
44
55import { getNodeMetadata , metadataStore } from '../tree/metadata' ;
6- import { GoToRefHandler , SchemaTreeListNode } from '../types' ;
6+ import { GoToRefHandler , SchemaKind , SchemaTreeListNode } from '../types' ;
7+ import { getPrimaryType } from '../utils/getPrimaryType' ;
78import { Caret , Description , Divider , Property , Validations } from './shared' ;
89
910export interface ISchemaRow {
@@ -17,9 +18,29 @@ const ICON_SIZE = 12;
1718const ICON_DIMENSION = 20 ;
1819const ROW_OFFSET = 7 ;
1920
21+ function isRequired ( treeNode : SchemaTreeListNode ) {
22+ if ( treeNode . parent === null ) return false ;
23+ try {
24+ const { path } = getNodeMetadata ( treeNode ) ;
25+ if ( path . length === 0 ) {
26+ return false ;
27+ }
28+
29+ const { schema } = getNodeMetadata ( treeNode . parent ) ;
30+
31+ return (
32+ getPrimaryType ( schema ) === SchemaKind . Object &&
33+ Array . isArray ( schema . required ) &&
34+ schema . required . includes ( String ( path [ path . length - 1 ] ) )
35+ ) ;
36+ } catch {
37+ return false ;
38+ }
39+ }
40+
2041export const SchemaRow : React . FunctionComponent < ISchemaRow > = ( { className, node, rowOptions, onGoToRef } ) => {
2142 const metadata = getNodeMetadata ( node ) ;
22- const { path , schemaNode } = metadata ;
43+ const { schemaNode } = metadata ;
2344
2445 const parentSchemaNode = ( node . parent !== null && metadataStore . get ( node . parent ) ?. schemaNode ) || null ;
2546 const description = 'annotations' in schemaNode ? schemaNode . annotations . description : null ;
@@ -56,12 +77,7 @@ export const SchemaRow: React.FunctionComponent<ISchemaRow> = ({ className, node
5677 </ div >
5778
5879 < Validations
59- required = {
60- parentSchemaNode !== null &&
61- 'required' in parentSchemaNode &&
62- Array . isArray ( parentSchemaNode . required ) &&
63- parentSchemaNode . required . includes ( String ( path [ path . length - 1 ] ) )
64- }
80+ required = { isRequired ( node ) }
6581 validations = { {
6682 ...( 'annotations' in schemaNode &&
6783 schemaNode . annotations . default && { default : schemaNode . annotations . default } ) ,
0 commit comments