diff --git a/.changeset/fix-date-form-fields-set.md b/.changeset/fix-date-form-fields-set.md new file mode 100644 index 000000000000..b9715242d4f4 --- /dev/null +++ b/.changeset/fix-date-form-fields-set.md @@ -0,0 +1,5 @@ +--- +"@sveltejs/kit": patch +--- + +fix: properly handle Date objects in form.fields.set diff --git a/packages/kit/src/runtime/form-utils.js b/packages/kit/src/runtime/form-utils.js index 3c0c4139f136..8756527881e2 100644 --- a/packages/kit/src/runtime/form-utils.js +++ b/packages/kit/src/runtime/form-utils.js @@ -617,6 +617,10 @@ function get_type_prefix(field_type, is_array, input_value) { */ function deep_clone(value) { if (value !== null && typeof value === 'object') { + if (value instanceof Date) { + return new Date(value.getTime()); + } + if (value instanceof File) { return value; } diff --git a/packages/kit/src/runtime/form-utils.spec.js b/packages/kit/src/runtime/form-utils.spec.js index f2c93f55cd30..640cceaa3f06 100644 --- a/packages/kit/src/runtime/form-utils.spec.js +++ b/packages/kit/src/runtime/form-utils.spec.js @@ -2,6 +2,7 @@ import { describe, expect, test } from 'vitest'; import { BINARY_FORM_CONTENT_TYPE, convert_formdata, + create_field_proxy, deep_set, deserialize_binary_form, serialize_binary_form, @@ -745,3 +746,27 @@ describe('deep_set', () => { expect(target).toEqual({ nested: { name: 'hello' } }); }); }); + +describe('create_field_proxy', () => { + // Regression test for https://github.com/sveltejs/kit/issues/16165 + // Before the fix, Date values fell through to the generic object branch + // of deep_clone, which iterated Object.keys (empty on Date), producing + // an empty plain object {} instead of a Date. + test('value() returns a Date clone for Date fields', () => { + const original = new Date('2025-06-25T00:00:00Z'); + const input = { created_at: original }; + + const proxy = create_field_proxy( + {}, + () => input, + () => {}, + () => ({}) + ); + + const cloned = proxy.created_at.value(); + + expect(cloned).toBeInstanceOf(Date); + expect(cloned.getTime()).toBe(original.getTime()); + expect(cloned).not.toBe(original); + }); +});