Skip to content

Commit ca5bbc6

Browse files
committed
Fixed FormPath types
1 parent 1f06f98 commit ca5bbc6

File tree

8 files changed

+84
-15
lines changed

8 files changed

+84
-15
lines changed

src/lib/client/proxies.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export type ProxyOptions = {
1111
taint?: TaintOption;
1212
};
1313

14-
type CorrectProxyType<In, Out, T extends Record<string, unknown>, Path extends FormPath<T>> =
14+
type FormPaths<T extends Record<string, unknown>> = FormPath<T> | FormPathLeaves<T>;
15+
16+
type CorrectProxyType<In, Out, T extends Record<string, unknown>, Path extends FormPaths<T>> =
1517
NonNullable<FormPathType<T, Path>> extends In ? Writable<Out> : never;
1618

1719
type DefaultOptions = {
@@ -40,7 +42,7 @@ const defaultOptions = {
4042

4143
///// Proxy functions ///////////////////////////////////////////////
4244

43-
export function booleanProxy<T extends Record<string, unknown>, Path extends FormPath<T>>(
45+
export function booleanProxy<T extends Record<string, unknown>, Path extends FormPaths<T>>(
4446
form: Writable<T> | SuperForm<T, unknown>,
4547
path: Path,
4648
options?: Prettify<Pick<DefaultOptions, 'trueStringValue' | 'taint'>>
@@ -51,7 +53,7 @@ export function booleanProxy<T extends Record<string, unknown>, Path extends For
5153
}) as CorrectProxyType<boolean, string, T, Path>;
5254
}
5355

54-
export function intProxy<T extends Record<string, unknown>, Path extends FormPath<T>>(
56+
export function intProxy<T extends Record<string, unknown>, Path extends FormPaths<T>>(
5557
form: Writable<T> | SuperForm<T, unknown>,
5658
path: Path,
5759
options?: Prettify<Pick<DefaultOptions, 'empty' | 'initiallyEmptyIfZero' | 'taint'>>
@@ -62,7 +64,7 @@ export function intProxy<T extends Record<string, unknown>, Path extends FormPat
6264
}) as CorrectProxyType<number, string, T, Path>;
6365
}
6466

65-
export function numberProxy<T extends Record<string, unknown>, Path extends FormPath<T>>(
67+
export function numberProxy<T extends Record<string, unknown>, Path extends FormPaths<T>>(
6668
form: Writable<T> | SuperForm<T, unknown>,
6769
path: Path,
6870
options?: Prettify<Pick<DefaultOptions, 'empty' | 'delimiter' | 'initiallyEmptyIfZero' | 'taint'>>
@@ -73,7 +75,7 @@ export function numberProxy<T extends Record<string, unknown>, Path extends Form
7375
}) as CorrectProxyType<number, string, T, Path>;
7476
}
7577

