@@ -65,40 +65,43 @@ function isObject(value: any): boolean {
6565 * @param schema2 - The second schema to merge
6666 */
6767export function deepMergeSchemas < T extends Record < string , any > > ( schema1 : T , schema2 : T ) : void {
68- // Handle null/undefined
68+ // Handle null/undefined values
6969 if ( ! schema1 || ! schema2 ) {
7070 return
7171 }
7272
7373 // Handle non-objects
74- if ( typeof schema1 !== 'object' || typeof schema2 !== 'object' )
74+ if ( typeof schema1 !== 'object' || typeof schema2 !== 'object' ) {
7575 return
76+ }
7677
77- // Merge all properties from obj2 into obj1
78- for ( const [ key , value ] of Object . entries ( schema2 ) ) {
79- // let's skip merging conditionals such as if, oneOf, then ,else, allOf, anyOf, etc.
78+ // Merge all properties from schema2 into schema1
79+ for ( const [ key , schema2Value ] of Object . entries ( schema2 ) ) {
80+ // let's skip merging some properties
8081 if ( KEYS_TO_SKIP . includes ( key ) ) {
8182 continue
8283 }
8384
84- // If the value is an object, merge recursively
85- if ( isObject ( value ) ) {
86- // If both objects have this key and it's an object, merge recursively
87- if ( isObject ( schema1 [ key ] ) ) {
88- deepMergeSchemas ( schema1 [ key ] , value )
85+ const schema1Value = schema1 [ key ]
86+
87+ // If the value is an object:
88+ if ( isObject ( schema2Value ) ) {
89+ // If both schemas have this key and it's an object, merge recursively
90+ if ( isObject ( schema1Value ) ) {
91+ deepMergeSchemas ( schema1Value , schema2Value )
8992 }
90- // Otherwise, if the value is different, assign it
91- else if ( schema1 [ key ] !== value ) {
92- schema1 [ key as keyof T ] = value
93+ // Otherwise, if the value is different, just assign it
94+ else if ( schema1Value !== schema2Value ) {
95+ schema1 [ key as keyof T ] = schema2Value
9396 }
9497 }
9598 // If the value is an array, cycle through it and merge values if they're different (take objects into account)
96- else if ( schema1 [ key ] && Array . isArray ( value ) ) {
97- const originalArray = schema1 [ key ]
99+ else if ( schema1Value && Array . isArray ( schema2Value ) ) {
100+ const originalArray = schema1Value
98101 // If the destiny value exists and it's an array, cycle through the incoming values and merge if they're different (take objects into account)
99- for ( const item of value ) {
102+ for ( const item of schema2Value ) {
100103 if ( item && typeof item === 'object' ) {
101- deepMergeSchemas ( originalArray , value )
104+ deepMergeSchemas ( originalArray , schema2Value )
102105 }
103106 // "required" is a special case, it only allows for new elements to be added to the array
104107 else if ( key === 'required' ) {
@@ -109,12 +112,13 @@ export function deepMergeSchemas<T extends Record<string, any>>(schema1: T, sche
109112 }
110113 // Otherwise, just assign it
111114 else {
112- schema1 [ key as keyof T ] = value as T [ keyof T ]
115+ schema1 [ key as keyof T ] = schema2Value as T [ keyof T ]
113116 }
114117 }
115118 }
116- else if ( schema1 [ key ] !== value ) {
117- schema1 [ key as keyof T ] = value
119+ // Finally, if the value is different, just assign it
120+ else if ( schema1 [ key ] !== schema2Value ) {
121+ schema1 [ key as keyof T ] = schema2Value
118122 }
119123 }
120124}
0 commit comments