Skip to content

Commit 78970c0

Browse files
committed
Prohibiting arbitrary data passed to superForm.
1 parent 0b46d3b commit 78970c0

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Changed
1111

12+
- It's not possible to send arbitrary data to `superForm` anymore, a `SuperValidated` structure is required, which is retured from `superValidate`, so in most cases this is not a problem.
1213
- Reverted that `message/setMessage` and `setError` will throw an error if status is lower than 400. Using a range error type check instead.
1314

1415
### Fixed

src/lib/client/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ export function superForm<
260260
// eslint-disable-next-line @typescript-eslint/no-explicit-any
261261
M = any
262262
>(
263-
form: z.infer<UnwrapEffects<T>> | SuperValidated<T, M>,
263+
form: SuperValidated<T, M>,
264264
options: FormOptions<UnwrapEffects<T>, M> = {}
265265
): SuperForm<UnwrapEffects<T>, M> {
266266
type UnwrappedT = UnwrapEffects<T>;
@@ -298,7 +298,7 @@ export function superForm<
298298
posted: false,
299299
errors: {},
300300
data: form ?? {},
301-
constraints: {} as SuperValidated<T, M>['constraints']
301+
constraints: {}
302302
};
303303
} else {
304304
if (_formId === undefined) _formId = form.id;
@@ -335,7 +335,7 @@ export function superForm<
335335
if (typeof initialForm.valid !== 'boolean') {
336336
throw new SuperFormError(
337337
'A non-validation object was passed to superForm. ' +
338-
"Check what's passed to its first parameter (null/undefined is allowed)."
338+
"Check what's passed to its first parameter."
339339
);
340340
}
341341

src/routes/spa/without-zod/+page.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
1818
console.log('Page loaded');
1919
20+
// @ts-expect-error Testing backwards compatibility of sending arbitrary data.
2021
const { form, errors, enhance, message } = superForm<Schema>(defaultData, {
2122
SPA: { failStatus: 401 },
2223
dataType: 'json',

src/routes/spa/zod/+page.svelte

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import { page } from '$app/stores';
3-
import { superForm } from '$lib/client';
3+
import { superForm, superValidateSync } from '$lib/client';
44
import SuperDebug from '$lib/client/SuperDebug.svelte';
55
import { schema } from './schema';
66
@@ -17,21 +17,24 @@
1717
1818
console.log('Page loaded');
1919
20-
const { form, errors, enhance, message } = superForm(defaultData, {
21-
SPA: true,
22-
dataType: 'json',
23-
onUpdate({ form, cancel }) {
24-
if ($page.url.searchParams.has('cancel')) cancel();
25-
else if (form.valid) {
26-
form.message = 'Successful!';
27-
form.data.random = String(Math.random()).slice(2);
28-
}
29-
},
30-
onUpdated({ form }) {
31-
console.log('onUpdated, valid:', form.valid);
32-
},
33-
validators: schema
34-
});
20+
const { form, errors, enhance, message } = superForm(
21+
superValidateSync(defaultData, schema),
22+
{
23+
SPA: true,
24+
dataType: 'json',
25+
onUpdate({ form, cancel }) {
26+
if ($page.url.searchParams.has('cancel')) cancel();
27+
else if (form.valid) {
28+
form.message = 'Successful!';
29+
form.data.random = String(Math.random()).slice(2);
30+
}
31+
},
32+
onUpdated({ form }) {
33+
console.log('onUpdated, valid:', form.valid);
34+
},
35+
validators: schema
36+
}
37+
);
3538
3639
// <SuperDebug data={{ $form, $errors }} />
3740
</script>

0 commit comments

Comments
 (0)