76-
export function dateProxy<T extends Record<string, unknown>, Path extends FormPath<T>>(
78+
export function dateProxy<T extends Record<string, unknown>, Path extends FormPaths<T>>(
7779
form: Writable<T> | SuperForm<T, unknown>,
7880
path: Path,
7981
options?: {
@@ -111,7 +113,7 @@ export function stringProxy<T extends Record<string, unknown>, Path extends Form
111113
* @param field Form field
112114
* @param type 'number' | 'int' | 'boolean'
113115
*/
114-
function _stringProxy<T extends Record<string, unknown>, Path extends FormPath<T>>(
116+
function _stringProxy<T extends Record<string, unknown>, Path extends FormPaths<T>>(
115117
form: Writable<T> | SuperForm<T, unknown>,
116118
path: Path,
117119
type: 'number' | 'int' | 'boolean' | 'date' | 'string',
@@ -460,7 +462,7 @@ function isSuperForm<T extends Record<string, unknown>>(
460462

461463
export type FieldProxy<T> = Writable<T>;
462464

463-
export function fieldProxy<T extends Record<string, unknown>, Path extends FormPath<T>>(
465+
export function fieldProxy<T extends Record<string, unknown>, Path extends FormPaths<T>>(
464466
form: Writable<T> | SuperForm<T, unknown>,
465467
path: Path,
466468
options?: ProxyOptions

src/lib/stringPath.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
export function splitPath(path: string) {
23
return path
34
.toString()
@@ -20,7 +21,6 @@ type BuiltInObjects = Date | Set<unknown> | File;
2021

2122
export type AllKeys<T> = T extends T ? keyof T : never;
2223

23-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2424
export type PickType<T, K extends AllKeys<T>> = T extends { [k in K]: any } ? T[K] : never;
2525

2626
// Thanks to https://dev.to/lucianbc/union-type-merging-in-typescript-9al
@@ -31,27 +31,27 @@ export type MergeUnion<T> = {
3131
/**
3232
* Lists all paths in an object as string accessors.
3333
*/
34-
export type FormPath<T extends object, Type = never> = string &
34+
export type FormPath<T extends object, Type = any> = string &
3535
StringPath<T, { filter: 'all'; objAppend: never; path: ''; type: Type }>;
3636

3737
/**
3838
* List paths in an object as string accessors, but only with non-objects as accessible properties.
3939
* Similar to the leaves in a node tree, if you look at the object as a tree structure.
4040
*/
41-
export type FormPathLeaves<T extends object, Type = never> = string &
41+
export type FormPathLeaves<T extends object, Type = any> = string &
4242
StringPath<T, { filter: 'leaves'; objAppend: never; path: ''; type: Type }>;
4343

4444
/**
4545
* List paths in an object as string accessors, but only with non-objects as accessible properties.
4646
* Also includes the _errors field for objects and arrays.
4747
*/
48-
export type FormPathLeavesWithErrors<T extends object, Type = never> = string &
48+
export type FormPathLeavesWithErrors<T extends object, Type = any> = string &
4949
StringPath<T, { filter: 'leaves'; objAppend: '_errors'; path: ''; type: Type }>;
5050

5151
/**
5252
* List all arrays in an object as string accessors.
5353
*/
54-
export type FormPathArrays<T extends object, Type = never> = string &
54+
export type FormPathArrays<T extends object, Type = any> = string &
5555
StringPath<T, { filter: 'arrays'; objAppend: never; path: ''; type: Type }>;
5656

5757
type Concat<
@@ -63,7 +63,7 @@ type StringPathOptions = {
6363
filter: 'arrays' | 'leaves' | 'all';
6464
objAppend: string | never;
6565
path: string;
66-
type: unknown | never;
66+
type: any;
6767
};
6868

6969
type If<

src/routes/(v1)/tests/reset-component-2/TextField.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
export let field: string;
99
export let form;
1010
11-
// @ts-expect-error Purposefully untyped
1211
const { value, errors } = formFieldProxy(form, field);
1312
</script>
1413

src/routes/(v2)/v2/Navigation.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
'onchange-target',
4040
'issue-356',
4141
'issue-358',
42-
'issue-360'
42+
'issue-360',
43+
'unknown-in-schema'
4344
].sort();
4445
</script>
4546

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { zod } from '$lib/adapters/zod.js';
2+
import { message, superValidate } from '$lib/server/index.js';
3+
import { schema } from './schema.js';
4+
import { fail } from '@sveltejs/kit';
5+
6+
export const load = async () => {
7+
const form = await superValidate(zod(schema));
8+
return { form };
9+
};
10+
11+
export const actions = {
12+
default: async ({ request }) => {
13+
const formData = await request.formData();
14+
console.log(formData);
15+
16+
const form = await superValidate(formData, zod(schema));
17+
console.log(form);
18+
19+
if (!form.valid) return fail(400, { form });
20+
21+
return message(form, 'Posted OK!');
22+
}
23+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client/index.js';
3+
//import { zod } from '$lib/adapters/zod.js'
4+
//import { schema } from './schema.js';
5+
import FormCmptAge from './FormCmptAge.svelte';
6+
7+
export let data;
8+
9+
const form = superForm(data.form, {
10+
taintedMessage: false
11+
//dataType: 'json',
12+
//validators: zod(schema)
13+
});
14+
</script>
15+
16+
<FormCmptAge {form} field="age" />
17+
<FormCmptAge {form} field="homePlanet" />
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script lang="ts" context="module">
2+
type T = Record<string, unknown>;
3+
type Value = number;
4+
</script>
5+
6+
<script lang="ts" generics="T extends Record<string, unknown>">
7+
import {
8+
formFieldProxy,
9+
type SuperForm,
10+
type FormPathLeaves,
11+
type FormFieldProxy
12+
} from '$lib/index.js';
13+
14+
export let form: SuperForm<T>;
15+
export let field: FormPathLeaves<T, Value>;
16+
17+
const { value } = formFieldProxy(form, field) as FormFieldProxy<Value>;
18+
</script>
19+
20+
{$value.toExponential()}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
name: z.string(),
5+
age: z.number(),
6+
homePlanet: z.unknown()
7+
});

0 commit comments

Comments
 (0)