1- import { get } from "lodash-es" ;
2-
1+ import { get , cloneDeep } from "lodash-es" ;
2+ import { resolveReference } from "@exodus/schemasafe/src/pointer" ;
3+
34export const upTo = ( str : string , match : string , start ?: number ) => {
45 const pos = str . indexOf ( match , start ) ;
56 return pos < 0 ? str . substring ( start || 0 ) : str . substring ( start || 0 , pos ) ;
@@ -39,30 +40,34 @@ export function camelToTitle(camel: string): string {
3940 return camelToWords ( camel ) . replace ( / [ a - z ] / i, ( ltr ) => ltr . toUpperCase ( ) )
4041}
4142
43+ export function resolveRefs ( root : Record < string , unknown > , schema : Record < string , unknown > ) : Record < string , unknown > {
44+ return schema [ '$ref' ]
45+ ? resolveReference ( root , new Set ( Object . keys ( root . definitions as Record < string , unknown > [ ] ) ) , schema [ '$ref' ] as string ) [ 0 ] [ 0 ]
46+ : cloneDeep ( schema )
47+ }
48+
4249/** manipulate the schema to allow any optional property to have a null value
4350 * which is appropriate for form input */
44- export function nullOptionalsAllowed ( schema : object ) : object {
51+ export function nullOptionalsAllowed ( schema : Record < string , unknown > ) : object {
4552 if ( schema === null || schema === undefined ) schema = { } ;
46- let newSchema = deepCopy ( schema ) ;
47- nullOptionalsAllowedApply ( newSchema as Record < string , unknown > ) ;
48- return newSchema ;
53+ return nullOptionalsAllowedApply ( schema , schema ) ;
4954}
5055
51- function nullOptionalsAllowedApply ( schema : Record < string , unknown > ) {
56+ function nullOptionalsAllowedApply ( root : Record < string , unknown > , unresolvedSchema : Record < string , unknown > ) : Record < string , unknown > {
57+ const schema = resolveRefs ( root , unresolvedSchema )
5258 let req = ( schema [ 'required' ] || [ ] ) as Array < string > ;
53- if ( schema [ '$ref' ] ) return ;
5459 switch ( schema [ 'type' ] ) {
5560 case 'object' :
5661 const properties = ( schema [ 'properties' ] || { } ) as Record < string , unknown > ;
5762 for ( let prop in properties ) {
5863 if ( req . indexOf ( prop ) < 0 ) {
59- nullOptionalsAllowedApply ( properties [ prop ] as Record < string , unknown > ) ;
64+ properties [ prop ] = nullOptionalsAllowedApply ( root , properties [ prop ] as Record < string , unknown > ) ;
6065 }
6166 }
6267 break ;
6368 case 'array' :
6469 const items = ( schema [ 'items' ] || { } ) as Record < string , unknown > ;
65- nullOptionalsAllowedApply ( items ) ;
70+ schema [ 'items' ] = nullOptionalsAllowedApply ( root , items ) ;
6671 if ( items [ 'oneOf' ] && ! ( items [ 'oneOf' ] as any [ ] ) . some ( subschema => subschema [ "type" ] == "null" ) ) {
6772 ( items [ 'oneOf' ] as any [ ] ) . push ( { type : 'null' } ) ;
6873 }
@@ -80,44 +85,10 @@ function nullOptionalsAllowedApply(schema: Record<string, unknown>) {
8085 const defns = schema [ 'definitions' ] as Record < string , unknown > ;
8186 if ( defns ) {
8287 for ( let defn in defns ) {
83- nullOptionalsAllowedApply ( defns [ defn ] as Record < string , unknown > ) ;
88+ defns [ defn ] = nullOptionalsAllowedApply ( root , defns [ defn ] as Record < string , unknown > ) ;
8489 }
8590 }
86- }
87-
88- export function deepCopy ( obj : object ) : object {
89- var copy ;
90-
91- // Handle the 3 simple types, and null or undefined
92- if ( null == obj || "object" != typeof obj ) return obj ;
93-
94- // Handle Date
95- if ( obj instanceof Date ) {
96- copy = new Date ( ) ;
97- copy . setTime ( obj . getTime ( ) ) ;
98- return copy ;
99- }
100-
101- // Handle Array
102- if ( obj instanceof Array ) {
103- copy = [ ] ;
104- for ( var i = 0 , len = obj . length ; i < len ; i ++ ) {
105- copy [ i ] = deepCopy ( obj [ i ] ) ;
106- }
107- return copy ;
108- }
109-
110- // Handle Object
111- if ( obj instanceof Object ) {
112- copy = { } as Record < string , unknown > ;
113- const recObj = obj as Record < string , unknown > ;
114- for ( var attr in recObj ) {
115- if ( recObj . hasOwnProperty ( attr ) ) copy [ attr ] = deepCopy ( recObj [ attr ] as object ) ;
116- }
117- return copy ;
118- }
119-
120- throw new Error ( "Unable to copy obj! Its type isn't supported." ) ;
91+ return schema ;
12192}
12293
12394let incrVal = 0 ;
0 commit comments