diff --git a/packages/conform-dom/formdata.ts b/packages/conform-dom/formdata.ts index 30aa5111..9c132265 100644 --- a/packages/conform-dom/formdata.ts +++ b/packages/conform-dom/formdata.ts @@ -128,11 +128,37 @@ export function setValue( : {} : valueFn(pointer[key]); - pointer[key] = newValue; + if (canAddProperty(pointer, key)) { + pointer[key] = newValue; + } pointer = pointer[key]; } } +function canAddProperty( + obj: Object | Array, + property: string | number, +): boolean { + const propertyStr = + typeof property === 'number' ? property.toString() : property; + if (Object.prototype.hasOwnProperty.call(obj, propertyStr)) { + const descriptor = Object.getOwnPropertyDescriptor(obj, propertyStr); + if (descriptor && !descriptor.writable) { + return false; + } + } + if (!Object.isExtensible(obj)) { + return false; + } + if (Object.isSealed(obj) && !(propertyStr in obj)) { + return false; + } + if (Object.isFrozen(obj) && !(propertyStr in obj)) { + return false; + } + return true; +} + /** * Retrive the value from a target object by following the paths */