Skip to content

Commit 72acae7

Browse files
committed
Type error with formFieldProxy when using a strongly typed status message.
Fixes #260.
1 parent 1561997 commit 72acae7

File tree

5 files changed

+136
-1
lines changed

5 files changed

+136
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ 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] - 2023-09-11
9+
10+
### Fixed
11+
12+
- Type error with `formFieldProxy` when using a strongly typed status message. ([#260](https://github.com/ciscoheat/sveltekit-superforms/issues/260))
13+
814
## [1.6.1] - 2023-08-22
915

1016
### Fixed

src/lib/client/proxies.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ export function formFieldProxy<
237237
T extends ZodValidation<AnyZodObject>,
238238
Path extends FormPathLeaves<z.infer<UnwrapEffects<T>>>
239239
>(
240-
form: SuperForm<T, unknown>,
240+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
241+
form: SuperForm<T, any>,
241242
path: Path
242243
): {
243244
path: Path;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { fail } from '@sveltejs/kit';
2+
import { message, superValidate } from '$lib/server';
3+
import type { Actions, PageServerLoad } from './$types';
4+
import { schema, type Message } from './utils';
5+
6+
///// Load function /////
7+
8+
export const load: PageServerLoad = async () => {
9+
const form = await superValidate<typeof schema, Message>(schema);
10+
return { form };
11+
};
12+
13+
///// Form actions /////
14+
15+
export const actions: Actions = {
16+
default: async ({ request }) => {
17+
const form = await superValidate<typeof schema, Message>(
18+
request,
19+
schema
20+
);
21+
22+
console.log('POST', form);
23+
24+
if (!form.valid) return fail(400, { form });
25+
26+
return message(form, {
27+
type: 'success',
28+
text: 'Form posted successfully!'
29+
});
30+
}
31+
};
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<script lang="ts">
2+
import { page } from '$app/stores';
3+
import { formFieldProxy, superForm, type SuperForm } from '$lib/client';
4+
import SuperDebug from '$lib/client/SuperDebug.svelte';
5+
import type { schema } from './utils';
6+
7+
export let data;
8+
9+
const formData = superForm(data.form);
10+
const { message, enhance } = formData;
11+
12+
const { errors, value } = formFieldProxy(formData, 'name');
13+
// This type cast should not be necessary, it should infer the type SuperForm<typeof schema, Message>
14+
//const x = formFieldProxy(formData as SuperForm<typeof schema, unknown>, 'name');
15+
</script>
16+
17+
<SuperDebug data={formData.form} />
18+
19+
<h3>Superforms testing ground</h3>
20+
21+
{#if $message}
22+
<div
23+
class="status"
24+
class:error={$page.status >= 400}
25+
class:success={$page.status == 200}
26+
>
27+
{$message.type}: {$message.text}
28+
</div>
29+
{/if}
30+
31+
<form method="POST" use:enhance>
32+
<label>
33+
Name<br />
34+
<input
35+
name="name"
36+
aria-invalid={$errors ? 'true' : undefined}
37+
bind:value={$value}
38+
/>
39+
{#if $errors}<span class="invalid">{$errors}</span>{/if}
40+
</label>
41+
42+
<button>Submit</button>
43+
</form>
44+
45+
<hr />
46+
<p>
47+
<a target="_blank" href="https://superforms.rocks/api">API Reference</a>
48+
</p>
49+
50+
<style>
51+
.invalid {
52+
color: red;
53+
}
54+
55+
.status {
56+
color: white;
57+
padding: 4px;
58+
padding-left: 8px;
59+
border-radius: 2px;
60+
font-weight: 500;
61+
}
62+
63+
.status.success {
64+
background-color: seagreen;
65+
}
66+
67+
.status.error {
68+
background-color: #ff2a02;
69+
}
70+
71+
input {
72+
background-color: #ddd;
73+
}
74+
75+
a {
76+
text-decoration: underline;
77+
}
78+
79+
hr {
80+
margin-top: 4rem;
81+
}
82+
83+
form {
84+
padding-top: 1rem;
85+
padding-bottom: 1rem;
86+
}
87+
</style>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
name: z.string().min(1)
5+
});
6+
7+
export type Message = {
8+
type: 'success' | 'error';
9+
text: string;
10+
};

0 commit comments

Comments
 (0)