@@ -218,23 +218,54 @@ const DynamicJsonForm = ({
218
218
const updateArray = ( array : JsonValue [ ] , path : string [ ] , value : JsonValue ) : JsonValue [ ] => {
219
219
const [ index , ...restPath ] = path ;
220
220
const arrayIndex = Number ( index ) ;
221
+
222
+ // Validate array index
223
+ if ( isNaN ( arrayIndex ) ) {
224
+ console . error ( `Invalid array index: ${ index } ` ) ;
225
+ return array ;
226
+ }
227
+
228
+ // Check array bounds
229
+ if ( arrayIndex < 0 ) {
230
+ console . error ( `Array index out of bounds: ${ arrayIndex } < 0` ) ;
231
+ return array ;
232
+ }
233
+
221
234
const newArray = [ ...array ] ;
222
235
223
236
if ( restPath . length === 0 ) {
224
237
newArray [ arrayIndex ] = value ;
225
238
} else {
239
+ // Ensure index position exists
240
+ if ( arrayIndex >= array . length ) {
241
+ console . warn ( `Extending array to index ${ arrayIndex } ` ) ;
242
+ newArray . length = arrayIndex + 1 ;
243
+ newArray . fill ( null , array . length , arrayIndex ) ;
244
+ }
226
245
newArray [ arrayIndex ] = updateValue ( newArray [ arrayIndex ] , restPath , value ) ;
227
246
}
228
247
return newArray ;
229
248
} ;
230
249
231
250
const updateObject = ( obj : JsonObject , path : string [ ] , value : JsonValue ) : JsonObject => {
232
251
const [ key , ...restPath ] = path ;
252
+
253
+ // Validate object key
254
+ if ( typeof key !== 'string' ) {
255
+ console . error ( `Invalid object key: ${ key } ` ) ;
256
+ return obj ;
257
+ }
258
+
233
259
const newObj = { ...obj } ;
234
260
235
261
if ( restPath . length === 0 ) {
236
262
newObj [ key ] = value ;
237
263
} else {
264
+ // Ensure key exists
265
+ if ( ! ( key in newObj ) ) {
266
+ console . warn ( `Creating new key in object: ${ key } ` ) ;
267
+ newObj [ key ] = { } ;
268
+ }
238
269
newObj [ key ] = updateValue ( newObj [ key ] , restPath , value ) ;
239
270
}
240
271
return newObj ;
@@ -243,20 +274,34 @@ const DynamicJsonForm = ({
243
274
const updateValue = ( current : JsonValue , path : string [ ] , value : JsonValue ) : JsonValue => {
244
275
if ( path . length === 0 ) return value ;
245
276
246
- if ( ! current ) {
247
- current = ! isNaN ( Number ( path [ 0 ] ) ) ? [ ] : { } ;
248
- }
277
+ try {
278
+ if ( ! current ) {
279
+ current = ! isNaN ( Number ( path [ 0 ] ) ) ? [ ] : { } ;
280
+ }
249
281
250
- if ( Array . isArray ( current ) ) {
251
- return updateArray ( current , path , value ) ;
252
- } else if ( typeof current === 'object' && current !== null ) {
253
- return updateObject ( current , path , value ) ;
282
+ // Type checking
283
+ if ( Array . isArray ( current ) ) {
284
+ return updateArray ( current , path , value ) ;
285
+ } else if ( typeof current === 'object' && current !== null ) {
286
+ return updateObject ( current , path , value ) ;
287
+ } else {
288
+ console . error ( `Cannot update path ${ path . join ( '.' ) } in non-object/array value:` , current ) ;
289
+ return current ;
290
+ }
291
+ } catch ( error ) {
292
+ console . error ( `Error updating value at path ${ path . join ( '.' ) } :` , error ) ;
293
+ return current ;
254
294
}
255
-
256
- return current ;
257
295
} ;
258
296
259
- onChange ( updateValue ( value , path , fieldValue ) ) ;
297
+ try {
298
+ const newValue = updateValue ( value , path , fieldValue ) ;
299
+ onChange ( newValue ) ;
300
+ } catch ( error ) {
301
+ console . error ( 'Failed to update form value:' , error ) ;
302
+ // Keep the original value unchanged
303
+ onChange ( value ) ;
304
+ }
260
305
} ;
261
306
262
307
return (
0 commit comments