Skip to content

Commit e5313fc

Browse files
committed
Nullable nested objects couldn't be set with form when null.
Fixes #311
1 parent 271fc8f commit e5313fc

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ Headlines: Added, Changed, Deprecated, Removed, Fixed, Security
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Fixed
11+
12+
- Using full version of klona to address a cloning issue. ([#312](https://github.com/ciscoheat/sveltekit-superforms/pull/312))
13+
- Nullable nested objects couldn't be set with `$form` when `null`. ([#311](https://github.com/ciscoheat/sveltekit-superforms/pull/311))
14+
815
## [1.13.2] - 2024-01-07
916

1017
### Fixed

src/lib/traversal.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ export function traversePath<T extends object>(
9191
realPath: FieldPath<T>,
9292
modifier?: (data: PathData) => undefined | unknown | void
9393
): PathData | undefined {
94-
if (!realPath.length) return undefined;
95-
const path: FieldPath<T> = [realPath[0]];
94+
if (!realPath.length || !obj) return undefined;
9695

96+
const path: FieldPath<T> = [realPath[0]];
9797
let parent = obj;
9898

9999
while (path.length < realPath.length) {
@@ -111,11 +111,13 @@ export function traversePath<T extends object>(
111111
: parent[key];
112112

113113
if (value === undefined) return undefined;
114-
else parent = value as T; // TODO: Handle non-object values
114+
else parent = value as T;
115115

116116
path.push(realPath[path.length]);
117117
}
118118

119+
if (!parent) return undefined;
120+
119121
const key = realPath[realPath.length - 1];
120122
return {
121123
parent,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { superValidate } from '$lib/server';
2+
import { schema } from './schema';
3+
4+
import type { PageServerLoad } from './$types';
5+
6+
///// Load function /////
7+
8+
export const load: PageServerLoad = async () => {
9+
const form = await superValidate(schema);
10+
return { form };
11+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script lang="ts">
2+
import { onMount } from 'svelte';
3+
import { superForm } from '$lib/client';
4+
import SuperDebug from '$lib/client/SuperDebug.svelte';
5+
6+
export let data;
7+
8+
const { form } = superForm(data.form);
9+
10+
onMount(() => {
11+
$form.nested = { foo: 'bar' };
12+
});
13+
</script>
14+
15+
<SuperDebug data={$form} />
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
nested: z.any().nullable().default(null),
5+
});

0 commit comments

Comments
 (0)